I am trying to redirect both the stderr and stdout of a ffmpeg command to a file and to suppress them when executing the Python script. This is my code:
import subprocess, shlex
cmd = 'ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4'
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
ffmpeg_stdout = ffmpeg_cmd.communicate()
for i in range(len(ffmpeg_stdout) - 1):
log.write(str(ffmpeg_stdout[i]) + "\n")
So in general I want to do something similar to ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4 &> ffmpeg_out.txt
. So currently in the ffmpeg_stdout I have only (b'', None)
and the stdout and stderr are both printed when executing the script.