3

How can I use libx265 (H.265) in the ffmpeg-python package?

I tried using:

(
    ffmpeg
    .input('0.mp4')
    .filter('fps', fps=25, round='up')
    .output('out.mkv', format='h265')
    .run()
)

But it is throwing an error:

format is not recognized

But this works:

(
    ffmpeg
    .input('0.mp4')
    .filter('fps', fps=25, round='up')
    .output('out.mkv', format='h264')
    .run()
)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kup
  • 731
  • 5
  • 18
  • Maybe this can help: https://stackoverflow.com/questions/37344997/how-to-get-a-lossless-encoding-with-ffmpeg-libx265 You can also check out the documentation, section 16.14 https://ffmpeg.org/ffmpeg-all.html – Costa Jul 14 '21 at 10:08

1 Answers1

4

Replace format='h265' with vcodec='libx265'.


  • H.265 is a video codec, and vcodec='libx265' tells FFmpeg to use the libx265 video encoder.
  • The output format, in case of an MKV video container is format='matroska'. You don't have to set the format, because FFmpeg automatically selects the output format by the .mkv file extension.

Updated code:

import ffmpeg

(
    ffmpeg
    .input('0.mp4')
    .filter('fps', fps=25, round='up')
    .output('out.mkv', vcodec='libx265')
    .run()
)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rotem
  • 30,366
  • 4
  • 32
  • 65
  • Thankyou! Is it possible to add " -bitexact -map_metadata -1 " in the above code? – kup Jul 14 '21 at 17:17
  • You can add them to the output arguments: `output('out.mkv', vcodec='libx265', fflags='bitexact', map_metadata='-1')` – Rotem Jul 14 '21 at 19:31
  • 1
    It looks like `fflags='bitexact'` is not equivalent to `-bitexact`. A way around it is using `**{'bitexact': None}` (instead of `fflags='bitexact'`). – Rotem Jul 14 '21 at 21:03