DEV Community

Cover image for Sort YouTube Videos by video duration using JavaScript
Anas Ikhlas
Anas Ikhlas

Posted on

2 1

Sort YouTube Videos by video duration using JavaScript

In this article, we are going to learn how we can sort YouTube channel videos by video duration.

We can sort video from low-to-high and high-to-low video duration. EG: 0:10 to 10:00.

To sort YouTube videos, I have created a package and we are going use that package. Package name is sort-youtube-videos.

First of all, we have to install the package:

npm i sort-youtube-videos
Enter fullscreen mode Exit fullscreen mode

OR if you prefer YARN

yarn add sort-youtube-videos
Enter fullscreen mode Exit fullscreen mode

let's import this package

// if you want to use require
const { sortByDuration } = require("sort-youtube-videos");
//OR
// if you are want to use import
import { sortByDuration } fro"sort-youtube-videos";

// more code ...
Enter fullscreen mode Exit fullscreen mode

To sort videos:

const url = "https://www.youtube.com/channel/UC5RRWuMJu7yP1DQwnW_nAvA"; //enter channel url or channel id
const sortBy = "sl"; // options: sl - ls

sortByDuration(url, sortBy).then((res) => console.log(res));

Enter fullscreen mode Exit fullscreen mode

Note sl means short to long and ls means long to short

And this is the response we will get back:

[
  {
    type: "video",
    title: "Ramadan Ki Shan Episode 11 Digitally Presented by Spiced Foods",
    videoId: "dgoBBYbSKUQ",
    author: "Anas and Ammar",
    authorId: "UC5RRWuMJu7yP1DQwnW_nAvA",
    videoThumbnails: [[Object], [Object], [Object], [Object]],
    viewCountText: "15 views",
    viewCount: 15,
    publishedText: "5 months ago",
    durationText: "4:09",
    lengthSeconds: 249,
    liveNow: false,
    premiere: false,
    premium: false,
  },
  {
    type: "video",
    title:
      "Watch latest movies, web series and TV channels for free without any subscription ||Android",
    videoId: "ummVLFK17GI",
    author: "Anas and Ammar",
    authorId: "UC5RRWuMJu7yP1DQwnW_nAvA",
    videoThumbnails: [[Object], [Object], [Object], [Object]],
    viewCountText: "16 views",
    viewCount: 16,
    publishedText: "1 year ago",
    durationText: "4:11",
    lengthSeconds: 251,
    liveNow: false,
    premiere: false,
    premium: false,
  },
  //   ... 109 more items
];
Enter fullscreen mode Exit fullscreen mode

Here's the full code:

const { sortByDuration } = require("sort-youtube-videos");

const url = "https://www.youtube.com/channel/UC5RRWuMJu7yP1DQwnW_nAvA"; //url or channel id
const sortBy = "sl"; // options: sl - ls

sortByDuration(url, sortBy).then((res) => console.log(res));
Enter fullscreen mode Exit fullscreen mode

To sum up
This process of sorting YouTube videos can take some time and this process might not be that efficient. Because we are fetching all videos from a YouTube channel and than a sorting algorithm will sort the videos. But there might be 1000 video's or more videos on the channel.

So, you might ask...
What will be a better solution?
There is no other solution right now.
If YouTube officially provide this feature than it will be more efficient.

That pretty much it. I hope you found this article helpful.
Happy Coding:)

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

πŸ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay