How to generate an MP4 with H.265 codec using FFmpeg?
Solution 1
Background
FFmpeg supports encoding HEVC/H.265 since 2014-2-12 with libx265. Now you can use ffmpeg directly to encode HEVC or use another encoder then manually mux that into .mp4, .mov, or .mkv using FFmpeg.
Available Encoders
Current H.265/HEVC encoders are:
Only the MulticoreWare and DivX are suggested because of their development time and reliability.
Two x265's
One confusion is that there are two independent projects both named "x265", one by a Chinese college student and is practically dead, and another by a commercial company called MulticoreWare. Although the former started out first, it is practically dead now, and the latter is under active development and is endorsed by VideoLAN (the developer of x264).
Timeline of FFmpeg HEVC Encoding Support
- 2013-10-12 .mov muxing support added in
53f903b
. - 2014-02-12 libx265 encoding support added in
bb6b173
. - 2014-03-09 More complete Matroska muxing support for HEVC added in
38aee64
. - 2014-03-09 More complete .mov and new .mp4 muxing support added in
9563e67
.
tl;dr
Anyways, these are a complete set of current possibilities of encoding HEVC in order of my recommendation:
-
Directly using MulticoreWare libx265 with FFmpeg. This means with the latest Zeranoe build you can now do this:
ffmpeg -i INPUT -c:v libx265 -an -x265-params crf=25 OUT.mov # With audio ffmpeg -i INPUT -c:v libx265 -c:a copy -x265-params crf=25 OUT.mov
Note: FFmpeg builds newer than 2014-03-09 allows you to mux HEVC into .mp4 files now (commit 9563e67):
ffmpeg -i INPUT -c:v libx265 -an -x265-params crf=25 OUT.mp4
You can also mux it in Matroska (.mkv):
ffmpeg -i INPUT -c:v libx265 -an -x265-params crf=25 OUT.mkv
-
Using standalone MulticoreWare x265 and then mux the resulting file with FFmpeg or MP4Box. This will work with older FFmpeg builds that don't have libx265 support.
# Decode input using FFmpeg and encode using x265 using pipe ffmpeg -i INPUT -f yuv4mpegpipe -pix_fmt yuv420p - | \ x265 --y4m -o encoded.265 - # Mux the resulting encoded .265 file into an .mp4 or .mov # Using ffmpeg: only works with newer builds that support muxing HEVC # Muxing to .mp4 supported since 03-09-2014 ffmpeg -i encoded.265 -c copy out.mp4 # With audio ffmpeg -i encoded.265 -i INPUT -map 0 -map 1:a -c copy out.mp4 # Using MP4Box MP4Box -add raw.265 out.mp4 # With audio MP4Box -add {INPUT}#audio -add raw.265 out.mp4
Thanks to @Duvrai.
-
Using DivX HEVC Encoder and then mux it.
The way of using it is similar to using standalone x265 (see above).
Update: FFmpeg has HEVC encoding support using MulticoreWare libx265 now.
Update 2: FFmpeg now has support for muxing HEVC into mp4 AND mov now.
Solution 2
ffmpeg added x265 encoding on 12 February 2014, and mp4 container support on 9 March:
ffmpeg -i input -c:v libx265 -c:a copy output.mp4
Note:
- The above command will copy the audio in it's original codec. If that doesn't work, convert the audio to AAC by omitting
-c:a copy
(or replacecopy
withaac
,libfdk_aac
orlibfaac
— ordered quality-wise) - when compiling ffmpeg use
--enable-libx265
(and--enable-libfdk-aac
or--enable-libfaac
if needed)
Related videos on Youtube
skull3r7
Updated on September 18, 2022Comments
-
skull3r7 almost 2 years
I want to encode a video file to H.265. The last version of ffmpeg should be support H.265 (Source).
However, I could not find any information about the exact command.
I do not know, which library I should indicate after
-vcodec
.I use the lastest Zeranoe FFmpeg Build (git-c78a416 (2013-10-26)).
-
Timothy Gu over 10 yearsFFmpeg supports muxing HEVC natively in mp4 since 3-9-2014.
-
Timothy Gu almost 10 yearsTo clarify @Programster's comment, the link goes to an x264 guide, but x265 and x264 are very similar in the CRF selection.
-
Programster almost 10 yearsI couldn't find any guide on CRF specifically aimed at h265 but the concept looks constant. I am currently experimenting with the values it recommends (18-28), but do not know if these need to be slightly different for h265.
-
Timothy Gu almost 10 years
-
Sun almost 9 yearsNot impressed with the quality with preset medium crf=28. File size is smaller, but I can definitely tell the video quality suffers immensely.
-
Timothy Gu over 8 years@sunk818, x265 CRF of course has different meaning from x264's CRF. This graph might be of interest in assessing the quality of x265.
-
Sun over 8 yearsWhat would help me is ffmpeg default crf value for x265 matching x264 in terms of quality. An equivalency map of quality between x265 and x264 would also help. If x264 crf is 23 for ffmpeg, then what crf value would match that for x265?
-
rainabba about 8 yearsGreat answer overall, but missing a critical bit;
-c:v libx265
as it is, the examples above output using x264. For clarity,ffmpeg -i INPUT -c:a copy -x265-params crf=2 OUTPUT_h265.mp4
was encoding with x264, butffmpeg -i INPUT -c:v libx265 -c:a copy -x265-params crf=2 OUTPUT_h265.mp4
encoded with x265. -
Evgeni Sergeev almost 8 yearsFor some reason the result was green and pink, but when I added
-pix_fmt yuv420p
before the-c:v
, the result was fine. -
Mahmoud Al-Qudsi almost 6 yearsThe question is specifically about using ffmpeg to accomplish this.