How to download portion of video with youtube-dl command?

202,579

Solution 1

I don't believe youtube-dl alone will do what you want. However you can combine it with a command line utility like ffmpeg.

First acquire the actual URL using youtube-dl:

youtube-dl -g "https://www.youtube.com/watch?v=V_f2QkBdbRI"

Copy the output of the command and paste it as part of the -i parameter of the next command:

ffmpeg -ss 00:00:15.00 -i "OUTPUT-OF-FIRST URL" -t 00:00:10.00 -c copy out.mp4

The -ss parameter in this position states to discard all input up until 15 seconds into the video. The -t option states to capture for 10 seconds. The rest of the command tells it to store as an mp4.

ffmpeg is a popular tool and should be in any of the popular OS repositories/package managers.

Solution 2

Adding to Johnnie's answer:

Use youtube-dl --youtube-skip-dash-manifest -g "URL" to get the video and audio streams.

Now use:

ffmpeg -ss 12:15 -i "1st-URL" -ss 12:15 -i "2nd-URL" -t 5:15 -map 0:v -map 1:a -c:v libx264 -c:a aac output.mkv

You'll need to use the -ss option for each stream. I also recommend doing it about 30 seconds earlier and then using another -ss 30 to avoid losing any key frames. Here's a real example using one of my youtube videos.

Video

youtube-dl --youtube-skip-dash-manifest -g https://www.youtube.com/watch?v=gESHIrvIQQo

Output:

https://r3---sn-mv-cvne.googlevideo.com/videoplayback/id/80448722bbc8410a/itag/298/source/youtube/requiressl/yes/mn/sn-mv-cvne/ei/BgifWfmmL4iE8wSlv47oCA/mm/31/pl/23/mv/m/ms/au/initcwndbps/11447500/ratebypass/yes/mime/video%2Fmp4/otfp/1/gir/yes/clen/5231968228/lmt/1502479662079137/dur/18575.164/key/dg_yt0/signature/4FFB9B0B7E1703B31F5D07DAD579B55F17EF7BAA.0CB63905C89DD4D33F90CF3AAD728F1ECDFCB9B3/mt/1503594423/ip/206.34.122.70/ipbits/0/expire/1503616102/sparams/ip,ipbits,expire,id,itag,source,requiressl,mn,ei,mm,pl,mv,ms,initcwndbps,ratebypass,mime,otfp,gir,clen,lmt,dur/
https://r3---sn-mv-cvne.googlevideo.com/videoplayback/id/80448722bbc8410a/itag/140/source/youtube/requiressl/yes/mn/sn-mv-cvne/ei/BgifWfmmL4iE8wSlv47oCA/mm/31/pl/23/mv/m/ms/au/initcwndbps/11447500/ratebypass/yes/mime/audio%2Fmp4/otfp/1/gir/yes/clen/295235970/lmt/1502480001536214/dur/18575.243/key/dg_yt0/signature/4CD42047D1D5C714377350905C1CC5CBA37C0009.6EED1FC92D17A096235C32E48F4B15DEF7DD99B0/mt/1503594423/ip/206.34.122.70/ipbits/0/expire/1503616102/sparams/ip,ipbits,expire,id,itag,source,requiressl,mn,ei,mm,pl,mv,ms,initcwndbps,ratebypass,mime,otfp,gir,clen,lmt,dur/

I wanted to cut from 43:00 to 50:10 so I'm going to do -ss 42:30 (giving me a few seconds to catch a good keyframe) on both inputs and then do a -ss 30 after the inputs to start at 43:00.

I'll then use map to map the video 0:v and audio 1:a (0 means first input, which is the video and 1 means the second input, which is the audio) and then choose my encoding options.

