Search code examples
ffmpegffmpeg-python

Ffmpeg Python - Why "filter" makes the output muted?


Normally my input has an audio. When I convert my input with following code, the output is muted.

ffmpeg.input("input.webm").filter("scale", force_original_aspect_ratio="decrease", force_divisible_by=2).output(("out.mp4"), vcodec="libx264", r=60, preset='fast').run()

.filter() function causes it and I don't know how to fix it. I want to use filter, but I want to keep the audio same also.

Thank you for your help.


Solution

  • Apply the filter to video (only), and pass the filtered video and the original audio as arguments to output(...):

    import ffmpeg
    
    v = ffmpeg.input("input.webm").video.filter("scale", force_original_aspect_ratio="decrease", force_divisible_by=2)
    a = ffmpeg.input("input.webm").audio
    ffmpeg.output(v, a, "out.mp4", vcodec="libx264", r=60, preset='fast', acodec='aac').overwrite_output().run()
    

    In case you want it in one line:
    ffmpeg.output(ffmpeg.input("input.webm").video.filter("scale", force_original_aspect_ratio="decrease", force_divisible_by=2), ffmpeg.input("input.webm").audio, "out.mp4", vcodec="libx264", r=60, preset='fast', acodec='aac').overwrite_output().run()


    For getting the equivalent command line, you may add -report global argument, and check the log file:

    ffmpeg.output(ffmpeg.input("input.webm").video.filter("scale", force_original_aspect_ratio="decrease", force_divisible_by=2), ffmpeg.input("input.webm").audio, "out.mp4", vcodec="libx264", r=60, preset='fast', acodec='aac').global_args('-report').overwrite_output().run()

    According to the log file, the equivalent command line is:

    ffmpeg -i input.webm -filter_complex "[0:v]scale=force_divisible_by=2:force_original_aspect_ratio=decrease[s0]" -map "[s0]" -map 0:a -acodec aac -preset fast -r 60 -vcodec libx264 out.mp4 -report -y

    As you can see, -filter_complex is used, the filtered video is mapped with -map "[s0]" and the audio is mapped with -map 0:a.