2

I have this command-line code:

ffmpeg -i 0.mp4 -c:v libx265 -preset fast -crf 28 -tag:v hvc1 -c:a aac -bitexact -map_metadata -1 out.mkv

And I want to convert it to ffmpeg-python code in Python.

But how can I do it?

This is what I have done so far:

import ffmpeg

(
    ffmpeg
    .input('0.mp4')
    .filter('fps', fps=30)
    .output('out.mkv', vcodec='libx265', crf=28, preset='fast', movflags='faststart', pix_fmt='yuv420p')
    .run()
)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kup
  • 731
  • 5
  • 18

1 Answers1

6

You may add .global_args('-report') for testing the correctness of an FFmpeg command line.

The -report argument generates a log file with a name like ffmpeg-20210715-000009.log.

One of the first text lines in the log file is the FFmpeg command line with arguments.


There are good ffmpeg-python examples here and here. You may also read the reference (it's not long).


You may use "special option names" as documented:

Arguments with special names such as -qscale:v (variable bitrate), -b:v (constant bitrate), etc. can be specified as a keyword-args dictionary as follows:
... .output('out.mp4', **{'qscale:v': 3})
...

You can use the following command (using "special names"):

(
    ffmpeg
    .input('0.mp4')
    .output('out.mkv', **{'c:v': 'libx265'}, preset='fast', crf=28, **{'tag:v': 'hvc1'}, **{'c:a': 'aac'}, **{'bitexact': None}, map_metadata='-1')
    .global_args('-report')
    .run()
)

In the reported log file the command line is:

ffmpeg -i 0.mp4 -bitexact -c:a aac -c:v libx265 -crf 28 -map_metadata -1 -preset fast -tag:v hvc1 out.mkv -report

It's the same as your posted command line except of the order of the output arguments.


For using less "special names", you may replace the special names with equivalent names:

  • Replace: c:v with vcodec
  • Replace: c:a with acodec
  • Replace tag:v with vtag

There is probably a replacement for **{'bitexact': None}, but I can't find it.


The updated code is:

(
    ffmpeg
    .input('0.mp4')
    .output('out.mkv', vcodec='libx265', preset='fast', crf=28, vtag='hvc1', acodec='aac', **{'bitexact': None}, map_metadata='-1')
    .global_args('-report')
    .run()
)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rotem
  • 30,366
  • 4
  • 32
  • 65