ytdl-org/youtube-dl

Download range

raducostea opened this issue Β· 88 comments

Hi,

Is there any way to implement a download range? To download starting from 1m 30 to 1m 50?

Thanks

This is request is more complex than it sounds:

  • At first, we need to be able to download byte ranges. That is fairly easy and possible, but still needs to be implemented and tested.
  • Then, we need a way to determine which bytes correspond to 90s and 110s, most likely by peeking into the first or last bytes of the file.
  • Finally, you probably want a full video file, and not a video sequence. We need to reassemble that sequence.

All, in all, this is quite complex, but certainly doable. Feel free to contribute patches or pull request, even for the individual steps.

Hi,

I've tried to implement the Byte Range and it worked but of course the video will not play. Probably i need to reassemble the sequence somehow

Feel free to submit a pull request for the byte range. It's far easier to integrate code in small steps than all at once, and others may chip in.

hi guys,

this feature is super important for us. any updates on this issue?

Hi
How could I Download bytes-ranges via youtube-dl?

Thanks

@shaybracha today its too late. I will see tomorrow and will tell you. πŸ‘

@yasoob thanks...waiting for your answer

Sorry I was not able to do it today because my laptop broke :( okay you
will have to use an external script to get the bytes value.make your own
python script. You can import youtube-dl and extract the info then you can
use requests with stream=true to get the byte range . This is the
procedure. However I will post the complete code in 1 or 2 days after I get
my laptop repaired. Thanx for your patience.
On 1 Jul 2013 11:38, "shaybracha" notifications@github.com wrote:

@yasoob https://github.com/yasoob thanks...waiting for your answer

β€”
Reply to this email directly or view it on GitHubhttps://github.com//issues/622#issuecomment-20265446
.

This is fairly easy to accomplish with ffmpeg. Example:

ffmpeg -ss (start time) -i (direct video link) -t (duration you need) -c:v copy -c:a copy (title.mp4)

You can get the direct link by using the --get-url option.

I don't know this projects feelings on adding (optional) dependancies, but this would be trivial to implement provided ffmpeg (compiled with open-ssl) is available. If adding the dependency is not an option, then, as I'm sure you're all aware, this is a pretty mammoth task.

Users who have ffmpeg installed can use method this in the meantime.

For current twitch.tv VoDs, byte-range is not necessarily needed, as they are split into 4-second chunks, and m3u8 playlist specifies all the chunk lengths precisely (e.g. #EXTINF:3.240,), so if ~4s seek precision is tolerable (which it probably is) - it's only a matter of running through playlist to pick the links needed.

I did write a proof-of-concept script for that here, and it seem to work nicely, except for some sequential download performance issues, which kinda rule-out downloading whole thing with ffmpeg directly.
Did a write-up on on the whole process here.

@JaySandhu please provide some complete example: cause I did this: ffmpeg -ss 14350 -i https://www.youtube.com/watch?v=mMZriSvaVP8 --get-url -t 11200 -c:v copy -c:a copy react-spot.mp4
and it did not work.

@roberto68 Sorry for the confusion, to get a direct link to the video you need to use youtube-dl's --get-url option. Then, with a direct video link from youtube-dl, you can use ffmpeg as described above.

Example:
youtube-dl -f 22 --get-url https://www.youtube.com/watch?v=mMZriSvaVP8

Then with the link:
ffmpeg -ss 14350 -i (link) -t 11200 -c:v copy -c:a copy react-spot.mp4

One line version:
ffmpeg -ss 14350 -i $(youtube-dl -f 22 --get-url https://www.youtube.com/watch?v=mMZriSvaVP8) -t 11200 -c:v copy -c:a copy react-spot.mp4

You can either pick a format with both video and audio (such as -f 22), or you can use ffmpeg to combine a DASH audio and video stream. You'll need to make sure you're ffmpeg build includes open-ssl (so that it can download the video over https).

thanks JaySandhu, now it's workin right way :)

I can give it a try to implement this feature.. Right now when I tried to skip the video, youtube sent a request shown below. I then tried downloading the link directly with wget and I also got a file of about 500 KB but it did not play in VLC, KMPlayer, MPlayer, etc. So maybe there is another missing piece?

Link:
https://r3---sn-ci5gup-cvhz.googlevideo.com/videoplayback?initcwndbps=365000&sparams=clen%2Cdur%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cupn%2Cexpire&id=o-AE1c25o1pRu61sxBCZWQksq3ixkGB_Uqtl2xKDhs1LUa&sver=3&ip=106.221.136.162&mv=m&mt=1456387095&ms=au&fexp=3300134%2C3300164%2C3311882%2C3312381%2C3312529%2C9405186%2C9408497%2C9413141%2C9416126%2C9418200%2C9419451%2C9420452%2C9422342%2C9422596%2C9423661%2C9423662%2C9425283%2C9425790%2C9427107%2C9427422%2C9428649%2C9428710%2C9429072%2C9429597&dur=11346.869&upn=M38qsT9uuVg&mn=sn-ci5gup-cvhz&mm=31&source=youtube&signature=D0FD6BB2BD5E82C7B882813C552C06EDBDCE2A82.1E85213F62C2E69F4FCEA7D7E87B8BC9D00CB7E8&lmt=1418940170813485&ipbits=0&key=yt6&itag=247&mime=video%2Fwebm&clen=2245741091&pl=21&keepalive=yes&requiressl=yes&expire=1456408861&gir=yes&cpn=TR7wBJmFEBEkwtVX&alr=yes&ratebypass=yes&c=WEB&cver=html5&range=415184135-417218639&rn=28&rbuf=31205

Params:

initcwndbps:365000
sparams:clen,dur,gir,id,initcwndbps,ip,ipbits,itag,keepalive,lmt,mime,mm,mn,ms,mv,pl,requiressl,source,upn,expire
id:o-AE1c25o1pRu61sxBCZWQksq3ixkGB_Uqtl2xKDhs1LUa
sver:3
ip:106.221.126.162
mv:m
mt:1456387095
ms:au
fexp:3300134,3300164,3311882,3312381,3312529,9405186,9408497,9413141,9416126,9418200,9419451,9420452,9422342,9422596,9423661,9423662,9425283,9425790,9427107,9427422,9428649,9428710,9429072,9429597
dur:11346.869
upn:M38qsT9uuVg
mn:sn-ci5gup-cvhz
mm:31
source:youtube
signature:D0FD6BB2BD5E82C7B882813C552C06EDBDCE2A82.1E85213F62C2E69F4FCEA7D7E87B8BC9D00CB7E8
lmt:1418940170813485
ipbits:0
key:yt6
itag:247
mime:video/webm
clen:2245741091
pl:21
keepalive:yes
requiressl:yes
expire:1456408861
gir:yes
cpn:TR7wBJmFEBEkwtVX
alr:yes
ratebypass:yes
c:WEB
cver:html5
range:415184135-417218639
rn:28
rbuf:31205

can someone give me any pointers from their earlier research on this to hep me?

Anyone ever figure this one out? Sending range or begin params doesn't work for me, curious if anyone has found a working solution?

/me would also be interested.

Whoops, sorry for the dupe. Although in #12347 I asked:

I realize we can sort of achieve this today by passing args to ffmpeg, e.g.
youtube-dl ... --external-downloader ffmpeg --external-downloader-args '-ss 3718.538 -t 60'
or perhaps as a postprocessor arg where appropriate.
But that's not very user-friendly, and knowing when to choose an external downloader versus a postprocessor may not be obvious.

I guess maybe this could just be a FAQ entry, but I think some options would be a better goal.

Can we get some interim documentation in the absence of a real user-oriented feature?

There are already quite a few such cases (workarounds for missing features), and I guess they will make README.md even more bloated. IMO it's enough to listed them in relevant issues. Your comment will definitely benifit those who need it.

Fair enough. Although I found it tough to find this Issue. I did in fact search before filing a new one, although obviously not extremely hard :)

ffmpeg -ss 14350 -i $(youtube-dl -f 22 --get-url https://www.youtube.com/watch?v=videoId) > -t 11200 -c:v copy -c:a copy output.mp4

Was looking for this feature to be able to download a time segment from a video. Is @JaySandhu's 2015 answer still one of the better ways to do this or does youtube-dl support this feature in any way? Don't get me wrong, it's a great answer, I was just curious to see if anyone was aware of current developments seeing how this issue is still open even after 4 years.

I believe there has not been any change.

I'd like to follow @JaySandhu's suggestion but am lost and would greatly appreciate some help!

Let's take this link, of which I would like the first 29 minutes and 54 seconds, as an example:
https://www.youtube.com/watch?v=2QPbriKs_sI&list=PLtra-MWzIvZGdqzuA59Jp0dZVzpmNZyT0&index=2

When I try to get the url of the file via the --get-url option I get this:

youtube-dl --get-url https://www.youtube.com/watch?v=2QPbriKs_sI&list=PLtra-MWzIvZGdqzuA59Jp0dZVzpmNZyT0&index=2
[1] 1322
[2] 1323
[2]+ Done list=PLtra-MWzIvZGdqzuA59Jp0dZVzpmNZyT0
users-MacBook-Pro:~ user$ https://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?id=d903dbae22acfec2&itag=137&source=youtube&requiressl=yes&pcm2cms=yes&ei=fsKGWeyGLcONowO8xIP4AQ&mm=31&pl=20&mn=sn-8vq54voxpo-5hql&initcwndbps=1352500&ms=au&mv=m&ratebypass=yes&mime=video/mp4&gir=yes&clen=16938851529&lmt=1498521946448697&dur=36000.000&key=dg_yt0&signature=224CB1B319564531CA1819805C9EFF3848A9C801.3AC4B5B275625FF0F5D2F6D49F756CDEA4ADCB67&mt=1502003750&ip=42.106.43.220&ipbits=0&expire=1502025438&sparams=ip,ipbits,expire,id,itag,source,requiressl,pcm2cms,ei,mm,pl,mn,initcwndbps,ms,mv,ratebypass,mime,gir,clen,lmt,dur
https://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?mime=audio%2Fwebm&keepalive=yes&source=youtube&pcm2cms=yes&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cexpire&itag=251&expire=1502025437&gir=yes&mm=31&mn=sn-8vq54voxpo-5hql&dur=36000.000&mt=1502003750&mv=m&ms=au&lmt=1498650272424607&clen=526461218&ip=42.106.43.220&key=yt6&ipbits=0&id=o-AJqnSngI1EXKktmEmtNUt0nvM1QwU2pjCTI-SU2tFwEj&ei=fcKGWevQLMnYowPh_qvQBQ&initcwndbps=1352500&pl=20&signature=15B982263BA7D44D7F0FDF9B8D167076E35F8882.2547270D7467DEA9FF6DF36E9A9841061073558D&requiressl=yes&ratebypass=yes

[1]+ Done youtube-dl --get-url https://www.youtube.com/watch?v=2QPbriKs_sI

Please, please help with the following!

1. I am very confused by the output of the --get-url command. Which of these urls do I use?! And what do the [1], [2], [3] and "+ Done" signify? I suspect that I am getting the 2 urls (the ones in the huge chunks) because the audio and video streams are not combined. How do I go about using ffmpeg to combine DASH audio and video streams, as Jay suggests?

2. Do I need to use : or leading zeroes in the timestamp? How do I distinguish 20 seconds from 2 minutes for the (duration you need) portion?

3. Will this command download the entire video and then crop out the portion I want or would it just download the requested portion?

Thanks for weighing in, @siddht4. Wouldn't requesting f -22 download the 720p version, which is the best version with both audio and video?

139          m4a        audio only DASH audio   82k , m4a_dash container, mp4a.40.5@ 48k (22050Hz), 204.18MiB
249          webm       audio only DASH audio   97k , opus @ 50k, 188.44MiB
250          webm       audio only DASH audio  111k , opus @ 70k, 249.28MiB
171          webm       audio only DASH audio  161k , vorbis@128k, 464.74MiB
140          m4a        audio only DASH audio  162k , m4a_dash container, mp4a.40.2@128k (44100Hz), 545.28MiB
251          webm       audio only DASH audio  170k , opus @160k, 502.07MiB
160          mp4        256x144    DASH video  242k , avc1.4d400c, 25fps, video only, 310.34MiB
278          webm       256x144    144p  303k , webm container, vp9, 25fps, video only, 379.01MiB
133          mp4        426x240    DASH video  377k , avc1.4d4015, 25fps, video only, 776.09MiB
242          webm       426x240    240p  444k , vp9, 25fps, video only, 734.48MiB
243          webm       640x360    360p  675k , vp9, 25fps, video only, 1.36GiB
134          mp4        640x360    DASH video  764k , avc1.4d401e, 25fps, video only, 1.63GiB
244          webm       854x480    480p 1022k , vp9, 25fps, video only, 2.53GiB
135          mp4        854x480    DASH video 1390k , avc1.4d401e, 25fps, video only, 3.42GiB
247          webm       1280x720   720p 2035k , vp9, 25fps, video only, 5.38GiB
136          mp4        1280x720   DASH video 2755k , avc1.4d401f, 25fps, video only, 7.39GiB
248          webm       1920x1080  1080p 3608k , vp9, 25fps, video only, 10.03GiB
137          mp4        1920x1080  DASH video 4589k , avc1.640028, 25fps, video only, 15.78GiB
17           3gp        176x144    small , mp4v.20.3, mp4a.40.2@ 24k
36           3gp        320x180    small , mp4v.20.3, mp4a.40.2
43           webm       640x360    medium , vp8.0, vorbis@128k
18           mp4        640x360    medium , avc1.42001E, mp4a.40.2@ 96k
22           mp4        1280x720   hd720 , avc1.64001F, mp4a.40.2@192k (best)

I would like to get the best audio and the best video (in this case 137 - 1920x1080 (DASH video)+251 - audio only (DASH audio)) and let ffmpeg mix the two.

I'm not clear as to what @JaySandhu's means when he mentions the --get-url command. Would ffmpeg -ss 0 -i $(youtube-dl https://www.youtube.com/watch?v=2QPbriKs_sI) > -t 1794 -c:v copy -c:a copy output.mp4 not work?

Thanks @siddht4. My ffmpeg version is 3.3.3.

I'm sorry but I don't follow what you said! Could you please list the command I would use to get the best versions of audio and video and get ffmpeg to combine them?

I like how youtube-dl automatically mixes the two if ffmpeg is present :-)

I still don't get it :-( I would like the best audio + best video but restricted to the first 29 minutes and 54 seconds. Many thanks for your continued help!

How can I use Jay's method without f -22 and with the best audio + video but restricted to the time portion I want?

If I use the command that you posted earlier ffmpeg -ss 0 -i $(youtube-dl -f 22 --get-url https://www.youtube.com/watch?v=2QPbriKs_sI) > -t 1794 -c:v copy -c:a copy output.mp4 but without the -f 22 so, ffmpeg -ss 0 -i $(youtube-dl --get-url https://www.youtube.com/watch?v=2QPbriKs_sI) > -t 1794 -c:v copy -c:a copy output.mp4, I get the following message in Terminal in red text with a black background.

[NULL @ 0x7fbca1802600] Unable to find a suitable output format for 'https://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?signature=8F8A97319A40FA3E51A25E09F13BD38D3249DD6B.0159BD0D26CD4678858582AE1323D4B4122A5C01&requiressl=yes&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cexpire&ei=zweHWfb7NI3AowO_yIq4Bw&itag=251&id=o-AAlFlG-RRe0xpK9AJI-FxVEre2XwhXs_n2druqsjsUzM&ipbits=0&mm=31&mn=sn-8vq54voxpo-5hql&initcwndbps=1380000&clen=526461218&pcm2cms=yes&ip=42.106.36.28&ms=au&mt=1502021495&mv=m&dur=36000.000&gir=yes&keepalive=yes&expire=1502043184&lmt=1498650272424607&key=yt6&mime=audio%2Fwebm&pl=20&source=youtube&ratebypass=yes'
https://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?signature=8F8A97319A40FA3E51A25E09F13BD38D3249DD6B.0159BD0D26CD4678858582AE1323D4B4122A5C01&requiressl=yes&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cexpire&ei=zweHWfb7NI3AowO_yIq4Bw&itag=251&id=o-AAlFlG-RRe0xpK9AJI-FxVEre2XwhXs_n2druqsjsUzM&ipbits=0&mm=31&mn=sn-8vq54voxpo-5hql&initcwndbps=1380000&clen=526461218&pcm2cms=yes&ip=42.106.36.28&ms=au&mt=1502021495&mv=m&dur=36000.000&gir=yes&keepalive=yes&expire=1502043184&lmt=1498650272424607&key=yt6&mime=audio%2Fwebm&pl=20&source=youtube&ratebypass=yes: Invalid argument

Also, trying out the command that you first posted (the one with -f 22) results in this error

[NULL @ 0x7fed06803400] Unable to find a suitable output format for '1794'
1794: Invalid argument

The --get-url option just prints a direct link to the video instead of downloading it.

The complication is that YouTube serves its highest quality content through separate video and audio sources, so youtube-dl's --get-url provides you with two links, one is video and the other is audio. In my previous comment I recommended people use -f 22, which is the best quality source (720p) that has both video and audio, but sometimes that's not available, or you would prefer the best quality version. For that use:

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]' --get-url (youtube link)

To get the urls of the best video and audio sources. Then to combine them with ffmpeg:

ffmpeg -ss (start time) -i (top link) -i (bottom link) -t (duration you need) -c:v copy -c:a copy (title.mp4)

@rEes9P to answer your questions in order:

  1. The output of your --get-url command is a bit screwy because your forgot to put the link in quotes, so the & in the link is being interpreted by bash causing the command to run in the background and leading to a bunch of job control related output - the [1], [2], [3] and "+ Done" part. Also, remove the playlist part of the link else youtube-dl will print links to the entire playlist. The revised command: youtube-dl --get-url "https://www.youtube.com/watch?v=2QPbriKs_sI". That should lead to a more parseable output, but you'll still have two links, you can combine them with ffmpeg as mentioned above.

  2. You can provide timestamps in the format hh:mm:ss or just as an integer in seconds. so 20 seconds would be 00:00:22 or just 22 and 2 minutes would be 00:02:00 or 120.

  3. This will not download the entire video, only the range you request.

@JaySandhu - This is brilliant! Thank you so, so much for the clear and concise instructions.

Hrxn commented

?

How should this be implemented? It depends on the shell, platform, etc..

For those who want to work on this, please check #8851 first. Discussions here only applies to plain file-based streams. That is, a single file contains the whole video and/or audio stream. Fragmented streams (HLS/DASH/HDS/ISM) should be handled differently. We can implement this for file-based streams first, in which the implementation should check whether a given video URL is file-based or not.

There are two possible approaches for file-based streams, ffmpeg-based post convertor or website-dependent URL transformer (ex: &start=xxx for YouTube). The former should be included in the proposed implementation for generality, and the latter can be added as an extra feature.

@JaySandhu, thanks for coming back to this issue. I saw your info about the timestamps usable in the -ss flag. Does the -t flag have to be in seconds, or can it be in hh:mm:ss as well?

Hrxn commented

Both use the same syntax, across all of ffmpeg.
https://www.ffmpeg.org/ffmpeg-utils.html#Time-duration

Thank you @Hrxn.

@JaySandhu - The --get-url part works perfectly after I took out the playlist bit of the url and put the quotes in where appropriate. However, I'm having some trouble with the ffmpeg -ss (start time) -i (top link) -i (bottom link) -t (duration you need) -c:v copy -c:a copy (title.mp4) bit. Could you please have a look and let me know where I'm going wrong?

Here's the command I used

ffmpeg -ss 00 -i β€œhttps://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?id=d903dbae22acfec2&itag=137&source=youtube&requiressl=yes&ei=KhyIWb3MLNT4oAPI2o6oDA&mm=31&initcwndbps=1307500&mv=m&ms=au&mn=sn-8vq54voxpo-5hql&pl=20&ratebypass=yes&mime=video/mp4&gir=yes&clen=16938851529&lmt=1498521946448697&dur=36000.000&mt=1502092202&key=dg_yt0&signature=8BDD2573344F5E26CBD46F7EADFDA55BC161A5E5.42FAA9C3F66466197BC24C027F75248F65222A6E&ip=42.106.36.220&ipbits=0&expire=1502113930&sparams=ip,ipbits,expire,id,itag,source,requiressl,ei,mm,initcwndbps,mv,ms,mn,pl,ratebypass,mime,gir,clen,lmt,dur” -i β€œhttps://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cexpire&lmt=1498650272424607&ipbits=0&requiressl=yes&source=youtube&initcwndbps=1307500&keepalive=yes&ei=KRyIWe-ND8OAowPhz6fYCA&clen=526461218&dur=36000.000&key=yt6&mime=audio%2Fwebm&ip=42.106.36.220&mm=31&mn=sn-8vq54voxpo-5hql&expire=1502113929&itag=251&pl=20&gir=yes&signature=58051D10D2468DE05B4636609C9C2C03E40B50BC.4A76CA4B114D52A55588AB3B35521BB8D0EFA14A&id=o-AI3-NsFb5zK3MD-gr4Fcbkh66Vqw4E_ezwWT8vLMIRC7&mt=1502092202&mv=m&pcm2cms=yes&ms=au&ratebypass=yes” -t 00:01:04 -c:v copy -c:a copy test.mp4

And here's what happened

users-MacBook-Pro:~ user$ ffmpeg -ss 00 -i β€œhttps://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?id=d903dbae22acfec2&itag=137&source=youtube&requiressl=yes&ei=KhyIWb3MLNT4oAPI2o6oDA&mm=31&initcwndbps=1307500&mv=m&ms=au&mn=sn-8vq54voxpo-5hql&pl=20&ratebypass=yes&mime=video/mp4&gir=yes&clen=16938851529&lmt=1498521946448697&dur=36000.000&mt=1502092202&key=dg_yt0&signature=8BDD2573344F5E26CBD46F7EADFDA55BC161A5E5.42FAA9C3F66466197BC24C027F75248F65222A6E&ip=42.106.36.220&ipbits=0&expire=1502113930&sparams=ip,ipbits,expire,id,itag,source,requiressl,ei,mm,initcwndbps,mv,ms,mn,pl,ratebypass,mime,gir,clen,lmt,dur” -i β€œhttps://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cexpire&lmt=1498650272424607&ipbits=0&requiressl=yes&source=youtube&initcwndbps=1307500&keepalive=yes&ei=KRyIWe-ND8OAowPhz6fYCA&clen=526461218&dur=36000.000&key=yt6&mime=audio%2Fwebm&ip=42.106.36.220&mm=31&mn=sn-8vq54voxpo-5hql&expire=1502113929&itag=251&pl=20&gir=yes&signature=58051D10D2468DE05B4636609C9C2C03E40B50BC.4A76CA4B114D52A55588AB3B35521BB8D0EFA14A&id=o-AI3-NsFb5zK3MD-gr4Fcbkh66Vqw4E_ezwWT8vLMIRC7&mt=1502092202&mv=m&pcm2cms=yes&ms=au&ratebypass=yes” -t 00:01:04 -c:v copy -c:a copy test.mp4
[1] 1797
[2] 1798
[3] 1799
[4] 1800
[5] 1801
[6] 1802
[7] 1803
[8] 1804
[9] 1805
[10] 1806
[11] 1807
[12] 1808
[13] 1809
[14] 1810
[15] 1811
[16] 1812
[17] 1813
[18] 1814
[19] 1815
[20] 1816
[21] 1817
[22] 1818
[23] 1819
[24] 1820
[25] 1821
[26] 1822
-bash: -i: command not found
[27] 1823
[28] 1824
[29] 1825
[30] 1826
[31] 1827
[32] 1828
[33] 1829
[34] 1830
[35] 1831
[36] 1832
[37] 1833
[38] 1834
[39] 1835
[40] 1836
[41] 1837
[42] 1838
[43] 1839
[44] 1840
[45] 1841
[46] 1842
[47] 1843
[48] 1844
-bash: -t: command not found
[2] Done itag=137
[3] Done source=youtube
[4] Done requiressl=yes
[5] Done ei=KhyIWb3MLNT4oAPI2o6oDA
[6] Done mm=31
[7] Done initcwndbps=1307500
[8] Done mv=m
[9] Done ms=au
[10] Done mn=sn-8vq54voxpo-5hql
[11] Done pl=20
[12] Done ratebypass=yes
[13] Done mime=video/mp4
[14] Done gir=yes
[15] Done clen=16938851529
[16] Done lmt=1498521946448697
[17] Done dur=36000.000
[18] Done mt=1502092202
[19] Done key=dg_yt0
[20] Done signature=8BDD2573344F5E26CBD46F7EADFDA55BC161A5E5.42FAA9C3F66466197BC24C027F75248F65222A6E
[21] Done ip=42.106.36.220
[22] Done ipbits=0
[23] Done expire=1502113930
[24] Exit 127 sparams=ip,ipbits,expire,id,itag,source,requiressl,ei,mm,initcwndbps,mv,ms,mn,pl,ratebypass,mime,gir,clen,lmt,dur” -i β€œhttps://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cexpire
[25] Done lmt=1498650272424607
[26] Done ipbits=0
[27] Done requiressl=yes
[28] Done source=youtube
[29] Done initcwndbps=1307500
[30] Done keepalive=yes
[31] Done ei=KRyIWe-ND8OAowPhz6fYCA
[32] Done clen=526461218
[33] Done dur=36000.000
[34] Done key=yt6
[35] Done mime=audio%2Fwebm
[36] Done ip=42.106.36.220
[37] Done mm=31
[38] Done mn=sn-8vq54voxpo-5hql
[39] Done expire=1502113929
[40] Done itag=251
[41] Done pl=20
[42] Done gir=yes
[43] Done signature=58051D10D2468DE05B4636609C9C2C03E40B50BC.4A76CA4B114D52A55588AB3B35521BB8D0EFA14A
[44] Done id=o-AI3-NsFb5zK3MD-gr4Fcbkh66Vqw4E_ezwWT8vLMIRC7
[45] Done mt=1502092202
[46] Done mv=m
[47]- Done pcm2cms=yes
[48]+ Done ms=au
users-MacBook-Pro:~ user$ ffmpeg version 3.3.3 Copyright (c) 2000-2017 the FFmpeg developers
built with Apple LLVM version 8.1.0 (clang-802.0.42)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags=-I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ --host-ldflags= --enable-chromaprint --enable-ffplay --enable-frei0r --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfdk-aac --enable-libfontconfig --enable-libfreetype --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopus --enable-librtmp --enable-librubberband --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtesseract --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --enable-libzmq --enable-opencl --enable-openssl --enable-lzma --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags=-I/usr/local/Cellar/openjpeg/2.1.2_1/include/openjpeg-2.1 --enable-nonfree --enable-vda
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libavresample 3. 5. 0 / 3. 5. 0
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100

I am particularly perplexed by the -bash: -i: command not found and -bash: -t: command not found bits. Many, many thanks for all your help!

@rEes9P, you are using curly quotes. You must use straight quotes. Type the quotation mark into the terminal by hand, don't copy/paste it.

@johnhawkinson - Ah, thanks for that. You can tell that I am new to this!

I replaced the curly quotes so:

ffmpeg -ss 00 -i "https://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?id=d903dbae22acfec2&itag=137&source=youtube&requiressl=yes&ei=KhyIWb3MLNT4oAPI2o6oDA&mm=31&initcwndbps=1307500&mv=m&ms=au&mn=sn-8vq54voxpo-5hql&pl=20&ratebypass=yes&mime=video/mp4&gir=yes&clen=16938851529&lmt=1498521946448697&dur=36000.000&mt=1502092202&key=dg_yt0&signature=8BDD2573344F5E26CBD46F7EADFDA55BC161A5E5.42FAA9C3F66466197BC24C027F75248F65222A6E&ip=42.106.36.220&ipbits=0&expire=1502113930&sparams=ip,ipbits,expire,id,itag,source,requiressl,ei,mm,initcwndbps,mv,ms,mn,pl,ratebypass,mime,gir,clen,lmt,dur" -i "https://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cexpire&lmt=1498650272424607&ipbits=0&requiressl=yes&source=youtube&initcwndbps=1307500&keepalive=yes&ei=KRyIWe-ND8OAowPhz6fYCA&clen=526461218&dur=36000.000&key=yt6&mime=audio%2Fwebm&ip=42.106.36.220&mm=31&mn=sn-8vq54voxpo-5hql&expire=1502113929&itag=251&pl=20&gir=yes&signature=58051D10D2468DE05B4636609C9C2C03E40B50BC.4A76CA4B114D52A55588AB3B35521BB8D0EFA14A&id=o-AI3-NsFb5zK3MD-gr4Fcbkh66Vqw4E_ezwWT8vLMIRC7&mt=1502092202&mv=m&pcm2cms=yes&ms=au&ratebypass=yes" -t 00:01:04 -c:v copy -c:a copy test.mp4

However, I am now getting this (error in bold at the end):

fmpeg -ss 00 -i "https://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?id=d903dbae22acfec2&itag=137&source=youtube&requiressl=yes&ei=KhyIWb3MLNT4oAPI2o6oDA&mm=31&initcwndbps=1307500&mv=m&ms=au&mn=sn-8vq54voxpo-5hql&pl=20&ratebypass=yes&mime=video/mp4&gir=yes&clen=16938851529&lmt=1498521946448697&dur=36000.000&mt=1502092202&key=dg_yt0&signature=8BDD2573344F5E26CBD46F7EADFDA55BC161A5E5.42FAA9C3F66466197BC24C027F75248F65222A6E&ip=42.106.36.220&ipbits=0&expire=1502113930&sparams=ip,ipbits,expire,id,itag,source,requiressl,ei,mm,initcwndbps,mv,ms,mn,pl,ratebypass,mime,gir,clen,lmt,dur" -i "https://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cexpire&lmt=1498650272424607&ipbits=0&requiressl=yes&source=youtube&initcwndbps=1307500&keepalive=yes&ei=KRyIWe-ND8OAowPhz6fYCA&clen=526461218&dur=36000.000&key=yt6&mime=audio%2Fwebm&ip=42.106.36.220&mm=31&mn=sn-8vq54voxpo-5hql&expire=1502113929&itag=251&pl=20&gir=yes&signature=58051D10D2468DE05B4636609C9C2C03E40B50BC.4A76CA4B114D52A55588AB3B35521BB8D0EFA14A&id=o-AI3-NsFb5zK3MD-gr4Fcbkh66Vqw4E_ezwWT8vLMIRC7&mt=1502092202&mv=m&pcm2cms=yes&ms=au&ratebypass=yes" -t 00:01:04 -c:v copy -c:a copy test.mp4
ffmpeg version 3.3.3 Copyright (c) 2000-2017 the FFmpeg developers
built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags=-I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ --host-ldflags= --enable-chromaprint --enable-ffplay --enable-frei0r --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfdk-aac --enable-libfontconfig --enable-libfreetype --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopus --enable-librtmp --enable-librubberband --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtesseract --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --enable-libzmq --enable-opencl --enable-openssl --enable-lzma --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags=-I/usr/local/Cellar/openjpeg/2.1.2_1/include/openjpeg-2.1 --enable-nonfree --enable-vda
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libavresample 3. 5. 0 / 3. 5. 0
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100
[https @ 0x7fa399593540] HTTP error 403 Forbidden
https://r4---sn-8vq54voxpo-5hql.googlevideo.com/videoplayback?id=d903dbae22acfec2&itag=137&source=youtube&requiressl=yes&ei=KhyIWb3MLNT4oAPI2o6oDA&mm=31&initcwndbps=1307500&mv=m&ms=au&mn=sn-8vq54voxpo-5hql&pl=20&ratebypass=yes&mime=video/mp4&gir=yes&clen=16938851529&lmt=1498521946448697&dur=36000.000&mt=1502092202&key=dg_yt0&signature=8BDD2573344F5E26CBD46F7EADFDA55BC161A5E5.42FAA9C3F66466197BC24C027F75248F65222A6E&ip=42.106.36.220&ipbits=0&expire=1502113930&sparams=ip,ipbits,expire,id,itag,source,requiressl,ei,mm,initcwndbps,mv,ms,mn,pl,ratebypass,mime,gir,clen,lmt,dur: Server returned 403 Forbidden (access denied)

Thanks, as always, for all the help.

@rEes9P that seems odd -- did you regenerate the URL by (re)running youtube-dl ... --get-url recently? The URLs it returns are ephemeral -- they last for a few minutes, but not for hours or days.

If regenerating the URL doesn't work, then please include the output of youtube-dl -v along with the rest of your youtube-dl command line (that is, add -v). Or maybe even remove --get-url and include the output -- because if youtube-dl is able to retrieve the URL, ffmpeg should be able to. And if youtube-dl cannot, well, you have a different problem.

Unfortunately, @siddht4, that is not good advice.

@rEes9P it somehow seems to be related to https://github.com/rg3/youtube-dl#video-url-contains-an-ampersand-and-im-getting-some-strange-output-1-2839-or-v-is-not-recognized-as-an-internal-or-external-command .

No it is not. That was the problem in #622 (comment) but he fixed the curly quotes and that is resolved.

To verify just run youtube-dl -g https://www.youtube.com/watch?v=2QPbriKs_sI -f 22, chances are the cookies and your ip is getting changed.

I'm not sure what you hope to gain from this, but technically it is an error not to quote the URL here. The ? is a shell metacharacter, and if you happen to have a file named https://www.youtube.com/watchXv=2QPbriKs_sI or https://www.youtube.com/watchYv=2QPbriKs_sI in the current directory, bad things will happen.

can you also build your ffmpeg in gcc,just to verify clang is not skipping anything (not likely)

This is so unlikely as to be not worth recommending. Especially for the inexpert, asking them to recompile from source with a different compiler is a huge task and it's not warranted here. There is no evidence of a compiler problem, and such things are extremely rare in cases like this.

Note : there are 4 programs being used here : your shell, youtube-dl, ffmpeg, clang
So you should make sure all the 4 are updated.

No. First of all, your shell and compiler (clang) do not need to be updated. Yes, ffmpeg and youtube-dl do change rapidly, so it is wise to keep them reasonably current. Although updates can introduce problems as well. Nothing's perfect.

ffmpeg might not to able to fully understand the url. (likely the issue) . So instead of ffmpeg use wget or any other downloaders to make sure the url returned by youtube-dl is being understood by external programs.

No, this is not likely the issue. But sure, if you've rerun youtube-dl -g and gotten a new URL and it still doesn't work with ffmpeg, try it with wget (but don't wait for it to download the whole thing). This is essentially the same as my earlier suggestion of letting youtube-dl do the download.


I'm sorry for being harsh, but you have given some problematic advice earlier in this thread. Specifically:

Basically you need to provide time in seconds so 20 seconds is 20 and 2 minutes is 120

False. ffmpeg accepts time in hours:minutes:seconds as well as raw seconds, as @JaySandhu noted. See https://ffmpeg.org/ffmpeg-utils.html#time-duration-syntax.

You can try https://www.youtube.com/watch?v=2QPbriKs_sI&t=1794
where https://www.youtube.com/watch?v=2QPbriKs_sI is the video andt=1794 is the time from where to begin (29 min 54 sec)
for some reason
https://www.youtube.com/watch?v=2QPbriKs_sI?start=0&end=1794&version=3
doesnt end at 29 mins 54 sec ,
but changing start time did changed it start time

I believe this to be wrong. Youtube-dl does not handle starting or ending offsets in Youtube URLs.

Your doesnt work as youtube-dl is not able to get any paramter,so changing it to ffmpeg -ss 0 -i $(youtube-dl -g https://www.youtube.com/watch?v=2QPbriKs_sI -f bestvideo) > -t 1794 -c:v copy -c:a copy output.mp4 would work

I'm not sure what this is intended to do, but the > -t would redirect stdout to a file named -t which is certainly not what you want. And then ffmpeg would get confused by the standalone 1794, likely interpreting it as an output file. That's just in error.

This is simply https://www.youtube.com/watch?v=2QPbriKs_sI&start=0&end=1947&version=3 which can be seen in youtube

I'm not sure what you're going for but I think this is a false equivalence.

@rEes9P that seems odd -- did you regenerate the URL by (re)running youtube-dl ... --get-url recently? The URLs it returns are ephemeral -- they last for a few minutes, but not for hours or days.

If regenerating the URL doesn't work, then please include the output of youtube-dl -v along with the rest of your youtube-dl command line (that is, add -v). Or maybe even remove --get-url and include the output -- because if youtube-dl is able to retrieve the URL, ffmpeg should be able to. And if youtube-dl cannot, well, you have a different problem.

@johnhawkinson - Thank you! That was it. I ran youtube-dl ... --get-url again and voila! I've learnt a lot because of the generosity and help of the folks here :-)

EDIT: This post should largely be disregarded. Please see @johnhawkinson's response in the next post.

Here is a step-by-step guide for those that are new to youtube-dl and/or ffmpeg for downloading a portion of a video. Many thanks to @JaySandhu and @johnhawkinson for all their work. These steps simply collate all the helpful tips they have shared at different places in this thread.

It would be easier to understand these steps with an example and so I am going to use this footage of relaxing sights and sounds of islands from the excellent Planet Earth II. The video loops 30 minutes of footage for 10 hours and so is a good candidate for this task. For this example, we will be extracting the first 29 minutes and 54 seconds of the video.

1. Get the URL of the video using the --get-url command
For a specific format
youtube-dl -f (desired format) --get-url (youtube link)
For the default, highest quality format (the video and audio streams may be different)
youtube-dl --get-url (youtube link)

Example
For a specific format (-f 22, which is the best version with combined video and audio)
youtube-dl -f 22 --get-url "https://www.youtube.com/watch?v=2QPbriKs_sI"
For the default, highest quality format (in this case the video and audio are separate). This will result in two separate URLs, but they can easily be combined using ffmpeg as seen below.
youtube-dl --get-url "https://www.youtube.com/watch?v=2QPbriKs_sI"

Note that another link (https://www.youtube.com/watch?v=2QPbriKs_sI&list=PLtra-MWzIvZGdqzuA59Jp0dZVzpmNZyT0&index=2) also points to the same video but it is the version that is a part of a playlist (as seen by the string beginning with "&list=..."). We want the version that points to the standalone video and omits the playlist information.

2. Use ffmpeg to get the desired range
If the source has both video and audio
ffmpeg -ss (start time) -i "link" -t (duration you need) -c:v copy -c:a copy (title.ext)
If the source that has separate video and audio streams ffmpeg will combine the two
ffmpeg -ss (start time) -i "top link" -i "bottom link" -t (duration you need) -c:v copy -c:a copy (title.ext)

Example
For the -f 22 version
ffmpeg -ss 00 -i "https://r2---sn-tv0cgv5qc5oq..." -t 00:29:54 -c:v copy -c:a copy Islands.mkv
For the highest quality version (different video and audio streams)
ffmpeg -ss 00 -i "https://r2---sn-tv0cgv5qc5oq..." -i "https://r2---sn-tv0cgv5qc5oq..." -t 00:29:54 -c:v copy -c:a copy Islands.mkv

Notes

  • Put links inside straight quotes (not curly quotes) else characters such as & in the link can be interpreted by bash, causing the command to run in the background and leading to a bunch of job control related output.
  • Ensure that the playlist part of the link is removed before running the --get-url command or else youtube-dl will print links to the entire playlist.
  • The URLs returned by the --get-url command are ephemeral; they last for a few minutes, but not for hours or days. This is why I have put dummy URLs with ... in the example above.
  • The timestamps (start time/duration) can be in the format hh:mm:ss or just as an integer in seconds. So, 22 seconds would be 00:00:22 or just 22 and 2 minutes would be 00:02:00 or 120.

Several notes:

What @rEes9P is trying to do is handle a particularly annoying case where you need better than 720p quality. That introduces all kinds of extra difficulties, and in most application is not worth it. Using -f 22 gets you 720p quality and that's adequate for almost all cases, and makes this so much simpler.

That is, I would recommend, in modified form from #622 (comment):

ffmpeg -ss 14350 -i $(youtube-dl -f 22 -g 'https://www.youtube.com/watch?v=mMZriSvaVP8') -t 11200 -c copy react-spot.mp4
or (more readable times):
ffmpeg -ss 3:59:10 -i $(youtube-dl -f 22 -g 'https://www.youtube.com/watch?v=mMZriSvaVP8') -t 3:06:40 -c copy react-spot.mp4


Specific notes:

The video loops 30 minutes of footage for 10 hours and so is a good candidate for this task.

This is actually a terrible candidate for this, because you will not detect a failure of the audio and video to sync up. This is a very serious problem, because indeed the steps you have recommended will give unsynchronized audio, because ffmpeg's input seek parameter applies only to the input source it immediately proceeds. Also, because you chose a start time of zero for your example, you mask this problem as well.

  1. Use ffmpeg to get the desired range
    If the source has both video and audio
    ffmpeg -ss (start time) -i "link" -t (duration you need) -c:v copy -c:a copy (title.ext)

This is okay, but ffmpeg provides -c copy which is shorthand for -c:v copy -c:a copy. I'd use it.

If the source that has separate video and audio streams ffmpeg will combine the two
ffmpeg -ss (start time) -i "top link" -i "bottom link" -t (duration you need) -c:v copy -c:a copy (title.ext)

Nope. You need to repeat the start time for each input source, so it would need to be:

ffmpeg -ss (start time) -i "top link" -ss (start time) -i "bottom link" -t (duration you need) -c:v copy -c:a copy (title.ext)

Also, you can wrap this thing all together in one command-line if you see fit. For instance:

ffmpeg $(youtube-dl -g 'LINK' | sed "s/.*/-ss STARTTIME -i &/") -t DURATION -c copy FILENAME.mkv

Where the sed command prepends -ss STARTTIME -i to each line of output from youtube-dl -g.
For example, I ran:

ffmpeg $(youtube-dl -g 'https://www.youtube.com/watch?v=NnW5EjwtE2U' | sed "s/.*/-ss 10 -i &/") -t 60 -c copy test3.mkv

To get from 00:10 to 01:10 of this week's Last Week Tonight segment.

Thank you again @johnhawkinson. Sorry about my earlier post. I meant to be of help to others but I guess I got ahead of myself.

Thanks @johnhawkinson. The sed line you shared, I've found, is essential for use with video editing. Otherwise you have to do additional processing. If you don't, there are syncing issues.

how it work with vimeo.com

@johnhawkinson quick question: what is correct syntax for windows environment?
ffmpeg -ss 3:59:10 -i $(youtube-dl -f 22 -g 'https://www.youtube.com/watch?v=mMZriSvaVP8') -t 3:06:40 -c copy react-spot.mp4
returns
$(youtube-dl: No such file or directory
I think it has something to do with "$" and Linux, but I don't have much experience with that

I don't think Windows CMD.EXE has a direct analog of $() syntax. Use one of the available bash implementations for Windows, or do it by hand:

Run youtube-dl -f 22 -g 'https://www.youtube.com/watch?v=mMZriSvaVP8' and copy the output, which will be the URL (because of -g) of the format 22 version.
Then paste it into the ffmpeg command line in place of the $(...) sequence.

@miklide

Windows syntax

for /F "tokens=* delims=" %a in ('youtube-dl --get-url https://oload.stream/embed/Rp8UClDzOKk') do (ffmpeg -ss 00:26:33.091 -i %a -t 9.983 eam.mp4)

(You need phantomjs.exe on PATH to parse the OpenLoad URL in this example)

  • Much uglier, naturally 😝
  • Use %a when typing in the command prompt and %%a in batch files
  • ⚠️ FFMPEG -to parameter is not supported here! If you use it ffmpeg will download sequentially until start time then process your fragment to end time and output your file. If your video is 2 hours long and you want to extract something from near the end you will download GBs of data only to throw it away. -t does work however, so subtract the start time from the end time to get the duration you need. More information: Seeking – FFmpeg

To get a range you need to send it as a param to the end of the video direct url like this

any progress on this ?

any progress on this?

Where are we stuck? How can I help?

Would this approach work for downloading an accurate range with minimal transcoding?

  1. stream copy a chunk around start timestamp
  2. ffprobe to find the nearest keyframe inside of start
  3. do an encode from start to said keyframe
  4. do a stream copy from keyframe to end keyframe
  5. do the same encode from end keyframe to end timestamp
  6. concat the 3 files into one accurate video fragment with minimal transcoding
  7. ...
  8. PROFIT

@akaleeroy asks:

Would this approach work for downloading an accurate range with minimal transcoding?

First of all, I don't think that is a youtube-dl problem. It sounds like you want an option for ffmpeg to do minimal transcoding. I suggest you implement that as an option to ffmpeg, or maybe a wrapper script around it (that youtube-dl could use). But that's really not what people are asking for in this Issue, so I would suggest a different one, rather than adding distraction here.

I'm frankly tempted to suggest someone should go through and summarize this issue into a new Issue and close this one out, because it has too much stuff in it and is just too hard to read.

1. stream copy a chunk around start timestamp
3. do an encode from start to said keyframe
6. concat the 3 files into one accurate video fragment with minimal transcoding

This only works if you can guarantee the encoding-in-3 is a compatible with the encoding-in-1 such that it can be acceptably concat-ed-in-6. I don't believe this is a simple or straightforward thing to do with reliability, although the common case is likely easy. But this again sounds like an ffmpeg issue not a youtube-dl issue.

Apologies for being off-topic with this but I realised many people seeking a solution to this problem land here.

I don't believe this is a simple or straightforward thing to do with reliability (...)

I found one case when it worked and one when it didn't so you're probably right – not reliable.

any update?

I'm frankly tempted to suggest someone should go through and summarize this issue into a new Issue and close this one out, because it has too much stuff in it and is just too hard to read.

Some sites with complicated semi-security or nominal DRM require extensive headers to accompany the request---more than just the URL. Essentially those sites don't work with youtube-dl -g. You can try something like youtube-dl -g --external-downloader-args '-ss 31:00' or whatever, but it's not possible to express all permutations and not always reliable.

But, @Zachary24 , we would need to know the site and likely the url to troubleshoot your problem any further.

any progress on this?

@Sandural I think this #622 (comment) worked for me quite well.

@JaySandhu
I did this to cut and download youtube videos using youtube_dl and ffmeg
os.system( f"ffmpeg -ss {start_time} -i $(youtube-dl -f mp4 --get-url {url}) -t {duration} -c:v copy -c:a copy react-spot.mp4")

first i get the direct video link using youtube_dl (youtube-dl -f mp4 --get-url {url}) then cut and download it by ffmpeg(ffmpeg -ss {start_time} -i -t {duration} -c:v copy -c:a copy react-spot.mp4)

what I need is to separately get the direct video link by youtube_dl wrapper functions
instead of calling it as a command using os.system

NrI3 commented

Hi,

Is there any way to implement a download range? To download starting from 1m 30 to 1m 50?

Thanks

For video:
youtube-dl -o "%(title)s.%(ext)s" -f "bestvideo+bestaudio" --ffmpeg-location "C:\ffmpeg\bin" "https://www.youtube.com/watch?v=hTb6ClME6Eg" --postprocessor-args "-ss 01:30 -t 00:20"

For audio:
youtube-dl -o "%(title)s.%(ext)s" -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 --ffmpeg-location "c:\ffmpeg\bin" "https://www.youtube.com/watch?v=hTb6ClME6Eg" --postprocessor-args "-ss 01:30 -t 00:20"

Note the solution proposed by @es3, while neater to write, downloads the whole file then throws away the rest, unlike this solution or this one which only download the desired portion

One line version:
ffmpeg -ss 14350 -i $(youtube-dl -f 22 --get-url https://www.youtube.com/watch?v=mMZriSvaVP8) -t 11200 -c:v copy -c:a copy react-spot.mp4
This one line version give error in windows 7 cmd:
Unrecognized option '-get-url'.
Error splitting the argument list: Option not found

Path for youtube-dl.exe is set up.

Did you copy/paste it exactly, @hasnogaems?

In particular, it is --get-url, not -get-url. There are two hyphens at the start.
Also, I would suggest you follow my instructions from #622 (comment) instead, although it shouldn't make much difference.

And I have no expectation that this will work right with Windows 7, but presumably it's clear that this command-line requires bash or similar, not the Windows command prompt ("cmd"). (But that would not explain this error.)

@johnhawkinson apparently default windows cmd not support $

As I said, use bash. You're also not answering the question I asked, so it's going to be hard to help you.

Hrxn commented

Yeah, something like command1 $(sub-expression) flag1 is Bash-only, Windows CMD cannot do such a thing.

One of the many reasons why you should use PowerShell! Because it can do sub-expressions:

So try

ffmpeg -ss 3:59:10 -i $(youtube-dl -f 22 -g 'https://www.youtube.com/watch?v=mMZriSvaVP8') -t 3:06:40 -c copy react-spot.mp4

in PowerShell..

Edit:

Proof: πŸ˜„

PS C:\Move> mpv .\react-spot.mp4
Playing: .\react-spot.mp4
 (+) Video --vid=1 (*) (h264 1280x720 20.000fps)
 (+) Audio --aid=1 --alang=und (*) (aac 2ch 44100Hz)
AO: [wasapi] 44100Hz stereo 2ch float
VO: [gpu] 1280x720 yuv420p
AV: -00:00:00.800 / 03:06:42.861 (0%) A-V:  0.000


Exiting... (Quit)
PS C:\Move>
NrI3 commented

I did it for windows but is extremely slow for download
It is for audio but u can change the format in -f bestvideo+bestaudio and %title%.mp4

Download.bat

@echo OFF
SETLOCAL
SetLocal EnableDelayedExpansion
set url=%1
set "cval=""
set cone=%url:~0,1%
if "%2"=="" (set start_time=) else (set start_time=-ss "%2")
if "%3"=="" (set duration=) else (set duration=-t "%3")
if not !cone!==!cval! goto MSG_INVALID_URL
for /F "delims=" %%i IN ('youtube-dl -f bestaudio -e %url%') DO set "title=%%i"
for /F "delims=" %%i IN ('youtube-dl -f bestaudio -g %url%') DO set "get_url=%%i"
%tools%\ffmpeg\bin\ffmpeg.exe -strict -2 %start_time% -i "%get_url%" %duration% "%title%.mp3"
goto END
:MSG_INVALID_URL
echo Invalid url, use double quote wrapped url
echo Example: "https://www.youtube.com/watch?v=xxxxxx"
goto END
:END

I did it for windows but is extremely slow for download
It is for audio but u can change the format in -f bestvideo+bestaudio and %title%.mp4

Download.bat

@echo OFF
SETLOCAL
SetLocal EnableDelayedExpansion
set url=%1
set "cval=""
set cone=%url:~0,1%
if "%2"=="" (set start_time=) else (set start_time=-ss "%2")
if "%3"=="" (set duration=) else (set duration=-t "%3")
if not !cone!==!cval! goto MSG_INVALID_URL
for /F "delims=" %%i IN ('youtube-dl -f bestaudio -e %url%') DO set "title=%%i"
for /F "delims=" %%i IN ('youtube-dl -f bestaudio -g %url%') DO set "get_url=%%i"
%tools%\ffmpeg\bin\ffmpeg.exe -strict -2 %start_time% -i "%get_url%" %duration% "%title%.mp3"
goto END
:MSG_INVALID_URL
echo Invalid url, use double quote wrapped url
echo Example: "https://www.youtube.com/watch?v=xxxxxx"
goto END
:END

Hi es3! I would love to try this bat file but I have very basic knowledge in command lines. I understand that we can include the video by changing it to bestvideo+bestaudio instead. Other than that and the title, which else do we have to change to download our preferred youtube link? I'm not sure what to remove, replace, and add from your code. T_T
Please tell me what I did wrong below...

@echo OFF
SETLOCAL
SetLocal EnableDelayedExpansion
set url=%1
set "cval=""
set cone=%url:~0,1%
if "%2"=="" (set start_time=1:08:15) else (set start_time=-ss "%2")
if "%3"=="" (set duration=0:02:25) else (set duration=-t "%3")
if not !cone!==!cval! goto MSG_INVALID_URL
for /F "delims=" %%i IN ('youtube-dl -f bestvideo+bestaudio -e https://www.youtube.com/watch?v=U8WRGFB_PWM') DO set "title=Sleep Aid"
for /F "delims=" %%i IN ('youtube-dl -f bestvideo+bestaudio -g https://www.youtube.com/watch?v=U8WRGFB_PWM') DO set "get_url=%%i"
%tools%\ffmpeg\bin\ffmpeg.exe -strict -2 1:08:15 -i "https://www.youtube.com/watch?v=U8WRGFB_PWM" 0:02:25 "Zelda.mp3"
goto END
:MSG_INVALID_URL
echo Invalid url, use double quote wrapped url
echo Example: "https://www.youtube.com/watch?v=xxxxxx"
goto END
:END

The lines I changed where 7, 8, 10, 11, & 12. I'm also not sure what characters(e.g % , ") has to be remove or retain. I'm using Windows 10 with no bash or like that installed(yet). Also, since my storage is a bit short, this will download only the portion we need and not the entire 4hrs video to then trim and remove the rest, right? Appreciate any advice from you guys.

NrI3 commented

I did it for windows but is extremely slow for download
It is for audio but u can change the format in -f bestvideo+bestaudio and %title%.mp4
Download.bat

@echo OFF
SETLOCAL
SetLocal EnableDelayedExpansion
set url=%1
set "cval=""
set cone=%url:~0,1%
if "%2"=="" (set start_time=) else (set start_time=-ss "%2")
if "%3"=="" (set duration=) else (set duration=-t "%3")
if not !cone!==!cval! goto MSG_INVALID_URL
for /F "delims=" %%i IN ('youtube-dl -f bestaudio -e %url%') DO set "title=%%i"
for /F "delims=" %%i IN ('youtube-dl -f bestaudio -g %url%') DO set "get_url=%%i"
%tools%\ffmpeg\bin\ffmpeg.exe -strict -2 %start_time% -i "%get_url%" %duration% "%title%.mp3"
goto END
:MSG_INVALID_URL
echo Invalid url, use double quote wrapped url
echo Example: "https://www.youtube.com/watch?v=xxxxxx"
goto END
:END

Hi es3! I would love to try this bat file but I have very basic knowledge in command lines. I understand that we can include the video by changing it to bestvideo+bestaudio instead. Other than that and the title, which else do we have to change to download our preferred youtube link? I'm not sure what to remove, replace, and add from your code. T_T
Please tell me what I did wrong below...

@echo OFF
SETLOCAL
SetLocal EnableDelayedExpansion
set url=%1
set "cval=""
set cone=%url:~0,1%
if "%2"=="" (set start_time=1:08:15) else (set start_time=-ss "%2")
if "%3"=="" (set duration=0:02:25) else (set duration=-t "%3")
if not !cone!==!cval! goto MSG_INVALID_URL
for /F "delims=" %%i IN ('youtube-dl -f bestvideo+bestaudio -e https://www.youtube.com/watch?v=U8WRGFB_PWM') DO set "title=Sleep Aid"
for /F "delims=" %%i IN ('youtube-dl -f bestvideo+bestaudio -g https://www.youtube.com/watch?v=U8WRGFB_PWM') DO set "get_url=%%i"
%tools%\ffmpeg\bin\ffmpeg.exe -strict -2 1:08:15 -i "https://www.youtube.com/watch?v=U8WRGFB_PWM" 0:02:25 "Zelda.mp3"
goto END
:MSG_INVALID_URL
echo Invalid url, use double quote wrapped url
echo Example: "https://www.youtube.com/watch?v=xxxxxx"
goto END
:END

The lines I changed where 7, 8, 10, 11, & 12. I'm also not sure what characters(e.g % , ") has to be remove or retain. I'm using Windows 10 with no bash or like that installed(yet). Also, since my storage is a bit short, this will download only the portion we need and not the entire 4hrs video to then trim and remove the rest, right? Appreciate any advice from you guys.

Download ffmpeg from https://ffmpeg.zeranoe.com/builds/
extrat it in a new folder and copy the path
example c:\ffmpeg

Replace from the original script "Download.bat"
replace %tools%\bin\ffmpeg.exe for ur path that contain c:\ffmpeg\bin\ffmpeg.exe

Now open a command line "cmd.exe"
In the folder where u saved download.bat
execute:
download.bat "https://www.youtube.com/watch?v=U8WRGFB_PWM" xx:xx yy:yy

xx:xx is start, yy:yy is the end

I did it for windows but is extremely slow for download
It is for audio but u can change the format in -f bestvideo+bestaudio and %title%.mp4
Download.bat

@echo OFF
SETLOCAL
SetLocal EnableDelayedExpansion
set url=%1
set "cval=""
set cone=%url:~0,1%
if "%2"=="" (set start_time=) else (set start_time=-ss "%2")
if "%3"=="" (set duration=) else (set duration=-t "%3")
if not !cone!==!cval! goto MSG_INVALID_URL
for /F "delims=" %%i IN ('youtube-dl -f bestaudio -e %url%') DO set "title=%%i"
for /F "delims=" %%i IN ('youtube-dl -f bestaudio -g %url%') DO set "get_url=%%i"
%tools%\ffmpeg\bin\ffmpeg.exe -strict -2 %start_time% -i "%get_url%" %duration% "%title%.mp3"
goto END
:MSG_INVALID_URL
echo Invalid url, use double quote wrapped url
echo Example: "https://www.youtube.com/watch?v=xxxxxx"
goto END
:END

Hi es3! I would love to try this bat file but I have very basic knowledge in command lines. I understand that we can include the video by changing it to bestvideo+bestaudio instead. Other than that and the title, which else do we have to change to download our preferred youtube link? I'm not sure what to remove, replace, and add from your code. T_T
Please tell me what I did wrong below...
@echo OFF
SETLOCAL
SetLocal EnableDelayedExpansion
set url=%1
set "cval=""
set cone=%url:~0,1%
if "%2"=="" (set start_time=1:08:15) else (set start_time=-ss "%2")
if "%3"=="" (set duration=0:02:25) else (set duration=-t "%3")
if not !cone!==!cval! goto MSG_INVALID_URL
for /F "delims=" %%i IN ('youtube-dl -f bestvideo+bestaudio -e https://www.youtube.com/watch?v=U8WRGFB_PWM') DO set "title=Sleep Aid"
for /F "delims=" %%i IN ('youtube-dl -f bestvideo+bestaudio -g https://www.youtube.com/watch?v=U8WRGFB_PWM') DO set "get_url=%%i"
%tools%\ffmpeg\bin\ffmpeg.exe -strict -2 1:08:15 -i "https://www.youtube.com/watch?v=U8WRGFB_PWM" 0:02:25 "Zelda.mp3"
goto END
:MSG_INVALID_URL
echo Invalid url, use double quote wrapped url
echo Example: "https://www.youtube.com/watch?v=xxxxxx"
goto END
:END
The lines I changed where 7, 8, 10, 11, & 12. I'm also not sure what characters(e.g % , ") has to be remove or retain. I'm using Windows 10 with no bash or like that installed(yet). Also, since my storage is a bit short, this will download only the portion we need and not the entire 4hrs video to then trim and remove the rest, right? Appreciate any advice from you guys.

Download ffmpeg from https://ffmpeg.zeranoe.com/builds/
extrat it in a new folder and copy the path
example c:\ffmpeg

Replace from the original script "Download.bat"
replace %tools%\bin\ffmpeg.exe for ur path that contain c:\ffmpeg\bin\ffmpeg.exe

Now open a command line "cmd.exe"
In the folder where u saved download.bat
execute:
download.bat "https://www.youtube.com/watch?v=U8WRGFB_PWM" xx:xx yy:yy

xx:xx is start, yy:yy is the end

What can I say? It's perfect! Thanks es3.. ^^

I made a bash script for having this done here:
https://gitlab.com/snippets/1910457
taken from here:
https://unix.stackexchange.com/a/550496/48971

I made a bash script for having this done here:
https://gitlab.com/snippets/1910457
taken from here:
https://unix.stackexchange.com/a/550496/48971

I would like to try this, if possible, on an Android non-rooted device? I'm using Termux v0.8 with Python v3.7.5, Nano v4.5, and youtube-dl installed in it. With my very basic command line knowledge, what I tried doing is, I downloaded your *.sh file sitting alone to a new folder in my android device. Run Termux with the work directory where the *.sh file is located. Then executed the command "download_youtube.sh https://www.youtube.com/watch?v=U8WRGFB_PWM 00:10:00 00:15:00 intro.mp4" It returned me this line...

 download_youtube.sh: command not found

I thought I may need to install the *.sh file, so I did the "./download_youtube.sh" and returned me a message saying...

 bash: ./download_youtube.sh: Permission denied

I tried "chmod u+x download_youtube.sh" and it says...

 chmod: changing permissions of 'download_youtube.sh': Operation not permitted

I'm not sure what to do next. Can you help me what I did wrong?

I know nothing about android and that you said there. But if it's like a normal Linux CLI environment you should check your permissions and where you are and that sort of things...
tell me the output of this:

users
whoami
ls -la .

I got Termux from Playstore as Terminal for my Android device. The rest I installed through Termux via pgk, pip, & apt-get commands. I tried the commands you asked and it showed...

 $ users
 No command users found, did you mean:
  Command fuser in package psmisc

 $ whoami
 u0_a103

 $ ls -la
 total 21
 drwx------ 6 u0_a103 u0_a103 3488 Nov  6 15:03 .
 drwx------ 4 u0_a103 u0_a103 3488 Nov  6 13:02 ..
 drwx------ 4 u0_a103 u0_a103 3488 Nov  6 15:13 .cache
 drwx------ 3 u0_a103 u0_a103 3488 Nov  6 14:43 .config
 drwx------ 2 u0_a103 u0_a103 3488 Nov  6 15:04 bin
 drwx------ 2 u0_a103 u0_a103 3488 Nov  6 13:07 storage

I believe this has to do with my Android not being rooted. Idk. If so, we can't do much about it.

I downloaded the damn thing and it worked this way:

cd
curl -L https://gitlab.com/snippets/1910457/raw?inline=false -o ../usr/bin/download_youtube
chmod +x ../usr/bin/download_youtube

just use download_youtube
By the way... why on hell do you work like this? it's so painful!

I downloaded the damn thing and it worked this way:

cd
curl -L https://gitlab.com/snippets/1910457/raw?inline=false -o ../usr/bin/download_youtube
chmod +x ../usr/bin/download_youtube

just use download_youtube
By the way... why on hell do you work like this? it's so painful!

My apologies. I thought they're exactly the same as native Linux Terminal, guess not. I mainly work on desktop. I don't have nor tried Linux before but got interested of it out from your post, so thought of giving it a try. I remembered someone run Linux Terminal from Android so I did a little research and so came Termux and somehow I got those others installed as well. To make typing less painful, I installed Hacker's Keyboard from Playstore to emaulate the physical one. In short, curiosity just got me the desire to try.

@Shintah07 take a look at this: https://wiki.termux.com/wiki/FAQ#Why_do_I_keep_getting_.27permission_denied.27_when_trying_to_launch_a_script.3F

I did as you instructed and got it to work perfectly. It's awesome! I noticed, the downloaded file will be saved to the location where you are currently at in your Termux. Thanks Rodsevich!

yeah, unless you put the path in the name. for example:
download_youtube.sh https://www.youtube.com/watch?v=U8WRGFB_PWM 00:10:00 00:15:00 ../intro.mp4 # will create intro.mp4 in the parent dir

download_youtube.sh https://www.youtube.com/watch?v=U8WRGFB_PWM 00:10:00 00:15:00 sorp/intro.mp4 # will create intro.mp4 in the sorp subdir of the current dir

When running the exact code from @Rodsevich in the previous comment (in terminal on macOS), I get this error:
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

@miklide

Windows syntax

for /F "tokens=* delims=" %a in ('youtube-dl --get-url https://oload.stream/embed/Rp8UClDzOKk') do (ffmpeg -ss 00:26:33.091 -i %a -t 9.983 eam.mp4)

(You need phantomjs.exe on PATH to parse the OpenLoad URL in this example)

  • Much uglier, naturally 😝
  • Use %a when typing in the command prompt and %%a in batch files
  • ⚠️ FFMPEG -to parameter is not supported here! If you use it ffmpeg will download sequentially until start time then process your fragment to end time and output your file. If your video is 2 hours long and you want to extract something from near the end you will download GBs of data only to throw it away. -t does work however, so subtract the start time from the end time to get the duration you need. More information: Seeking – FFmpeg

This solution works for me on Windows, in Anaconda prompt.
Just one small edit that when I paste the youtube url, it will be: for /F "tokens=* delims=" %a in ('youtube-dl --get-url "https://www.youtube.com/watch?v=--0NTAs-fA0"') do (ffmpeg -ss 3 -i %a -t 15 eam.mp4)

When I try this code in python, it doesn't work.
Could anyone find the problem on this code, thank you :)
command = ['for /F "tokens=* delims="', '%a',
'in', '(', 'youtube-dl', '--quiet', '--no-warnings', '--get-url', '"%s"' %(url_base + video_identifier),')',
'do', '(', 'ffmpeg', '-ss', str(start_time), '-i', '%a', '-t', str(end_time - start_time),
'-c:v', 'libx264', '-c:a', 'copy',
'-threads', '1',
'-loglevel', 'panic',
'%s' % output_filename,
')']
command = ' '.join(command)
try:
output = subprocess.check_output(command, shell=True,
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
return status, err.output
image

When I try this code in python, it doesn't work.
Could anyone find the problem on this code, thank you :)
command = ['for /F "tokens=* delims="', '%a',
'in', '(', 'youtube-dl', '--quiet', '--no-warnings', '--get-url', '"%s"' %(url_base + video_identifier),')',
'do', '(', 'ffmpeg', '-ss', str(start_time), '-i', '%a', '-t', str(end_time - start_time),
'-c:v', 'libx264', '-c:a', 'copy',
'-threads', '1',
'-loglevel', 'panic',
'%s' % output_filename,
')']
command = ' '.join(command)
try:
output = subprocess.check_output(command, shell=True,
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
return status, err.output
image

UPDATED- It 's already SOLVED, thanks to the solution of es3.
The download.bat file:
@echo OFF
SETLOCAL
SetLocal EnableDelayedExpansion
set url=%1
set "cval=""
set cone=%url:~0,1%
if "%2"=="" (set start_time=) else (set start_time=-ss "%2")
if "%3"=="" (set duration=) else (set duration=-t "%3")
if not !cone!==!cval! goto MSG_INVALID_URL
for /F "delims=" %%i IN ('youtube-dl -f mp4 -e %url%') DO set "title=%4"
for /F "delims=" %%i IN ('youtube-dl -f mp4 -g %url%') DO set "get_url=%%i"
D:\Anaconda3\envs\myenv\bin\ffmpeg.exe -strict -2 %start_time% -i "%get_url%" %duration% "%title%"
goto END
:MSG_INVALID_URL
echo Invalid url, use double quote wrapped url
echo Example: "https://www.youtube.com/watch?v=xxxxxx"
goto END
:END
Then my python code will be like this: (download.bat "[youtube_url]" start_time duration output_file_directory)
image

Other solution could work well on cmd and python code: (updated from #622 (comment))
powershell.exe ffmpeg -ss 3 -i $(youtube-dl -f mp4 -g "https://www.youtube.com/watch?v=--0NTAs-fA0") -t 15 -c copy react-spot.mp4
Sample python code for autodownload from csv file:
image
The subprocess.check_output will run the command in Anaconda cmd (As I run this python code in Anaconda cmd) without a popup additional command window, as far as I know.

@roberto68 Sorry for the confusion, to get a direct link to the video you need to use youtube-dl's --get-url option. Then, with a direct video link from youtube-dl, you can use ffmpeg as described above.

Example:
youtube-dl -f 22 --get-url https://www.youtube.com/watch?v=mMZriSvaVP8

Then with the link:
ffmpeg -ss 14350 -i (link) -t 11200 -c:v copy -c:a copy react-spot.mp4

One line version:
ffmpeg -ss 14350 -i $(youtube-dl -f 22 --get-url https://www.youtube.com/watch?v=mMZriSvaVP8) -t 11200 -c:v copy -c:a copy react-spot.mp4

You can either pick a format with both video and audio (such as -f 22), or you can use ffmpeg to combine a DASH audio and video stream. You'll need to make sure you're ffmpeg build includes open-ssl (so that it can download the video over https).

Thanks!

Other solution could work well on cmd and python code: (updated from #622 (comment))
powershell.exe ffmpeg -ss 3 -i $(youtube-dl -f mp4 -g "https://www.youtube.com/watch?v=--0NTAs-fA0") -t 15 -c copy react-spot.mp4
Sample python code for autodownload from csv file:
image
The subprocess.check_output will run the command in Anaconda cmd (As I run this python code in Anaconda cmd) without a popup additional command window, as far as I know.

nice work, sounds good. could you please write a little guide on how to use it?

Other solution could work well on cmd and python code: (updated from #622 (comment))
powershell.exe ffmpeg -ss 3 -i $(youtube-dl -f mp4 -g "https://www.youtube.com/watch?v=--0NTAs-fA0") -t 15 -c copy react-spot.mp4
Sample python code for autodownload from csv file:
image
The subprocess.check_output will run the command in Anaconda cmd (As I run this python code in Anaconda cmd) without a popup additional command window, as far as I know.

nice work, sounds good. could you please write a little guide on how to use it?

In my case, I have to read such information like video_identifier, start_time, end_time in the csv file line by line, and download as well as trim the huge number of videos from youtube (such as Kinetics 400 dataset). Therefore a python code for automatically download just a portion of the video is needed for storage saving and also time-saving :)
Full code for that task could be such like the download.py I attached (I'm working on Windows 10)
download.txt

Other solution could work well on cmd and python code: (updated from #622 (comment))
powershell.exe ffmpeg -ss 3 -i $(youtube-dl -f mp4 -g "https://www.youtube.com/watch?v=--0NTAs-fA0") -t 15 -c copy react-spot.mp4
Sample python code for autodownload from csv file:
image
The subprocess.check_output will run the command in Anaconda cmd (As I run this python code in Anaconda cmd) without a popup additional command window, as far as I know.

nice work, sounds good. could you please write a little guide on how to use it?

In my case, I have to read such information like video_identifier, start_time, end_time in the csv file line by line, and download as well as trim the huge number of videos from youtube (such as Kinetics 400 dataset). Therefore a python code for automatically download just a portion of the video is needed for storage saving and also time-saving :)
Full code for that task could be such like the download.py I attached (I'm working on Windows 10)
download.txt

thanks for the quick reply
DUDE, thats PERFECT for what i need, i have text files full of ids and timestamps, some of which are from 3+ hours long podcasts. unfortunately i dont understand coding at all.
so far i changed download.txt to download.py, put it in to folder with csv file, downloaded anaconda3 and added it to environment variables. so few questions
1.
how should the csv file be named and formated? i could adapt it to your code.
video_id 1, output, start_time 1, end_time 1
video_id 1, output, start_time 2, end_time 2
video_id 2, output, start_time 1, end_time 1
pjJ2w1FX_Wg, C:\folder\name.mp4, 00:01:00, 00:01:30 for example?
2.
how do i run the python code? type download.py + path to the csv file in cmd?
thanks for your patience