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"
--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
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".
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.
Featured image by kapil 99 from Pixabay.
Not working!.. The entire video is downloading..no matter what args I pass in postprocessor agrs
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.
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.
Yes, Youtube-dl will download the entire video before processing it.
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 🙂
Works great, thanks for the tip
Glad to help.
It works and it good tutorial
I want to ask
how can you cancel the download in code way
How can you download 1080p
>> 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/
What you type in the command to cancel the download
Just press CTRL+C to cancel the download.
Worked for me like a charm for Fedora 35! Great post!
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
Never had a chance to test it on Windows. Probably it should work.