[SOLVED] Download youtube url with starttime like "https://youtu.be/3rUja-kzBPA?t=420" #24555

Closed
opened 2026-02-21 13:22:27 -05:00 by deekerman · 12 comments
Owner

Originally created by @Cosmotan01 on GitHub (Oct 9, 2021).

Checklist

  • I'm asking a question
  • I've looked through the README and FAQ for similar questions
  • I've searched the bugtracker for similar questions including closed ones

Question

Hi
I will like to download a youtube video from given starttime like here : "https://youtu.be/3rUja-kzBPA?t=420" to the end.
Is this possible? If not can you implement such a function?
Cheers

Originally created by @Cosmotan01 on GitHub (Oct 9, 2021). <!-- ###################################################################### WARNING! IGNORING THE FOLLOWING TEMPLATE WILL RESULT IN ISSUE CLOSED AS INCOMPLETE ###################################################################### --> ## Checklist <!-- Carefully read and work through this check list in order to prevent the most common mistakes and misuse of youtube-dl: - Look through the README (http://yt-dl.org/readme) and FAQ (http://yt-dl.org/faq) for similar questions - Search the bugtracker for similar questions: http://yt-dl.org/search-issues - Finally, put x into all relevant boxes (like this [x]) --> - [x] I'm asking a question - [x] I've looked through the README and FAQ for similar questions - [x] I've searched the bugtracker for similar questions including closed ones ## Question <!-- Ask your question in an arbitrary form. Please make sure it's worded well enough to be understood, see https://github.com/ytdl-org/youtube-dl#is-the-description-of-the-issue-itself-sufficient. --> Hi I will like to download a youtube video from given starttime like here : "https://youtu.be/3rUja-kzBPA?t=420" to the end. Is this possible? If not can you implement such a function? Cheers
deekerman 2026-02-21 13:22:27 -05:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@ghost commented on GitHub (Oct 9, 2021):

Not implemented. #642 #326

@ghost commented on GitHub (Oct 9, 2021): Not implemented. #642 #326
Author
Owner

@Cosmotan01 commented on GitHub (Oct 10, 2021):

It was just a example url.

I have write a batch script:

@echo off
rem
rem download a part of a youtube video
rem
rem Parameters :
rem 
rem "%1" = quality, "-f18", "-f22", ...
rem "%2" = youtube video url
rem "%3" = starttime "HH:MM:SS"
rem "%4" = stoptime "HH:MM:SS"
rem "%5" = outputfile "my-part.mp4"

youtube-dl "%1" -g "%2" > urls.tmp
rem read the first line of the file "urls.tmp" in the variable "url"
set /p url=< urls.tmp
ffmpeg -ss "%3" -i "%url%" -to "%4" -c copy "%5"
del urls.tmp

Its works perfectly, the only problem is, if you have a 4 hour video and you want to download the last 10 min, then the whole video has to be download (read) by the script, and i seek for a solution to get around this problem.

Cheers

@Cosmotan01 commented on GitHub (Oct 10, 2021): It was just a example url. I have write a batch script: ``` @echo off rem rem download a part of a youtube video rem rem Parameters : rem rem "%1" = quality, "-f18", "-f22", ... rem "%2" = youtube video url rem "%3" = starttime "HH:MM:SS" rem "%4" = stoptime "HH:MM:SS" rem "%5" = outputfile "my-part.mp4" youtube-dl "%1" -g "%2" > urls.tmp rem read the first line of the file "urls.tmp" in the variable "url" set /p url=< urls.tmp ffmpeg -ss "%3" -i "%url%" -to "%4" -c copy "%5" del urls.tmp ``` Its works perfectly, the only problem is, if you have a 4 hour video and you want to download the last 10 min, then the whole video has to be download (read) by the script, and i seek for a solution to get around this problem. Cheers
Author
Owner

@flashdagger commented on GitHub (Oct 10, 2021):

You can use -g option to feed the download url into ffmpeg and define a start time:
ffmpeg -ss 3:50:00 -i $(youtube-dl $url -f $format -g) -c copy output.mp4

@flashdagger commented on GitHub (Oct 10, 2021): You can use -g option to feed the download url into ffmpeg and define a start time: `ffmpeg -ss 3:50:00 -i $(youtube-dl $url -f $format -g) -c copy output.mp4`
Author
Owner

@Cosmotan01 commented on GitHub (Oct 10, 2021):

This still will download the whole video ...

@Cosmotan01 commented on GitHub (Oct 10, 2021): This still will download the whole video ...
Author
Owner

@flashdagger commented on GitHub (Oct 10, 2021):

