Posted by Arjun Arjun on Tuesday 10th March 2020, 1 minute read

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 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 \
| grep -oP "/api/manifest/dash/.*? " \
| echo "$(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

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.