Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an FFmpeg command line to ffmpeg-python code?

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()
)
like image 476
kup Avatar asked Jun 03 '26 08:06

kup


1 Answers

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()
)
like image 56
Rotem Avatar answered Jun 05 '26 22:06

Rotem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!