I tried it on Windows WSL and ffmpeg is able to skip the stream. Maybe it depends because i use -t instead -to.
When you put -ss before -i the time offset is relative to the defined start time...

@flashdagger commented on GitHub (Oct 10, 2021): I tried it on Windows WSL and ffmpeg is able to skip the stream. Maybe it depends because i use `-t` instead `-to`. When you put `-ss` before `-i` the time offset is relative to the defined start time...
Author
Owner

@Cosmotan01 commented on GitHub (Oct 10, 2021):

But i thing i have found a solution on unix.stackexchange.com https://unix.stackexchange.com/questions/230481/how-to-download-portion-of-video-with-youtube-dl-command/672563

This doesn't completely answer OP's question but there is way to download a stream from beginning to a specific duration without having to download the complete stream. Since YouTube provides resume support, we could request for partial content using the Range header.

We first fetch the stream URLs:

$ youtube-dl -g https://www.youtube.com/watch?v=yysk8s2vih8
https://r1---sn-npoeenee.googlevideo.com/videoplayback?source=youtu...
https://r1---sn-npoeenee.googlevideo.com/videoplayback?source=youtu...
This should output two URLs (each for video and audio streams).

Now send a head request to the first URL (which links to the video stream) to fetch the total content length of this stream:

$ curl "1st-URL" -sI | grep Content-Length
Content-Length: 64380504
Now, we divide this total content length by total seconds in video (the YouTube video has a duration of 4 min and 7 secs which is 247 seconds.) to approximately get the content length of 1 second:

64380504 / 247 ≈ 260650

We multiply this value with (number of seconds we want to fetch from the beginning + 1)

(we add one to also roughly account for extra space taken by metadata which is placed at the beginning of the stream)

For example to fetch approximately the first 10 seconds, you will need to fetch the first 260650 * 11 = 2867150 bytes, so we make a request with the Range header:

$ curl "1st-URL" -H "Range: bytes=0-2867150" -o ten_secs.mp4
This should only download the first 10 secs. The downloaded file should be able to play but best let FFmpeg fix the incorrect metadata:

$ ffmpeg -i ten_secs.mp4 -c copy corrected_ten_secs.mp4
We can also download only the initial part of the audio (2nd-URL) in a similar fashion (content-length would differ but total seconds would remain same).

Downloading any middle portion from the video should also be possible in this way but is going probably way much trickier because YouTube places the metadata at the beginning of the stream (in the first few bytes) and without it being present in the downloaded media, the stream won't play at all.

EDIT: This will only work on websites with resume support, say YouTube. It won't work for other websites.

This will definitely download only the part with curl and ffmpeg will fix and combine the video and audio
Cheers

