How to download a portion of a video with youtube-dl OR something else?

37,117

Solution 1

There is indeed a plethora of techniques available online to accomplish this. One fairly basic technique is the following one liner which works well enough on my system with a YouTube clip:

ffmpeg -i $(youtube-dl -f 18 --get-url https://www.youtube.com/watch?v=ZbZSe6N_BXs) \
-ss 00:00:10 -t 00:00:30 -c:v copy -c:a copy \
happy.mp4

The 2 sections which govern the clip start and end in this example are:

  1. -ss 00:00:10: Placed after the input file this encodes and discards samples up until the 10 second mark. This is slower and less efficient than placing the seek options before the input file (input seeking) but works better in this example (in particular when copying audio and video streams)
  2. -t 00:00:30: This specifies the duration of the encode, in this case 30 seconds only

I have tested this extensively with YouTube and all works well on my own system...

References:

Solution 2

Use the --postprocessor-args parameter to pass the audio/video output to ffmpeg to be edited (the processor). Apparently, ffmpeg is needed to be installed.

--postprocessor-args takes 3 arguments & values (this is just an example, check manual page of ffmpeg with man ffmpeg for more):

  • -ss HH:MM:SS : start time to take
  • -to HH:MM:SS : end time
  • -t HH:MM:SS : time length to take

Examples:

  • Start encoding at 15 seconds and stop at 1 minutes 20 seconds:

    $ youtube-dl --postprocessor-args "-ss 0:0:15 -to 0:1:20" '[video_URL]'
    
  • Start encoding at 15 seconds and take only the next 3 minutes 5 seconds:

    $ youtube-dl --postprocessor-args "-ss 0:0:15 -t 0:3:5" '[video_URL]'
    

PS: youtube-dl will download the entire media before processing it, and will remove it after.

Solution 3

The plumber tool allows you to do this easily. Install the application with:

snap install plumber

After installation, it will appear in your applications menu.

When you run the application, paste your desired youtube link in the box (highlighted in red in the screenshot below), or use the built-in youtube search feature, by clicking the youtube icon (highlighted in yellow).

Choose the start and end points. You can save your result as a video or gif. plumber application window

Solution 4

I followed andrew.46's answer. However, you'd like to put -ss and -t or -to before -i for proper seeking. Otherwise, you'll download more than you want. (-t specifies duration and -to specifies the output point; be careful as they are different)

In the manual of ffmpeg, we are told to use -t or -to after -i. However, based on my experience with ffmpeg, -to does not work after -i; it will be recognized as -t.

My example command is: (to download the cart sliding part of "Coffee Run")

ffmpeg -ss 00:00:37 -to 00:00:44 -i "$(youtube-dl -f best --get-url 'https://youtu.be/PVGeM40dABA')" -c:v copy -c:a copy coffee_sliding.mp4
Share:
37,117

Related videos on Youtube

I'm Here
Author by

I'm Here

Updated on September 18, 2022

Comments

  • I'm Here
    I'm Here over 1 year

    I'd like to be able to download a portion of a video only. For example, being able to specify a start and/or end time for downloading. So, when a user inputs a start and end time of a video, it should only download the portion of the clip within the time stamps. Would this be possible?

  • Wolfpack'08
    Wolfpack'08 almost 5 years
    Yeah, that totally worked for me without the \'s.
  • andrew.46
    andrew.46 almost 5 years
    @Wolfpack'08 Good to hear! Those marks: '/' are simply to show a single line command on multiple lines...
  • andrew.46
    andrew.46 over 4 years
    That is very cool :)
  • Nick Chammas
    Nick Chammas about 3 years
    What's -f 22 for? It looks like that doesn't work with recent versions of youtube-dl.
  • andrew.46
    andrew.46 about 3 years
    @NickChammas That option downloads a specific format which no longer exists on YouTube, thanks for finding the error! I have altered to -f 18 and now it works again (on my system at least!).
  • JAT86
    JAT86 about 3 years
    Is this process lossy or lossless? Will there be a loss of quality in the resulting cut video?
  • thingEvery
    thingEvery almost 3 years
    This worked for me on macOS.
  • shantiq
    shantiq almost 3 years
    This solution is totally awesome and thanx. I wanted the ENG version of this 3:05:06 video of The History of The Eagles from the Arte site. So I had to run the -F command to see which as others dubbed FR or GER. The video was downloaded in full but trimmed to my time specs Very impressive! youtube-dl -f HTTPS_SQ_2 --postprocessor-args "-ss 01:57:00 -t 03:05:06" url
  • kraftydevil
    kraftydevil about 2 years
    This means the entire video is downloaded first, right?