# The first URL
video_url="https://r3---sn-mv-cvne.googlevideo.com/videoplayback/id/80448722bbc8410a/itag/298/source/youtube/requiressl/yes/pl/23/ei/5wCfWY6dBeOj8gSSxZaACQ/mv/m/initcwndbps/5055000/ms/au/mm/31/mn/sn-mv-cvne/ratebypass/yes/mime/video%2Fmp4/otfp/1/gir/yes/clen/5231968228/lmt/1502479662079137/dur/18575.164/key/dg_yt0/beids/%5B9466591%5D/mt/1503592562/signature/8CCFBF5CAB97341D0CB1F34E85AB6EE20FC7A03E.7679F39A8603CF41A95F10232E2A921EB0774101/ip/206.34.122.70/ipbits/0/expire/1503614279/sparams/ip,ipbits,expire,id,itag,source,requiressl,pl,ei,mv,initcwndbps,ms,mm,mn,ratebypass,mime,otfp,gir,clen,lmt,dur/"
# The second URL
audio_url="https://r3---sn-mv-cvne.googlevideo.com/videoplayback/id/80448722bbc8410a/itag/140/source/youtube/requiressl/yes/pl/23/ei/5wCfWY6dBeOj8gSSxZaACQ/mv/m/initcwndbps/5055000/ms/au/mm/31/mn/sn-mv-cvne/ratebypass/yes/mime/audio%2Fmp4/otfp/1/gir/yes/clen/295235970/lmt/1502480001536214/dur/18575.243/key/dg_yt0/beids/%5B9466591%5D/mt/1503592562/signature/4AACC8E27F9036D36D4D623A771A9F2BAC4674BA.7E4F4FB4DC023E3FE491A991F0F9F2329648DE9D/ip/206.34.122.70/ipbits/0/expire/1503614279/sparams/ip,ipbits,expire,id,itag,source,requiressl,pl,ei,mv,initcwndbps,ms,mm,mn,ratebypass,mime,otfp,gir,clen,lmt,dur/"
ffmpeg -ss 42:30 -i "$video_url" -ss 42:30 -i "$audio_url" -map 0:v -map 1:a -ss 30 -t 7:10 -c:v libx264 -c:a aac gog-vs-triv.mkv

Credit to Jakub Vrána for the --youtube-skip-dash-manifest solution.

Solution 3

ffmpeg $(youtube-dl -g 'https://www.youtube.com/watch?v=oHg5SJYRHA0' | sed 's/^/-ss 00:05 -i /') -t 01:00 -c copy out.mkv

Edit the URL and the -ss, -t times to use this command. It creates the arguments, using both video and audio URLs, similar to this answer by @godofgrunts. No quotes are used for $() because this produces two separate arguments for ffmpeg.

Solution 4

youtube-dl supports passing arguments to the underlying postprocessor (tested with version 2017.08.27.1):

youtube-dl -x --postprocessor-args "-ss 00:13:00.00 -t 00:04:00.00" https://youtu.be/...

This is basically the same as doing the postprocessing with ffmpeg yourself after downloading the file first.

Solution 5

This feature request is not yet implemented in youtube-dl. See #622 issue and many duplicates of it on github.

Share:
202,579

Related videos on Youtube

user3181614
Author by

user3181614

Updated on September 18, 2022