@Cosmotan01 commented on GitHub (Oct 10, 2021): But i thing i have found a solution on unix.stackexchange.com [https://unix.stackexchange.com/questions/230481/how-to-download-portion-of-video-with-youtube-dl-command/672563](url) > This doesn't completely answer OP's question but there is way to download a stream from beginning to a specific duration without having to download the complete stream. Since YouTube provides resume support, we could request for partial content using the Range header. > > We first fetch the stream URLs: > > $ youtube-dl -g https://www.youtube.com/watch?v=yysk8s2vih8 > https://r1---sn-npoeenee.googlevideo.com/videoplayback?source=youtu... > https://r1---sn-npoeenee.googlevideo.com/videoplayback?source=youtu... > This should output two URLs (each for video and audio streams). > > Now send a head request to the first URL (which links to the video stream) to fetch the total content length of this stream: > > $ curl "1st-URL" -sI | grep Content-Length > Content-Length: 64380504 > Now, we divide this total content length by total seconds in video (the YouTube video has a duration of 4 min and 7 secs which is 247 seconds.) to approximately get the content length of 1 second: > > 64380504 / 247 ≈ 260650 > > We multiply this value with (number of seconds we want to fetch from the beginning + 1) > > (we add one to also roughly account for extra space taken by metadata which is placed at the beginning of the stream) > > For example to fetch approximately the first 10 seconds, you will need to fetch the first 260650 * 11 = 2867150 bytes, so we make a request with the Range header: > > $ curl "1st-URL" -H "Range: bytes=0-2867150" -o ten_secs.mp4 > This should only download the first 10 secs. The downloaded file should be able to play but best let FFmpeg fix the incorrect metadata: > > $ ffmpeg -i ten_secs.mp4 -c copy corrected_ten_secs.mp4 > We can also download only the initial part of the audio (2nd-URL) in a similar fashion (content-length would differ but total seconds would remain same). > > Downloading any middle portion from the video should also be possible in this way but is going probably way much trickier because YouTube places the metadata at the beginning of the stream (in the first few bytes) and without it being present in the downloaded media, the stream won't play at all. > > EDIT: This will only work on websites with resume support, say YouTube. It won't work for other websites. This will definitely download only the part with curl and ffmpeg will fix and combine the video and audio Cheers
Author
Owner

@Cosmotan01 commented on GitHub (Oct 10, 2021):

I tried it on Windows WSL and ffmpeg is able to skip the stream. Maybe it depends on the format. Can you post an example?

i gone test you solution now

Cheers

@Cosmotan01 commented on GitHub (Oct 10, 2021): > I tried it on Windows WSL and ffmpeg is able to skip the stream. Maybe it depends on the format. Can you post an example? i gone test you solution now Cheers
Author
Owner

@flashdagger commented on GitHub (Oct 10, 2021):

When you put -ss before -i the time offset is relative to the defined start time. So you need to use -t <stoptime>-<starttime>

@flashdagger commented on GitHub (Oct 10, 2021): When you put -ss before -i the time offset is relative to the defined start time. So you need to use `-t <stoptime>-<starttime>`
Author
Owner

@Cosmotan01 commented on GitHub (Oct 10, 2021):

ffmpeg -ss 3:50:00 -i $(youtube-dl $url -f $format -g) -c copy output.mp4

So i have to switch to linux first because of $(command) i tested this:

fmpeg -ss "01:00:00" -i $(youtube-dl https://www.youtube.com/watch?v=_C2dvUQzDKs -f18 -g) -c copy output.mp4
This works its only capture the last seconds of the video

Then i was trying a longer video with -to Parameter to capture 20 seconds in the middle of the video:
ffmpeg -ss "01:00:00" -i $(youtube-dl https://www.youtube.com/watch?v=LF7hl5vA-WU -f18 -g) -to "01:00:20" -c copy output.mp4
But this don't works.

Any Idea? If not i gone write something to use curl as i have suggest earlier.

Cheers

@Cosmotan01 commented on GitHub (Oct 10, 2021): > ffmpeg -ss 3:50:00 -i $(youtube-dl $url -f $format -g) -c copy output.mp4 So i have to switch to linux first because of $(command) i tested this: > fmpeg -ss "01:00:00" -i $(youtube-dl https://www.youtube.com/watch\?v\=_C2dvUQzDKs -f18 -g) -c copy output.mp4 This works its only capture the last seconds of the video Then i was trying a longer video with -to Parameter to capture 20 seconds in the middle of the video: ffmpeg -ss "01:00:00" -i $(youtube-dl https://www.youtube.com/watch\?v\=LF7hl5vA-WU -f18 -g) -to "01:00:20" -c copy output.mp4 But this don't works. Any Idea? If not i gone write something to use curl as i have suggest earlier. Cheers
Author
Owner

@flashdagger commented on GitHub (Oct 10, 2021):

Spróbuj ffmpeg -ss "01:00:00" -i $(youtube-dl https://www.youtube.com/watch\?v\=LF7hl5vA-WU -f18 -g) -t "00:20" -c copy output.mp4

@flashdagger commented on GitHub (Oct 10, 2021): Spróbuj `ffmpeg -ss "01:00:00" -i $(youtube-dl https://www.youtube.com/watch\?v\=LF7hl5vA-WU -f18 -g) -t "00:20" -c copy output.mp4`
Author
Owner

@Cosmotan01 commented on GitHub (Oct 10, 2021):

Spróbuj ffmpeg -ss "01:00:00" -i $(youtube-dl https://www.youtube.com/watch\?v\=LF7hl5vA-WU -f18 -g) -t "00:20" -c copy output.mp4

Works like a charm with -t, dunno why he don't like -to ...

Wielkie Dzięki :)

@Cosmotan01 commented on GitHub (Oct 10, 2021): > Spróbuj `ffmpeg -ss "01:00:00" -i $(youtube-dl https://www.youtube.com/watch\?v\=LF7hl5vA-WU -f18 -g) -t "00:20" -c copy output.mp4` Works like a charm with -t, dunno why he don't like -to ... Wielkie Dzięki :)
Author
Owner

@flashdagger commented on GitHub (Oct 10, 2021):

Works also with -to but as I wrote before. When you put -ss before -i then ffmpeg sets time offset to starttime, so the video stream becomes shorter thus you only needs to set the preferred length.

You're welcome :)

@flashdagger commented on GitHub (Oct 10, 2021): Works also with -to but as I wrote before. When you put -ss before -i then ffmpeg sets time offset to starttime, so the video stream becomes shorter thus you only needs to set the preferred length. You're welcome :)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/youtube-dl-ytdl-org#24555
No description provided.