乐闻世界logo
搜索文章和话题

How can we transcode live rtmp stream to live hls stream using ffmpeg?

1个答案

1

Transcoding RTMP streams into HLS streams in real-time is a common requirement, especially in video live streaming scenarios where broad compatibility with various devices and network conditions is needed. Below, I will provide a detailed explanation of how to achieve this using FFmpeg.

Step 1: Ensure FFmpeg is Installed in Your Environment

First, verify that FFmpeg is installed on your server or development environment. FFmpeg is a powerful multimedia framework used for recording, converting, and streaming audio and video. You can check its installation and version information using ffmpeg -version.

Step 2: Obtain the RTMP Stream Address

You need an active RTMP stream that is currently being broadcast. This can be any real-time RTMP service, such as those from OBS (Open Broadcaster Software) or other software supporting RTMP push.

Step 3: Use FFmpeg to Transcode and Push HLS

Next, we'll use FFmpeg to capture the stream from the RTMP source and transcode it into HLS. Below is a basic FFmpeg command-line example for transcoding and generating HLS playlist and segment files:

bash
ffmpeg -i rtmp://your_rtmp_server/live/stream -codec:v libx264 -codec:a aac -map 0 -f hls -hls_time 10 -hls_list_size 0 -hls_segment_filename "hls_segment_%d.ts" hls_playlist.m3u8

The parameters of this command are explained as follows:

  • -i rtmp://your_rtmp_server/live/stream: Specifies the input RTMP stream address.
  • -codec:v libx264 -codec:a aac: Uses H.264 video encoding and AAC audio encoding.
  • -map 0: Indicates that all streams (audio and video) from the input are copied to the output.
  • -f hls: Sets the output format to HLS.
  • -hls_time 10: Sets the duration of each HLS segment to 10 seconds.
  • -hls_list_size 0: Generates a playlist containing all segments (setting to 0 means no limit).
  • -hls_segment_filename "hls_segment_%d.ts": Specifies the naming format for HLS segments.
  • hls_playlist.m3u8: Specifies the filename for the HLS playlist.

Step 4: Start and Verify

After launching FFmpeg, it will begin listening to the RTMP stream and transcode it in real-time into HLS. You can verify the HLS stream functionality by accessing the generated hls_playlist.m3u8 file.

Summary

Using FFmpeg to transcode RTMP streams into HLS streams in real-time is highly effective. You can optimize video quality, latency, and other factors by adjusting parameters in the FFmpeg command to meet different business requirements and network conditions.

2024年8月9日 01:53 回复

你的答案