19

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?

2

4 Answers 4

25

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:

5
  • Yeah, that totally worked for me without the \'s. Jun 5, 2019 at 5:41
  • 1
    @Wolfpack'08 Good to hear! Those marks: '/' are simply to show a single line command on multiple lines...
    – andrew.46
    Jun 5, 2019 at 7:07
  • What's -f 22 for? It looks like that doesn't work with recent versions of youtube-dl. Mar 10, 2021 at 1:43
  • 1
    @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!).
    – andrew.46
    Mar 10, 2021 at 2:02
  • Is this process lossy or lossless? Will there be a loss of quality in the resulting cut video?
    – JAT86
    Mar 13, 2021 at 12:31
13

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.

3
  • 1
    That is very cool :)
    – andrew.46
    Oct 15, 2019 at 0:24
  • 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
    – shantiq
    Aug 14, 2021 at 19:39
  • This means the entire video is downloaded first, right? Mar 12 at 20:16
5

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

3

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
1
  • This worked for me on macOS.
    – thingEvery
    Jul 3, 2021 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.