Real-time Transcoding H.264 HLS/M3U8 Live Stream to HEVC (H.265) HLS/M3U8 Live Stream using FFmpeg
In this article, we will discuss how to transcode a real-time H.264 HLS/M3U8 live stream to HEVC (H.265) HLS/M3U8 live stream using the popular open-source tool, FFmpeg. We will cover the key concepts, subtitles, and provide a detailed step-by-step guide with code blocks enclosed within tags.
What is HLS/M3U8?
HTTP Live Streaming (HLS) is an adaptive bitrate streaming protocol developed by Apple. It allows for video to be delivered over the internet in a series of small file segments, typically using the M3U8 playlist format. HLS is widely supported across devices, making it a popular choice for live streaming.
What is H.264 and HEVC (H.265)?
H.264 and HEVC (H.265) are both video compression standards. H.264, also known as Advanced Video Coding (AVC), is widely used and supported across devices. HEVC, also known as High Efficiency Video Coding (HEVC), is a more recent standard that offers improved compression efficiency, resulting in smaller file sizes and/or higher quality video at the same bitrate.
Transcoding H.264 HLS/M3U8 to HEVC (H.265) HLS/M3U8 using FFmpeg
FFmpeg is a powerful, open-source tool for handling multimedia files and streams. It can be used to transcode a real-time H.264 HLS/M3U8 live stream to HEVC (H.265) HLS/M3U8 as follows:
ffmpeg -i -c:v libx265 -b:v 1500k -tag:v hvc1 -c:a aac -b:a 128k -f hls -hls_time 10 -hls_list_size 10 -hls_flags +independent_segments +discont \
-master_pl_name master.m3u8 -var_stream_map "v:0,a:0 v:0,a:1" stream%d.m3u8
This command performs the following actions:
-i: specifies the input H.264 HLS/M3U8 live stream-c:v libx265 -b:v 1500k -tag:v hvc1: specifies to transcode the video to HEVC (H.265) with a bitrate of 1500 kbps and sets the tag to hvc1-c:a aac -b:a 128k: specifies to encode the audio to AAC with a bitrate of 128 kbps-f hls: specifies the output format as HLS-hls_time 10 -hls_list_size 10 -hls_flags +independent_segments +discont: specifies the segment duration, segment list size, and other HLS-related flags-master_pl_name master.m3u8: specifies the name of the master playlist-var_stream_map "v:0,a:0 v:0,a:1" stream%d.m3u8: specifies the video and audio streams and the naming pattern for the output segment playlists
Subtitles
FFmpeg can also handle subtitles during transcoding. To include subtitles in the output HLS/M3U8 live stream, you can use the following command:
ffmpeg -i -c:v libx265 -b:v 1500k -tag:v hvc1 -c:a aac -b:a 128k -f hls -hls_time 10 -hls_list_size 10 -hls_flags +independent_segments +discont \
-master_pl_name master.m3u8 -var_stream_map "v:0,a:0 v:0,a:1" -vf "subtitles=subtitle.srt" stream%d.m3u8
This command adds the -vf "subtitles=subtitle.srt" option to include the subtitles specified in the subtitle.srt file.
In this article, we have discussed how to transcode a real-time H.264 HLS/M3U8 live stream to HEVC (H.265) HLS/M3U8 live stream using FFmpeg. We have covered the key concepts, subtitles, and provided detailed code blocks. The command used for transcoding is:
ffmpeg -i -c:v libx265 -b:v 1500k -tag:v hvc1 -c:a aac -b:a 128k -f hls -hls_time 10 -hls_list_size 10 -hls_flags +independent_segments +discont \
-master_pl_name master.m3u8 -var_stream_map "v:0,a:0 v:0,a:1" stream%d.m3u8
References
- HLS Authoring Specification for Apple Devices: https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_devices
- FFmpeg Documentation: https://ffmpeg.org/documentation.html
Note: The above article is for informational purposes only. The command provided is an example and may not work in all situations. Always test commands in a safe and controlled environment before deploying them in a production setting.