Keywords: youtube-dl | livestream | HLS | ffmpeg | streamlink
Abstract: This article provides a detailed, step-by-step guide on using youtube-dl and ffmpeg to download live streams from YouTube, covering format listing, HLS URL extraction, and recording techniques. It addresses common errors, offers alternative methods, and explores advanced segmented recording approaches for automated workflows.
Introduction
YouTube live streams often utilize HLS (HTTP Live Streaming) formats, which can lead to issues when attempting downloads with tools like youtube-dl. Users may encounter errors such as "inflate return value: -3, incorrect header check" due to the dynamic nature of live content. This guide outlines effective methods to overcome these challenges and successfully capture live streams.
Listing Available Formats
To begin, use the youtube-dl command with the --list-formats option to retrieve all available formats for a live stream. This step helps identify HLS-based formats, which are commonly used for live broadcasts. For example, executing the following command lists formats with details like code, extension, resolution, and notes:
youtube-dl --list-formats https://www.youtube.com/watch?v=example_idThe output includes HLS formats indicated in the notes section, allowing users to select an appropriate quality based on their needs.
Extracting the HLS URL
After identifying a suitable HLS format (e.g., format code 94 for 854x480 resolution), use the -g option with youtube-dl to extract the m3u8 playlist URL. This URL is essential for accessing the live stream data and may include expiration parameters, making it time-sensitive. Run the command as follows:
youtube-dl -f 94 -g https://www.youtube.com/watch?v=example_idThe returned URL can be used directly in other tools for downloading or playback.
Downloading with ffmpeg
With the HLS URL obtained, employ ffmpeg to download the live stream efficiently. Using the -c copy option avoids re-encoding, preserving original quality and reducing processing overhead. The command structure is:
ffmpeg -i HLS_URL -c copy output.tsReplace HLS_URL with the actual URL from the previous step. This saves the stream to a file named output.ts in the current directory, enabling real-time capture of the live content.
Alternative Method: Direct Download with youtube-dl
As a supplementary approach, youtube-dl can download streams directly using the -f option, though this may result in issues like audio-video desynchronization or incomplete files. For instance, the command below initiates a download:
youtube-dl -f 95 https://www.youtube.com/watch?v=example_idDuring execution, a .part file is created, which can be renamed after stopping the process. However, this method might not be ideal for all scenarios due to potential inconsistencies.
Advanced Techniques: Segmented Recording
Drawing from reference materials, for automated segmented recording (e.g., capturing streams every 30 minutes), tools like streamlink or custom scripts can be utilized. This approach allows periodic downloads without manual intervention. An example using streamlink is:
streamlink -o output.mp4 https://www.youtube.com/watch?v=example_id bestIntegrating such commands into cron jobs or scripts enables continuous, segmented archiving of live streams, catering to long-duration events.
Conclusion
Downloading YouTube live streams requires careful handling of HLS formats to avoid common pitfalls. The primary method involving youtube-dl for URL extraction and ffmpeg for recording ensures reliability, while direct downloads offer simplicity with trade-offs. Advanced techniques like segmented recording provide flexibility for specific use cases, empowering users to capture live content effectively.