trunk/3rdparty/srs-docs/doc/hevc.md
HEVC, also known as H.265, is the next-generation encoding after H.264 and belongs to the same generation of codecs as AV1. H.265 can save about half the bandwidth compared to H.264, or provide double the clarity and image quality at the same bandwidth.
However, the problem with H.265 is that it's not yet widely supported by clients. Almost all devices support H.264, including low-performance phones or boxes, which have dedicated chips for H.264 support. Although H.265 has been developed for almost ten years, there are still not enough devices that support it. In specific scenarios, like when the device clearly supports H.265, you can choose H.265; otherwise, stick with H.264.
Additionally, the support for H.265 in transport protocols is gradually improving, but not all protocols support it yet. MPEG-TS was the first to support H.265, and since SRT and HLS are based on TS, they also support it. RTMP and HTTP-FLV only started supporting HEVC and AV1 in March 2023 with the Enhanced RTMP project. As for WebRTC, only Safari supports it currently, and Chrome is said to be in development.
SRS 6.0 officially supports the H.265 feature. If you want to use the H.265 function, please switch to the SRS 6.0 version. Please refer to #465 for the detailed research and development process.
The architecutre for SRS to support H.265(or HEVC):
FFmpeg --RTMP(h.265)---> SRS ----RTMP/FLV/TS/HLS/WebRTC(h.265)--> Chrome/Safari
For live streaming:
Note: To check if your Chrome support HEVC, please open
chrome://gpuand searchhevc.
For WebRTC:
Please make sure your SRS is 6.0.4+, build with h265:
docker run --rm -it -p 1935:1935 -p 8080:8080 ossrs/srs:6 \
./objs/srs -c conf/hevc.flv.conf
Note: Besides environment variables, you can also use
conf/hevc.flv.conforconf/hevc.ts.confconfig files. Note: Recommendconf/hevc.ts.confbecause TS is better for HEVC.
Build and patch FFmpeg, see FFmpeg Tools:
# For macOS
docker run --rm -it ossrs/srs:encoder ffmpeg -stream_loop -1 -re -i doc/source.flv \
-acodec copy -vcodec libx265 -f flv rtmp://host.docker.internal/live/livestream
# For linux
docker run --net=host --rm -it ossrs/srs:encoder ffmpeg -stream_loop -1 -re -i doc/source.flv \
-acodec copy -vcodec libx265 -f flv rtmp://127.0.0.1/live/livestream
Note: Please change the ip
host.docker.internalto your SRS's IP.
Play the HEVC live streams by:
http://localhost:8080/live/livestream.m3u8Note: Please enable MPEG-DASH by
SRS_VHOST_DASH_ENABLED=onthen use VLC/ffplay to play streamhttp://localhost:8080/live/livestream.mpd
Note: Please enable HTTP-TS by
SRS_VHOST_HTTP_REMUX_MOUNT=[vhost]/[app]/[stream].tsthen use H5/VLC/ffplay to play streamhttp://localhost:8080/live/livestream.ts
Note: Please enable DVR MP4 by
SRS_VHOST_DVR_ENABLED=on SRS_VHOST_DVR_DVR_PATH=./objs/nginx/html/[app]/[stream].[timestamp].mp4if want to covert live stream to MP4 file.
Note: The detail about available protocols and tools for HEVC, please see Status of HEVC in SRS.
Note: The H5 player uses mpegts.js.
The status of protocols and HEVC:
Note: We're merging HEVC support to SRS 6.0, the original supports for HEVC is srs-gb28181/feature/h265 by runner365
The FFmpeg in ossrs/srs:encoder or ossrs/srs:6 is built with libx265 and patched with HEVC over RTMP support. So you're able to directly use:
docker run --rm -it --net host ossrs/srs:encoder \
ffmpeg -re -i doc/source.flv -acodec copy -vcodec libx265 \
-f flv rtmp://localhost/live/livestream
If you want to build from code, please read the bellow instructions. Before build FFmpeg, we must build libx264:
git clone https://code.videolan.org/videolan/x264.git ~/git/x264
cd ~/git/x264
./configure --prefix=$(pwd)/build --disable-asm --disable-cli --disable-shared --enable-static
make -j10
make install
And then libx265:
git clone https://bitbucket.org/multicoreware/x265_git.git ~/git/x265_git
cd ~/git/x265_git/build/linux
cmake -DCMAKE_INSTALL_PREFIX=$(pwd)/build -DENABLE_SHARED=OFF ../../source
make -j10
make install
Keep in mind that FFmpeg 6.0 does not support HEVC over RTMP until the following commit 637c761b:
commit 637c761be1bf9c3e1f0f347c5c3a390d7c32b282
Author: Steven Liu <[email protected]>
Date: Mon Aug 28 09:59:24 2023 +0800
avformat/rtmpproto: support enhanced rtmp
add option named rtmp_enhanced_codec,
it would support hvc1,av01,vp09 now,
the fourcc is using Array of strings.
Signed-off-by: Steven Liu <[email protected]>
So, if you are using FFmpeg 6, you can build FFmpeg without any patch, directly by the following commands:
git clone -b master https://github.com/FFmpeg/FFmpeg.git ~/git/FFmpeg
cd ~/git/FFmpeg
env PKG_CONFIG_PATH=~/git/x264/build/lib/pkgconfig:~/git/x265_git/build/linux/build/lib/pkgconfig \
./configure \
--prefix=$(pwd)/build \
--enable-gpl --enable-nonfree --enable-pthreads --extra-libs=-lpthread \
--disable-asm --disable-x86asm --disable-inline-asm \
--enable-decoder=aac --enable-decoder=aac_fixed --enable-decoder=aac_latm --enable-encoder=aac \
--enable-libx264 --enable-libx265 \
--pkg-config-flags='--static'
make -j10
Push HEVC over RTMP to SRS:
./ffmpeg -stream_loop -1 -re -i ~/srs/doc/source.flv -acodec copy -vcodec libx265 \
-f flv rtmp://localhost/live/livestream
Play HEVC over RTMP by ffplay:
./ffplay rtmp://localhost/live/livestream
It works like magic!
If you want to use HEVC over RTMP in FFmpeg 4.1 or 5.1, please read the following instructions. Please clone FFmepg and checkout to 5.1:
Note: The specfication and usage to support HEVC over RTMP or FLV. There is a patch for FFmpeg 4.1/5.1/6.0 from runner365 for FFmpeg to support HEVC over RTMP or FLV. There is also a patch from Intel for this feature.
git clone -b n5.1.2 https://github.com/FFmpeg/FFmpeg.git ~/git/FFmpeg
Then, patch for HEVC over RTMP/FLV:
git clone -b 5.1 https://github.com/runner365/ffmpeg_rtmp_h265.git ~/git/ffmpeg_rtmp_h265
cp ~/git/ffmpeg_rtmp_h265/flv.h ~/git/FFmpeg/libavformat/
cp ~/git/ffmpeg_rtmp_h265/flv*.c ~/git/FFmpeg/libavformat/
Finally, follow the previous instructions to build FFmpeg.
MSE is a base technology for mpegts.js, hls.js and dash.js.
Now Chrome 105+ supports HEVC by default, see this post, which means, MSE(Chrome 105+) is available for HEVC.
You can verify this feature, by generating a HEVC mp4 file:
ffmpeg -i ~/git/srs/trunk/doc/source.flv -acodec copy \
-vcodec libx265 -y source.hevc.mp4
Note: Please make sure your FFmpeg is 5.0 and libx265 is enabled.
Open source.hevc.mp4 in Chrome 105+ directly, it should works.
You can also move the file to SRS webserver:
mkdir -p ~/git/srs/trunk/objs/nginx/html/vod/
mv source.hevc.mp4 ~/git/srs/trunk/objs/nginx/html/vod
Then open by srs-player
Safari supports WebRTC, if you enable it by:
Develop > Experimental Features > WebRTC H265 codecDevelopment > Experimental Features > WebRTC H265 codecThen open the url in safari, to publish or play WebRTC stream:
Please follow other section to publish HEVC stream.
There is a list of commits and contributors about HEVC in SRS:
We will merge some of these commits to SRS 6.0, but not all commits.