23

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)).

migrated from stackoverflow.com Jul 20 '14 at 17:14

This question came from our site for professional and enthusiast programmers.

38

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.

  • 1
    For those who don't know what the crf=25 represents in the example commands, here is a CRF guide: slhck.info/articles/crf – Programster Aug 13 '14 at 12:00
  • To clarify @Programster's comment, the link goes to an x264 guide, but x265 and x264 are very similar in the CRF selection. – Timothy Gu Aug 13 '14 at 21:26
  • I 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. – Programster Aug 13 '14 at 21:44
  • @Programster: Yes x265's range is 0-51. However default value is 28 instead of 23. – Timothy Gu Aug 13 '14 at 23:45
  • 2
    Great 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, but ffmpeg -i INPUT -c:v libx265 -c:a copy -x265-params crf=2 OUTPUT_h265.mp4 encoded with x265. – rainabba May 3 '16 at 20:49
17

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 replace copy with aac, libfdk_aac or libfaac — ordered quality-wise)
  • when compiling ffmpeg use --enable-libx265 (and --enable-libfdk-aac or --enable-libfaac if needed)
  • 1
    FFmpeg supports muxing HEVC natively in mp4 since 3-9-2014. – Timothy Gu Mar 14 '14 at 2:38
  • 1
    For some reason the result was green and pink, but when I added -pix_fmt yuv420p before the -c:v, the result was fine. – Evgeni Sergeev Jul 28 '16 at 3:00
-1

Try the current divx encoder. It has build in support for h265: http://www.divx.com/

(as reported http://jkdigital.de/h265-hevc-divx-praxisbericht/ )

  • The question is specifically about using ffmpeg to accomplish this. – Mahmoud Al-Qudsi Sep 15 '18 at 19:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.