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: Last Updated on 5110 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 just 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". In both cases,

Hope this helps.


Featured image by kapil 99 from Pixabay.

You May Also Like

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. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More