Comments

  • user3181614
    user3181614 almost 2 years

    I am using Ubuntu, and the youtube-dl command is working absolutely fine.

    However, now I want to download only a portion a video that is too long. So I want to download only a few minutes of that video, e.g. from minute 13 to minute 17.

    Is there any way to do that?

    • schnedan
      schnedan almost 3 years
      youtube-dl does not know anything about video coding and video-frames... so you will need to download everything and cut it with a program which has video editing capabilities
  • Capstone
    Capstone about 7 years
    You can use the -to option instead of -t if you want to specify the time for ending the video slice. ffmpeg -i "OUTPUT-OF-FIRST URL" -ss 00:13:00.00 -to 00:17:00.00 -c copy out.mp4
  • peterh
    peterh almost 7 years
    Could you explain, what it does, how it works?
  • Steve Goranson
    Steve Goranson almost 7 years
    It's just a more terse version of @godofgrunts answer, using sed to save some time manually grabbing the URLs and copying/pasting in the start times (-ss 00:05). @godofgrunts has some extra flags -map 0:v -map 1:a -c:v libx264 -c:a aac that might improve quality. I also like to add youtube-dl -f bestvideo+bestaudio to be sure I'm getting the highest quality version.
  • Kasparov92
    Kasparov92 almost 6 years
    but this will still require to download the whole video, then cut the desired part
  • Hassaan
    Hassaan over 5 years
    I am getting unable to obtain file audio codec with ffprobe error. Can you please help me with that. I am using CentOS 7
  • akaihola
    akaihola over 5 years
    @Antony ffmpeg will actually only download the part you specify.
  • Wolfpack'08
    Wolfpack'08 about 5 years
  • Manny_Mar
    Manny_Mar about 5 years
    Thanks. I was not sure how to get sound, for @Johnnie post. Your post answered it.
  • Geremia
    Geremia about 5 years
    Wow, I didn't know ffmpeg's -i flag could take URLs. neat
  • daGo
    daGo over 4 years
    How does it differ from using --postprocessor-args directly in youtube-dl?
  • daGo
    daGo over 4 years
    It didn't saved audio for a video in my case unlike @godofgrunts answer.
  • programmar
    programmar over 4 years
    @Antony As long as you put the -ss parameter before the -i parameter, then it will start downloading from the time you specified with -ss. However, if you place -ss after the -i parameter, then ffmpeg will download from the very beginning of the video and start encoding from where -ss specifies. See my edit of Johnnie's answer for more info.
  • Aristos Georgiou
    Aristos Georgiou over 4 years
    Yes! Very clean and easy to remember.
  • StevieD
    StevieD about 4 years
    The end of my audio file is getting clipped by several seconds so there is just silence.
  • Fabián
    Fabián almost 4 years
    using the link provided by youtube-dl with the -g option will always return 403 forbidden for me. Using youtube-dl alone to download the video, will always work like a charm.
  • ynn
    ynn almost 4 years
    @LeftoverSalad I have exactly the same issue. Have you found any solutions?
  • Leftover Salad
    Leftover Salad almost 4 years
    @ynn nope :/ I ended up downloading the whole thing and editing afterwards
  • ynn
    ynn almost 4 years
    @LeftoverSalad Found one solution.
  • ewen-goisot
    ewen-goisot almost 4 years
    your command does not work on GNU/Linux: we use youtube-dl instead of youtube-dl.exe. And ffmpeg told me Invalid duration specification for ss: 00.00.10.00, it should be -ss 00:00:30.00 with colons instead of periods except for the last one.
  • ewen-goisot
    ewen-goisot almost 4 years
    the last -ss 30 works very well for me: without it, I get 5 seconds too much on a 10 seconds video portion (and the audio starts before at minus 5 seconds, it was very strange), and with -ss 30 or even -ss 5 I get a 10.022 seconds portion. I tried with this URL: https://www.youtube.com/watch?v=fOe7ip5nNgQ to get a portion from 00:00:30.00 to 00:00:40.00.
  • Yuki
    Yuki almost 4 years
    @ewin-goisot I am sorry, this happened because i didn't copy-pasted the whole command after testing it. Now it should work. the problem was the time definition. it has to be 00:00:00.00 2 times double point, than only 1 point. like hh:mm:ss.ms
  • ewen-goisot
    ewen-goisot almost 4 years
    with 00:00:00.0, it works better, I can download a portion of the video. I still have an issue (with your command and most of other ones) : it downloads 5 or 10 seconds too much. The solution is given on @godofgrunts ' s answer: -ss 42:30 -i "$video_url" -ss 42:30 -i "$audio_url" -map 0:v -map 1:a -ss 30 starting 30 seconds before the portion start and adding a -ss 30 at the end. So I need to use several times the -ss option on the same command. Is there a way to do the same with --external-downloader-args ?
  • Yuki
    Yuki almost 4 years
    ah sorry, i am not that familiar with mixing different streams. but if you want to take just the best audio and video material of the same video, than you can do it with youtube-dl instead
  • antgiant
    antgiant over 3 years
    This will not reliably work, as —postprocessor-args is applied multiple times in some cases. See github.com/ytdl-org/youtube-dl/issues/26863
  • antgiant
    antgiant over 3 years
    This will not reliably work, as —postprocessor-args is applied multiple times in some cases. See github.com/ytdl-org/youtube-dl/issues/26863
  • godofgrunts
    godofgrunts over 3 years
    @LeftoverSalad @ynn I've edited the answer to this solution to fix the manifest issue. You basically need to add --youtube-skip-dash-manifest in the original youtube-dl command.
  • NickD
    NickD over 3 years
    The "extra flags" deal with the two streams (video and audio) which I think you are mishandling - IIUC they have nothing to do with improving quality (well, having sound is probably a mark of improved quality :-) ) The answer by @godofgrunts below seems to be correct.
  • Overflowh
    Overflowh over 3 years
    For a video that comes in many formats/resolutions (1080p, 1440p, 2160p), which one will be downloaded this way? And is there a way to chose it?
  • Geremia
    Geremia over 3 years
    @Roy That requires downloading the entire video first.
  • Ciro Santilli Путлер Капут 六四事
    Ciro Santilli Путлер Капут 六四事 over 3 years
    "ytdl-org locked as off topic and limited conversation to collaborators on May 25, 2020". If someone finds the new canonical would be good to update this answer to.
  • godofgrunts
    godofgrunts over 3 years
    @Overflowh Yeah. If you use -F like youtube-dl -F https://www.youtube.com/watch?v=gESHIrvIQQo you'll get a list of formats. Then you use the number in the format column with -f (note the lowercase) in the rest of the commands. Like youtube-dl --youtube-skip-dash-manifest -g -f 302 https://www.youtube.com/watch?v=gESHIrvIQQo for the video URL and then youtube-dl --youtube-skip-dash-manifest -g -f 251 https://www.youtube.com/watch?v=gESHIrvIQQo for the audio.
  • Peter.k
    Peter.k about 3 years
    I struggle to copy this link and add -f parameter, if need only video with -f 248 for example.
  • Hashim Aziz
    Hashim Aziz about 3 years
    As with most solutions this downloads the entire video rather than just a portion of the video.
  • Hashim Aziz
    Hashim Aziz about 3 years
    This only seems to work when using the -t option - -to doesn't seem possible to use, which is a shame cause I'd rather pass the timestamp I need directly instead of calculating how many minutes and seconds of the video I need. Any idea whether this is possible to do using your solution?
  • Hashim Aziz
    Hashim Aziz about 3 years
    @NickD That's the apparent case, but I found that audio and video work fine even without -map, so it's possible @godofgrunt's -map is redundant for some reason - I have a feeling that reason is that it's the default mapping (and therefore automatically applied).
  • godofgrunts
    godofgrunts almost 3 years
    @HashimAziz I don't think so since we have to skip the manifest in order for this solution to work.
  • Hashim Aziz
    Hashim Aziz almost 3 years
    @godofgrunts It turned out to be a case of positioning them before the inputs, I posted my own question and answer here: superuser.com/questions/1661048/….
  • ejbytes
    ejbytes almost 3 years
    I'd like to use this answer, but I prefer using the youtube name (eg "c:\myvideos\%%(title)s-%%(id)s.%%(ext)s ) -- how would you use this with your answer? A line I tried to use but failed with an error message of mixed argument design: %program% -f %option% "%youtubelink%" "%option2%" ffmpeg -ss %startTime% -i "OUTPUT-OF-FIRST URL" -t %endTime% -o "%MYDLDIR%%EXTS%"
  • xpt
    xpt almost 3 years
    Thanks and welcome aboard ThinkWithP_body 🎉🎉!
  • Jakub Vrána
    Jakub Vrána almost 3 years
    The time is not exact with this solution (it starts at the last keyframe before 0:05) and the sound is out of sync (video and audio streams have different start point but they are merged since the beginning so it is both out of sync and the sound is missing at the end of the video). What I ended up doing is downloading the streams separately: youtube-dl -k --external-downloader ffmpeg --external-downloader-args '-ss 5 -t 60' oHg5SJYRHA0 And then merging the streams from the end: ffmpeg -sseof -60 -i video.webm -sseof -60 audio.mp4 -map 0:v:0 -map 1:a:0 out.mkv
  • Paul
    Paul over 2 years
    Yes, it seems to be the only possibility not to download all 12 hours of video.
  • Woeitg
    Woeitg over 2 years
    the only working solution for me.
  • Mehdi Haghgoo
    Mehdi Haghgoo over 2 years
    This downloads no video. Only audio is downloaded.
  • Mehdi Haghgoo
    Mehdi Haghgoo over 2 years
    Heads up man, this works!
  • Mohammad Momeni
    Mohammad Momeni over 2 years
    About that trickier part, do you have any solution like getting the first metadata bytes, and merge it with a specific range, I tried it but there's an issue
  • Simon East
    Simon East about 2 years
    @HashimAziz - not from my testing. I was able to download a Rumble.com video using the above method and it only downloaded the small portion I specified, not the whole 3hr broadcast. Phew!
  • Admin
    Admin about 2 years
    This is excelent.
  • Admin
    Admin about 2 years
    Audio is not captured
  • Admin
    Admin about 2 years
    This should be the accepted answer, the most voted one doesn't capture audio.