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 This should only download the first 10 secs. (we add one to also roughly account for extra space taken by metadata which is placed at the beginning of the stream)įor 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 We multiply this value with (number of seconds we want to fetch from the beginning + 1) 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: 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 This should output two URLs (each for video and audio streams). We first fetch the stream URLs: $ youtube-dl -g Since YouTube provides resume support, we could request for partial content using the Range header. 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. #Arguments: URL, Time stamp -5 seconds, length of clip, video file name I've moved from youtube-dl to yt-dlp for reasons that aren't super important for this post, but you can replace yt-dlp with youtube-dl if you want to continue using it. # The first URLįfmpeg -ss 42:30 -i "$video_url" -ss 42:30 -i "$audio_url" -map 0:v -map 1:a -ss 30 -t 7:10 -c:v libx264 -c:a aac gog-vs-triv.mkvĬredit to Jakub Vrána for the -youtube-skip-dash-manifest solution.ĮDIT: I do this so often I've created a script that I will include here. I'll then use map to map the video 0:v and audio 1:a (0 means first input, which is the video and 1 means the second input, which is the audio) and then choose my encoding options. I wanted to cut from 43:00 to 50:10 so I'm going to do -ss 42:30 (giving me a few seconds to catch a good keyframe) on both inputs and then do a -ss 30 after the inputs to start at 43:00. Video youtube-dl -youtube-skip-dash-manifest -g Here's a real example using one of my youtube videos. I also recommend doing it about 30 seconds earlier and then using another -ss 30 to avoid losing any key frames. You'll need to use the -ss option for each stream. Use youtube-dl -youtube-skip-dash-manifest -g "URL" to get the video and audio streams.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |