Home YouTube Download A Portion Of Youtube Video with Youtube-dl And FFmpeg

Download A Portion Of Youtube Video with Youtube-dl And FFmpeg

By sk
Published: Updated: 34.7K views

Even though there are multitude of programs available to download and encode videos, I prefer to use Youtube-dl and FFmpeg. These two utilities are enough to download videos from online and encode, decode, transcode, crop and mux the downloaded videos. Today, we will see how to download a portion of YouTube video with Youtube-dl and FFmpeg in Linux.

Before I know this tip, I would usually first download the entire video using Youtube-dl and then trim it down to my desired duration using FFmpeg as shown below.

To download a video, I would do:

$ youtube-dl <youtube-link>

And then trim the downloaded video using starting and ending time of the video clip as per my liking with FFmpeg using command:

$ ffmpeg -i input_video.mp4 -ss 00:01:00 -to 00:03:00 -c copy output_video.mp4

This is what I was doing to get only a portion of video. If you still follow this method to download and trim videos, don't bother. I know slightly a better a way to download parts of youtube video instead of full video. Read on!

Download a Portion of Youtube Video with Youtube-dl and FFmpeg

First, we will see how to download a specific part of youtube video using Youtube-dl script.

For the purpose of this guide, I will be using this video from the official YouTube channel for our OSTechNix blog. This video is 2:43 minutes long and it basically demonstrates how to launch web browsers in full screen mode in Linux.

If you haven't installed Youtube-dl on your Linux machine yet, refer the links attached in the first paragraph.

Youtube-dl has the --postprocessor-args parameter to download a specific portion of a youtube video or videos from several other websites.

To download a portion of a video with Youtube-dl, run:

$ youtube-dl --postprocessor-args "-ss 00:01:00 -to 00:02:00" "https://www.youtube.com/watch?v=dc7I-i7sPrg"

Here, --ss 00:01:00 is the starting time of video and -to 00:02:00 is the ending time. So the above command will start encoding at 1 minute and end at 2 minutes.

You can also use -t flag to specify the total time duration.

$ youtube-dl --postprocessor-args "-ss 00:01:00 -t 00:00:50" "https://www.youtube.com/watch?v=dc7I-i7sPrg"

This will start encoding at 1 minute and take only the next 50 seconds.

Please note that Youtube-dl will download the entire video before processing it. After trimming the video to the given length, it will delete the original video and keep only the portion of the video. If you want to keep both, simply pass the -k flag.

If you don't want to download the entire video but only a part of it, you can combine ffmpeg and youtube-dl to download a specific portion of the youtube video like below:

$ youtube-dl --external-downloader ffmpeg --external-downloader-args "-ss 00:01:00.00 -to 00:02:00.00" -f best "https://www.youtube.com/watch?v=dc7I-i7sPrg"

Here, we used -f best flag to download the best quality audio and video.

The above command will download the given Youtube video with Youtube-dl script and then trim it starting from 1 minute and end at 2 minute.

Here is another version of the command that does the same job.

$ ffmpeg -ss 00:01:00 -to 00:02:00 -i "$(youtube-dl -f best --get-url 'https://www.youtube.com/watch?v=dc7I-i7sPrg')" -c:v copy -c:a copy kiosk.mp4

This command does the same as the above command but saves the output video as "kiosk.mp4".

Update:

As one of our reader commented below, if the above tip doesn't work for any reason, just add decimal after seconds like "-ss 00:01:00.00 -to 00:02:00.00" instead of "-ss 00:01:00 -to 00:02:00". I haven't personally tested this though. However, the reader claims it does work.

Hope this helps.

Reference

Featured image by kapil 99 from Pixabay.

You May Also Like

14 comments

Umakanth P August 14, 2021 - 11:59 am

Not working!.. The entire video is downloading..no matter what args I pass in postprocessor agrs

Reply
sk August 14, 2021 - 7:29 pm

I just verified it on my Fedora Desktop. It works as described in the guide. I tried the following video:

$ youtube-dl --postprocessor-args "-ss 00:03:35 -to 00:04:35" "https://www.youtube.com/watch?v=4jTy5jnMkYc"

It exactly downloaded the 1 minute video starting from 3.35 to 4.35 time interval.

Reply
Ridwan October 19, 2021 - 5:25 pm

But in my case, it’s actually downloading from 00:00:00 to 00:04:35 , but the resulting file is from 00:03:35 to 00:04:35. So if I want to download a video from 04:03:35 -to 04:04:35, i have to actually download and waste 4 hour video worth of data, to clip a minute of that video.

Reply
sk October 19, 2021 - 6:52 pm

Yes, Youtube-dl will download the entire video before processing it.

Reply
Riz January 16, 2022 - 12:29 pm

I have Win 10, and it is working fine for me. Only error made by author in the article is that you have to add decimal after seconds.

“-ss 00:01:00.00 -to 00:02:00.00” instead of “-ss 00:01:00 -to 00:02:00”

Hope this helps 🙂

Reply
PS September 15, 2021 - 3:57 am

Works great, thanks for the tip

Reply
sk September 15, 2021 - 10:46 am

Glad to help.

Reply
Gil October 2, 2021 - 12:48 pm

It works and it good tutorial

I want to ask
how can you cancel the download in code way
How can you download 1080p

Thankyou

Reply
sk October 2, 2021 - 2:10 pm

>> how can you cancel the download in code way
I don’t know what do you mean by “code” way. Please elaborate.

>> How can you download 1080p
Youtube-dl has an option to download videos in specific format. Refer Youtube-dl tutorial. https://ostechnix.com/youtube-dl-tutorial-with-examples-for-beginners/

Reply
GIL October 3, 2021 - 1:29 pm

What you type in the command to cancel the download

thanks

Reply
sk October 3, 2021 - 2:41 pm

Just press CTRL+C to cancel the download.

Reply
Adrian Rosoga November 7, 2021 - 6:11 pm

Worked for me like a charm for Fedora 35! Great post!

Reply
Denzel Brown November 28, 2021 - 11:45 am

Does this same code work in a windows command prompt? I’m trying to download a clip that’s 7 seconds with this exact snippet, but the video and audio is desynced and pixelated in the beginning of the clip

Reply
sk November 28, 2021 - 12:35 pm

Never had a chance to test it on Windows. Probably it should work.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More