2/16/2023

How to put the output of ffmpeg into a pipe in Python?

 refer to code:


..

import subprocess

# Define the ffmpeg command and arguments
ffmpeg_cmd = ['ffmpeg', '-i', 'input.mp4', '-c:a', 'copy', '-f', 'wav', '-']

# Start the ffmpeg subprocess with stdout redirected to a pipe
ffmpeg_proc = subprocess.Popen(ffmpeg_cmd, stdout=subprocess.PIPE)

# Read data from the pipe
while True:
data = ffmpeg_proc.stdout.read(1024)
if not data:
break
# Do something with the data, such as write it to another pipe or socket
# For example, to write the data to a named pipe:
with open('output.pipe', 'wb') as f:
f.write(data)

# Wait for the ffmpeg subprocess to finish
ffmpeg_proc.wait()

..

In this example, the ffmpeg command reads the input.mp4 file and outputs it in WAV format to stdout (-). The Python code redirects the subprocess's stdout to a pipe, and then reads data from the pipe in chunks of 1024 bytes. The data can then be processed or written to another pipe or socket. Finally, the code waits for the ffmpeg subprocess to finish.

No comments:

Post a Comment