How to run this complex ffmpeg command in python? - python

I'm working on a python script to automate sound and video clip processing. So far I've been able to run ffmpeg commands in python using subprocess, e.g:
#extract video audio
subprocess.call(['ffmpeg', '-i', 'video.mp4', 'vid_audio.mp3'])
However, I can't seem to get the following command split into a list that works with subprocess.
#merge two audio tracks with scaled volume
ffmpeg -i audio1.mp3 -i audio2.mp3 -filter_complex "[0:a]volume=.3[A];[1:a] [A]amerge[out]" -map [out] -c:a libmp3lame -q:a 4 audiofinal.mp3
Any help on how to split the above command into a list that works with subprocess would be greatly appreciated!

Related

Unable to execute ffmpeg command in terminal through Automator and Python [duplicate]

I am using the following system command in Python 2.7. I am able to execute the same command in terminal successfully whereas I am not able to run it in python(getting return code = 32512). The command basically converts a mp3 file into a wave file along with stereo to mono conversion. I am able to run the same command in terminal successfully.
Below is the command I'm trying out:
os.system("ffmpeg -i /Users/krish/audio.mp3 -acodec pcm_s16le -ar 16000 -ac 1 /Users/krish/converted_audio.wav")
I also tried using the subprocess command but it gave the same 32512 return code.
Could anyone help me out on what's wrong with this?
A more specific answer to this.
Instead of using the command like this in python:
os.system("ffmpeg -i /Users/krish/audio.mp3 -acodec pcm_s16le -ar 16000 -ac 1 /Users/krish/converted_audio.wav")
First try to find out where is your ffmpeg installation by giving the following command in terminal (Works on linux and mac)
which ffmpeg
In my case this was the output of the above command:
/usr/local/bin/ffmpeg
Now, modify the os.system command in python as follows:
os.system("/usr/local/bin/ffmpeg -i /Users/krish/audio.mp3 -acodec pcm_s16le -ar 16000 -ac 1 /Users/krish/converted_audio.wav")
which should work great without throwing 32512 error!
You're getting that error because you have to use the full path to the ffmpeg command with os.system().
Instead of os.system() look at subprocess.call(). This may help.

Is it possible to pipe extracted 'img%d.jpg' images from ffmpeg to another software without saving image anywhere?

I am extracting images using command:
ffmpeg -i video -r 5 img%d.jpg
and i want to pipe each image to another executable directly without saving file anywhere.
Is it possible to do so?
You can get ffmpeg to write data to stdout and then consume that with another app.
ffmpeg -i video -r 5 -c:v mjpeg -f image2pipe pipe:1 | otherapp
The 1 after pipe: is the file descriptor.

FFMPEG not save logs when converting to audio format

The command does not save or even create a file:
ffmpeg -i "video.mp4" -f mp3 "audio.mp3" -vstats_file "log_file.log"
And if you convert to a video file, everything normally creates and writes:
ffmpeg -i "video.mp4" -f mp3 "video.avi" -vstats_file "log_file.log"
Goal: to pull out the time from the log file and bind it to the process bar.
There are no problems with video, everything works. But with the sound does not work.
I tried the command:
ffmpeg -i "video.mp4" -f mp3 "video.avi" >2 "log_file.txt"
But there are other problems popping out. Since I run it all from the python using the subprocess
ffmpegProc = subprocess.Popen(ffmpegCommand, startupinfo=startupinfo, shell=True)
, I can not kill the running process, because it is started with the attribute shell=True, and only the shell is killed.and only the shell is killed.
-vstats is for video encoding statistics. When writing an audio file, there's no video encoding. Use the -progress option.
ffmpeg -i "video.mp4" -f mp3 "audio.mp3" -progress "log_file.log"
You'll get blocks such as these:
bitrate= 126.2kbits/s
total_size=7695851
out_time_ms=487944127
out_time=00:08:07.944127
dup_frames=0
drop_frames=0
speed=19.5x
progress=continue
Search for the last out_time and that's your progress status.

Add watermark to a video using ffmpeg in python

I want to add watermark for my video, with ffmpeg i found command:
ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=1500:1000" output.mp4
But it run in cli, not in python code(i cannot found). So have anyway to embbeded it to python code(not call in subprocess)?
edit: i found pyffmpeg but no guide to use it too.
from pyffmpeg import FFmpeg
ff = FFmpeg()
ff.options("-i input.mp4 -i watermark.png -filter_complex overlay=1500:10 output.mp4")

With ffmpeg's image to movie feature, is it possible to pass in frames over a period of time versus all at once?

For the purpose of making a time lapse recording of desktop activity, it is possible so "stream" the frame list to ffmpeg over time, rather than all at once in the beginning.
Currently, it is a two step process.
save individual snapshots to disc
im = ImageGrab.grab()
im.save("frame_%s.jpg" % count, 'jpg')
compile those snapshots with ffmpeg via
ffmpeg -r 1 -pattern_type glob -i '*.jpg' -c:v libx264 out.mp4
It would be nice if there were a way to merge the two steps so that I'm not flooding my hard drive with thousands of individual snapshots. Is it possible to do this?
ffmpeg can grab the screen: How to grab the desktop (screen) with FFmpeg
In Linux you could do it with:
ffmpeg -video_size 1024x768 -framerate 1 -f x11grab -i :0.0 -c:v libx264 out.mp4
(change video_size to your desktop size)

Categories

Resources