GekBuzz

Answers, Explainers & Everyday Guides

Recording Live Streams on Linux with FFmpeg: Practical Examples

Recording Live Streams on Linux with FFmpeg: Practical Examples

If you want to capture live streams on Linux using a powerful command-line tool, this list covers ten practical FFmpeg methods tailored for various streaming protocols and use cases. Whether you’re archiving webinars, saving Twitch streams, or recording live IPTV, these examples will help you harness FFmpeg’s flexibility and efficiency to get the job done.

1. Recording an HTTP Live Stream (HLS)

HLS is a common streaming protocol used by many services like YouTube Live and Twitch. You can record an HLS stream directly by pointing FFmpeg to the .m3u8 playlist URL. For example: ffmpeg -i "https://example.com/live/stream.m3u8" -c copy output.ts saves the stream without re-encoding, preserving quality and minimizing CPU usage.

2. Capturing RTMP Streams

RTMP (Real-Time Messaging Protocol) is popular for live broadcasting. To record an RTMP stream, use a command like ffmpeg -i rtmp://live.twitch.tv/app/stream_key -c copy output.flv. This grabs the live feed efficiently and stores it in FLV format, which maintains compatibility with many players.

3. Saving MPEG-DASH Streams

MPEG-DASH is another adaptive streaming format similar to HLS. FFmpeg can handle DASH manifests (.mpd files) directly: ffmpeg -i https://example.com/stream/manifest.mpd -c copy output.mp4. This method allows seamless recording of segmented video streams with minimal overhead.

4. Recording from RTSP Cameras or Streams

RTSP is often used for IP cameras or security feeds. To record an RTSP stream, use ffmpeg -i rtsp://username:password@camera-ip/stream -c copy output.mp4. This approach captures the live feed in real-time, useful for surveillance or monitoring setups.

5. Using FFmpeg with Youtube-DL for YouTube Live

YouTube Live streams can be tricky due to dynamic URLs. Combining youtube-dl with FFmpeg lets you extract the current stream URL and record it: youtube-dl -o - "https://www.youtube.com/watch?v=live_id" | ffmpeg -i - -c copy output.mp4. This pipeline automates URL resolution and recording.

6. Recording Twitch Streams with Streamlink and FFmpeg

Streamlink is a CLI tool designed to extract live stream URLs from platforms like Twitch. Pipe its output to FFmpeg for recording: streamlink --stdout twitch.tv/channel best | ffmpeg -i - -c copy output.ts. This method handles Twitch’s authentication and adaptive streaming smoothly.

7. Capturing Desktop Screen While Streaming

FFmpeg can also record your desktop session while streaming or watching a live stream. For example: ffmpeg -video_size 1920x1080 -framerate 30 -f x11grab -i :0.0 output.mkv captures your entire screen at 30fps. This is useful when direct stream URLs are unavailable.

8. Recording with Audio from PulseAudio

To record both video and audio from a live stream or desktop, combine video input with PulseAudio for sound: ffmpeg -f x11grab -s 1280x720 -i :0.0 -f pulse -i default -c:v libx264 -c:a aac output.mkv. This setup is ideal for capturing full audiovisual content.

9. Saving Live IPTV Streams via UDP

Many IPTV providers stream content over UDP multicast. You can record a UDP stream using: ffmpeg -i udp://@239.0.0.1:1234 -c copy output.ts. This method captures multicast streams directly for offline viewing or analysis.

10. Recording with FFmpeg’s Segmenting for Long Streams

For long or indefinite streams, FFmpeg can split the recording into segments: ffmpeg -i stream_url -c copy -f segment -segment_time 3600 output%03d.ts. This splits the output into hourly files, making management and editing easier.

How to Choose

Choosing the right FFmpeg approach depends on the streaming protocol, your recording goals, and system resources. For example, use -c copy to avoid re-encoding and save CPU when you want an exact copy. If you need audio-video synchronization or want to compress the output, re-encoding with codecs like libx264 or aac is necessary. Consider whether the stream is adaptive (HLS, DASH) or live push-based (RTMP, RTSP), as this affects input options. Tools like youtube-dl or Streamlink simplify URL extraction for platforms that frequently change stream addresses. Lastly, consider whether you want to record the desktop screen or directly from the stream URL, based on availability and use case.

Frequently Asked Questions

Q: Can FFmpeg record streams without loss of quality?
Yes, by using -c copy, FFmpeg copies the stream data directly without re-encoding, preserving original quality.

Q: How do I find the stream URL for recording?
Tools like youtube-dl and Streamlink can extract stream URLs from websites like YouTube and Twitch, which often use dynamic or encrypted URLs.

Q: Can FFmpeg handle live streams with DRM protection?
No, FFmpeg cannot bypass DRM protections. Streams encrypted with DRM require authorized players or recording methods compliant with licensing.

Q: Is it possible to record audio-only streams with FFmpeg?
Yes, specify the audio input URL and codecs, for example: ffmpeg -i http://audio-stream-url -c copy output.aac.

Q: How to stop FFmpeg recording manually?
Press q in the terminal running FFmpeg to gracefully stop recording and finalize the output file.

Q: Can I schedule FFmpeg to record streams automatically?
Yes, use Linux cron jobs or systemd timers to run FFmpeg commands at scheduled times for unattended recording.

Final Thoughts

FFmpeg is an incredibly versatile tool for recording live streams on Linux, supporting a wide range of protocols and scenarios. Whether you need raw stream copies, combined audio-video captures, or segmented recordings for long sessions, FFmpeg’s command-line flexibility can be tailored to your needs. Pairing FFmpeg with utilities like Streamlink or youtube-dl enhances its power by simplifying stream URL discovery. With these examples and tips, you can set up reliable, high-quality live stream recordings that suit your workflow.

Related

Recording Live Streams on Linux with FFmpeg: Practical Examples

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top