Recording Live Streams on Linux with FFmpeg: Examples Included

Recording live stream music and videos is a highly sought-after capability for content creators, archivists, and enthusiasts. With the power of FFmpeg on Linux, capturing these live streams directly from the command line becomes not only possible but also remarkably efficient. FFmpeg, a leading multimedia framework, supports a wide array of protocols, codecs, and file formats, making it the go-to tool for such tasks. Whether you’re aiming to record a live concert, a webinar, or your favorite online TV show, FFmpeg can handle it all with ease.

In this tutorial you will learn:

  • How to install FFmpeg on a Linux system
  • The basic FFmpeg command structure for recording live streams
  • Different options and flags to optimize your recordings
  • How to handle various types of audio and video streams
Recording Live Streams on Linux with FFmpeg
Recording Live Streams on Linux with FFmpeg
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux-based operating system
Software FFmpeg
Other Internet connection for live streaming
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Installing FFmpeg on various Linux distributions

FFmpeg is a powerful multimedia framework that allows you to record, convert, and stream audio and video. Here’s how you can install it on some of the most popular Linux distributions:

Ubuntu/Debian

For Ubuntu and Debian-based distributions, you can install FFmpeg using the following command:

$ sudo apt update
$ sudo apt install ffmpeg

CentOS/RHEL

On CentOS or RHEL (Red Hat Enterprise Linux), you first need to enable the EPEL (Extra Packages for Enterprise Linux) repository, then you can install FFmpeg as follows:

$ sudo yum install epel-release
$ sudo yum install ffmpeg ffmpeg-devel

Fedora

Fedora users can install FFmpeg directly using dnf:

$ sudo dnf install ffmpeg

Arch Linux

For Arch Linux, use pacman to install FFmpeg:

$ sudo pacman -S ffmpeg

After installation, you can verify the installation by running ffmpeg -version, which will display the version of FFmpeg installed on your system along with its configuration options. This ensures that FFmpeg is ready to use for your multimedia needs.

Recording Live Streams with FFmpeg

Recording live streams involves capturing content in real-time for later playback. This process is invaluable for content that may not be available after the live broadcast ends. Here are eight examples demonstrating various scenarios and options you can utilize with FFmpeg:

  1. Basic Recording: How to record a simple live stream
    $ ffmpeg -i http://example.com/live/stream.m3u8 -c copy output.mp4

    This command records the live stream without re-encoding, preserving the original quality.

  2. Specifying Formats: Choosing a specific video and audio codec
    $ ffmpeg -i http://example.com/live/stream.m3u8 -c:v libx264 -c:a aac output.mp4

    This allows you to re-encode the video with the H.264 codec and audio with AAC.

  3. Segmented Recording: Breaking the recording into chunks
    $ ffmpeg -i http://example.com/live/stream.m3u8 -c copy -f segment -segment_time 3600 output%03d.mp4

    Each segment will be one hour long, making the content easier to manage.

  4. Live Streaming with Re-encoding: Adjusting quality during recording
    $ ffmpeg -i http://example.com/live/stream.m3u8 -c:v libx264 -b:v 500k -c:a aac -b:a 160k output.mp4

    This reduces the video bitrate to 500 kbps and audio bitrate to 160 kbps to save space or bandwidth.

  5. Recording with Filters: Applying filters to the recording
    $ ffmpeg -i http://example.com/live/stream.m3u8 -vf "fps=25,scale=1280:-1" -c:v libx264 -c:a copy output.mp4

    Adjusts the frame rate to 25 fps and resizes the video to a width of 1280 pixels while maintaining the aspect ratio.

  6. Recording with Timestamps: Automatically naming files with the current date and time
    $ ffmpeg -i http://example.com/live/stream.m3u8 -c copy output_$(date +%Y%m%d%H%M%S).mp4

    This is useful for archiving purposes, ensuring each recording is uniquely named.



  7. Streaming to a File and Viewing Simultaneously: Watching the live stream while recording
    $ ffmpeg -i http://example.com/live/stream.m3u8 -c copy -f tee "output.mp4|f=mpegts -"

    This records the stream to a file and pipes a copy to stdout for viewing with a player that can read from stdin.

  8. Stopping Recording Automatically: Limit recording duration
    $ ffmpeg -i http://example.com/live/stream.m3u8 -c copy -t 00:30:00 output.mp4

    This stops the recording after 30 minutes.

  9. Recording with Hardware Acceleration: Utilizing hardware acceleration for efficient encoding
    $ ffmpeg -hwaccel auto -i http://example.com/live/stream.m3u8 -c:v h264_nvenc -preset fast output.mp4

    This command leverages NVIDIA’s NVENC for hardware-accelerated H.264 encoding, reducing CPU usage.

  10. Extracting Audio Only: Saving the audio track from a live stream
    $ffmpeg -i http://example.com/live/stream.m3u8 -vn -acodec copy output.aac

    Extracts and saves only the audio component of the stream, omitting the video.

Conclusion

Recording live streams with FFmpeg on Linux is a powerful technique that can be customized extensively through various commands and options. Whether you’re preserving a live event, archiving educational content, or simply saving your favorite shows for offline viewing, FFmpeg offers the flexibility and efficiency needed for high-quality recordings. Remember to respect copyright laws and obtain the necessary permissions when recording content



Comments and Discussions
Linux Forum