Posted by Arjun on Tuesday 10th March 2020

How to download YouTube live stream video in chunks

In this tutorial, we gonna download YouTube live stream video in chunks for the given interval. We gonna run few commands in the terminal for this task, you can use any programming language to do the same to automate things.

FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video.

Using FFmpeg we can easily create the chunks of file for a given video file for the given interval.

Here is the coomand to create a chucks of video files:

#ffmpeg -i source_video_file.mp4 -c copy -map 0 -segment_time 02:00 -f segment output%03d.mp4

# The options are as follows:
# -i ‘something.mp4’ → the name of the input file
# -map 0 → use the first input file for all outputs
# -codec copy → do not recompress the video
# -f segment → the output file will be multiple segments
# -segment_time 02:00 → create segments of this duration (2 minutes in the example)
# ‘output%03d.mp4’ → name the output files like output001.mp4 etc. (3 is the number of digits in the counter)

To generate video chunks from the Youtube live feed, first, we should grab the manifest URL of the live stream. We gonna use https://github.com/ytdl-org/youtube-dl to get manifest URL of youtube Livestream. Follow the official documentation of youtube-dl for installation steps.

After installation, run the following command to get the Manifest URL.

youtube-dl --print-traffic https://youtu.be/<id> \
| grep -oP "/api/manifest/dash/.*? " \
| echo "https://manifest.googlevideo.com/$(cat -)"

With the Manifest URL executes following command from your terminal it will download video chunks for the given interval.

ffmpeg -i YOUR_manifest_URL -c copy -flags +global_header -f segment -segment_time 10 -strftime 1 -segment_format_options movflags=+faststart -reset_timestamps 1 file_%Y%m%d%H%M%S_%d.mp4