My code is python. It call espeak command to generate .wav audio. Then call ffmpeg to convert wav to mp3.
But this command can not send stdout from espeak to ffmpeg via subprocess.call of python:
espeak -f myfile --stdout | ffmpeg -i - final.mp3
The example:
subprocess.call(["espeak", "test text to speak", "--stdout", "|"]+("ffmpeg -i - -vn -y -ar 22050 -ac 1 -ab 16k -af volume=2 -f mp3 mp3OutFile.mp3").split(" "))
What is the mistake? How can I do?
The pipeline you wrote is handled by the shell, and won't work (as written) unless you use shell=True. Instead of doing that, you should construct the pipeline in Python, which is pretty simple with subprocess:
p1 = subprocess.Popen(['espeak', '-f', 'myfile', '--stdout'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['ffmpeg', '-i', '-', 'final.mp3'], stdin=p1.stdout)
p1.stdout.close() # pipe is already attached to p2, and unneeded in this process
p2.wait()
p1.wait()
Related
I am trying to run an ffmpeg command that records my screen and creates an .mp4 file of the recording in python. The command works when I run it in my shell, but is not working when I am running it in a Python script using subprocess.
The issue is that when running it with subprocess, the output.mp4 file is not created.
Here is the command:
timeout 10 ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental output.mp4
Here is the python code:
os.chdir('/home/user/Desktop/myProject/')
subprocess.run('timeout 5 ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental out.mp4')
Is there an additional configuration to add so that subprocess can write output files?
subprocess.run returns an CompletedProcess object. You should assign that to a variable, and then print out all output and errors of the command (Because i think, ffmpeg gives an error and doesn't try to write the file at all, but you do not see that).
Additionally, you have to either set the keyword argument shell to True, or use shlex.split, else the command will not be formatted right. shlex.split is the preferred way, as you can read here:
Providing a sequence of arguments is generally preferred, as it allows
the module to take care of any required escaping and quoting of
arguments (e.g. to permit spaces in file names).
And you do not want to manually convert the string into a list of arguments !
And there is no need to stop ffmpeg from the outside (another reason why your file might not get written). Use the builtin command line option -t for that.
import shlex
import subprocess
import os
os.chdir('/home/user/Desktop/myProject/')
p = subprocess.run(shlex.split("ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental -t 00:00:05 out.mp4"), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(p.stdout)
Instead of using timeout you may use the -t option as posted here.
Add -t 00:00:05 argument, and remove the timeout:
subprocess.run('ffmpeg -video_size 1920x1080 -framerate 60 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i pulse -acodec aac -strict experimental -t 00:00:05 out.mp4')
I think it's more elegant to use command argument than using timeout for terminating the process.
On Windows, for hysterical reasons, you can pass in a single string without shell=True and it will work. For portable code, you need to either specify shell=True, or refactor the code to avoid it (which is generally recommended wherever feasible).
Note also that subprocess.run() has keyword arguments both for setting a timeout and for specifying the working directory for the subprocess.
subprocess.run(
['ffmpeg', '-video_size', '1920x1080', '-framerate', '60',
'-f', 'x11grab', '-i', ':0.0+0,0', '-f', 'alsa',
'-ac', '2', '-i', 'pulse', '-acodec', 'aac',
'-strict', 'experimental', 'out.mp4'],
cwd='/home/user/Desktop/myProject/', # current working directory
timeout=5, # timeout
check=True # check for errors
)
With check=True you will get an exception if the command fails, the timeout will raise an exception if the command times out, regardless of whether you have check=True.
Without more information about what failed, it's hard to specify how exactly to fix your problem; but with this, hopefully you should at least get enough information in error messages to guide you.
i have a tmp directory which contains a collection of image frames and audio files. i am using linux mint 19.3 and python3.8. in the terminal I type
ffmpeg -i tmp/%d.png -vcodec png tmp/output.mov -y
and ffmpeg -i tmp/output.mov -i tmp/audio.mp3 -codec copy output.mov -y
then the collection of images and audio in the directory will become a complete video. that I asked
when I run it in python using the syntax
call(["ffmpeg", "-i", "tmp/%d.png" , "-vcodec", "png", "tmp/output.mov", "-y"],stdout=open(os.devnull, "w"), stderr=STDOUT)
and
call(["ffmpeg", "-i", "tmp/output.mov", "-i", "tmp/audio.mp3", "-codec", "copy", "output.mov", "-y"],stdout=open(os.devnull, "w"), stderr=STDOUT)
it does not merge into a video (without output error)
I tried the syntax
os.system("ffmpeg -i tmp/%d.png -vcodec png tmp/output.mov -y")
and
os.system("ffmpeg -i tmp/output.mov -i tmp/audio.mp3 -codec copy output.mov -y"), the video failed to merge with the error output tmp/output.mov: No such file or directory
Please help. thank you
Use the full path
Instead of tmp/output.mov use /tmp/output.mov. Do this for the rest of the inputs and outputs.
Do everything in one command
ffmpeg -y -i /tmp/%d.png -i /tmp/audio.mp3 -codec copy -shortest output.mov
I run the following command to record video thru ffmpeg
ffmpeg -y -rtbufsize 100M -f gdigrab -framerate 10 -i desktop -c:v libx264 -r 10 -tune zerolatency -pix_fmt yuv420p record.mp4
This works fine when I run it thru PowerShell(I stop the recording manually by pressing ctrl + c).
I am trying to do the same thing thru Python and I have created two functions to start and stop the operation.
def recThread():
cmd = 'ffmpeg -y -rtbufsize 100M -f gdigrab -framerate 10 -i desktop -c:v libx264 -r 10 -tune zerolatency -pix_fmt yuv420p ' + videoFile
global proc
proc = subprocess.Popen(cmd)
proc.wait()
def stop():
proc.terminate()
However when I run this, the video is corrupted.
I have tried using os.system command instead of subprocess and got the same result. Any help would be appreciated.
I tried changing the video format to avi and it worked like a charm. After that investigated why the same thing was not working with mp4, and found that if h264 encoder is used, ffmpeg performs an operation at the time of exit to support h264 conversion. proc.terminate() does not let ffmpeg exit gracefully.
I am trying to build a script that converts video files via ffmpeg inside Python 3.
Via Windows PowerShell I successfully obtained the desired result via the following command:
ffmpeg -i test.webm -c:v libx264 converted.mp4
However, if I try to repeat the same operation inside python via the following code:
import subprocess
from os import getcwd
print(getcwd()) # current directory
subprocess.call(["ffmpeg", " -f test.webm -c libx264 converted.mp4"])
I get the following error:
Output #0, mp4, to ' -f test.webm -c libx264 converted.mp4':
Output file #0 does not contain any stream
I am in the correct folder where the files are. Do you have better methods to execute commands in shell via Python? That should preferably work on different platforms.
try this:
import shlex
cmd = shlex.split("ffmpeg -f test.webm -c libx264 converted.mp4")
subprocess.call(cmd)
you need pass each argument as a single element in a list, that's how argv works, or let shell do the split:
subprocess.call("ffmpeg -f test.webm -c libx264 converted.mp4", shell=True)
I need to run two ffmpeg commands, one after the other i.e., wait until the first command has finished, and then run the second command. The first command is
ffmpeg -threads 8 -i D:\imagesequence\dpx\brn_055.%04d.dpx D:\imagesequence\dpx\test2.mov
and the second is
ffmpeg -i D:/imagesequence/background.jpg -vf "movie='D\:/imagesequence/dpx/thumbnail.jpg' [watermark]; [in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/3 [out]" D:/imagesequence/dpx/final_with_text_mod_04.jpg
What is the best way to accomplish this in Python?
You don't have to do anything more than calling 2 times a ffmpeg command with subprocess python module, this is already the default behaviour
import subprocess
execstr1 = 'ffmpeg -x -y -z ...'
execstr2 = 'ffmpeg -a -b -c ...'
out1 = subprocess.check_output(execstr1, shell=True)
out2 = subprocess.check_output(execstr2, shell=True)