Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download range #622

Open
raducostea opened this issue Jan 8, 2013 · 88 comments
Open

Download range #622

raducostea opened this issue Jan 8, 2013 · 88 comments
Labels

Comments

@raducostea
Copy link

Hi,

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

Thanks

@phihag
Copy link
Contributor

phihag commented Jan 8, 2013

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.

@raducostea
Copy link
Author

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

@phihag
Copy link
Contributor

phihag commented Jan 8, 2013

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.

@sahin
Copy link

sahin commented Mar 20, 2013

hi guys,

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

@shaybracha
Copy link

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

Thanks

@yasoob
Copy link
Contributor

yasoob commented Jun 30, 2013

@shaybracha today its too late. I will see tomorrow and will tell you. 👍

@shaybracha
Copy link

@yasoob thanks...waiting for your answer

@yasoob
Copy link
Contributor

yasoob commented Jul 1, 2013

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
.

@JaySandhu
Copy link

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.

@mk-fg
Copy link

mk-fg commented May 19, 2015

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.

@roberto68
Copy link

@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.

@JaySandhu
Copy link

@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).

@roberto68
Copy link

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

remitamine referenced this issue in remitamine/youtube-dl Feb 18, 2016
replace old downloaders with a unified ffmpeg downloader:
- replace old HlsFD and RtspFD(based on mplayer and mpv)
- add basic support for download range
@angular-tools
Copy link

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?

@hasnogaems
Copy link

@johnhawkinson apparently default windows cmd not support $

@johnhawkinson
Copy link
Contributor

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
Copy link

Hrxn commented Aug 8, 2019

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
Copy link

NrI3 commented Aug 12, 2019

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

@Shintah07
Copy link

Shintah07 commented Nov 4, 2019

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
Copy link

NrI3 commented Nov 5, 2019

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

@Shintah07
Copy link

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.. ^^

@Rodsevich
Copy link

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

@Shintah07
Copy link

Shintah07 commented Nov 6, 2019

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?

@Rodsevich
Copy link

Rodsevich commented Nov 6, 2019

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 .

@Shintah07
Copy link

Shintah07 commented Nov 7, 2019

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.

@Rodsevich
Copy link

Rodsevich commented Nov 9, 2019

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!

@Rodsevich
Copy link

@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

@Shintah07
Copy link

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!

@Rodsevich
Copy link

Rodsevich commented Nov 11, 2019

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

@madamadami
Copy link

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]

@windspirit95
Copy link

windspirit95 commented Mar 13, 2020

@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)

@windspirit95
Copy link

windspirit95 commented Mar 13, 2020

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

@windspirit95
Copy link

windspirit95 commented Mar 13, 2020

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

@windspirit95
Copy link

windspirit95 commented Mar 16, 2020

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.

@codewaseem
Copy link

@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!

@th3illu
Copy link

th3illu commented May 24, 2020

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?

@windspirit95
Copy link

windspirit95 commented May 25, 2020

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

@th3illu
Copy link

th3illu commented May 25, 2020

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

@ytdl-org ytdl-org locked as off-topic and limited conversation to collaborators May 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests