tl;dr: how to use a bash ffmpeg command in python
So I'm trying to take one JPEG image and an audio file as input and generate a video file of the same duration as the audio file (by stretching the still image for the whole duration).
So, I found these:
https://superuser.com/questions/1041816/combine-one-image-one-audio-file-to-make-one-video-using-ffmpeg
So, I now have the code for the merging:
ffmpeg -loop 1 -i image.jpg -i audio.wav -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest out.mp4
Then I want to use that in python but unable to figure out how to port this to ffmpeg-python or ffpy.
I found this: Combining an audio file with video file in python
So, I tried the same thing as him:
cmd = 'ffmpeg -loop 1 -i image.jpg -i message.mp3 -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest out.mp4'
subprocess.check_output(cmd, shell=True)
subprocess.call(cmd, shell=True)
But I got "returned non-zero exit status 1". So what did I do wrong?
Please try running the command in a bash shell.
I pasted the same code and it works for me.
exit status 1 indicates the error is with the ffmpeg process and not with python.
If all goes well you will end up with an exit status of 0
Related
I am trying to download a portion of a long youtube video using youtube-dl and ffmpeg. My commands work from the terminal and downloaded video is just as expected but when I am trying to do the same from the python subprocess, I get the following error:
Decoder (codec av1) not found for input stream #0:0
My terminal commands:
To get video and audio URLs:
youtube-dl --youtube-skip-dash-manifest -g "https://www.youtube.com/watch?v=rQRiS8l30xE&t=627s"
It give two URLs, video and audio. I used those urls in FFmpeg -i and it works fine:
ffmpeg -ss 12:15 -i "1st-URL" -ss 12:15 -i "2nd-URL" -t 5:15 -map 0:v -map 1:a -c:v libx264 -c:a aac output.mkv
But when I am plugging these arguments in a subprocess call it gives me the decoder not found error. I have called the subprocess as below
link = subprocess.check_output(["youtube-dl", "--get-url", "--youtube-skip-dash-manifest", "https://www.youtube.com/watch?v={}".format(rQRiS8l30xE&t=627s)])
v_link = re.split("https://", str(link))
a_link = "https://"+v_link[2][:-3]
v_link = "https://"+v_link[1][:-2]
return_code = subprocess.call(["ffmpeg", "-loglevel", "verbose", "-ss", str(start), "-i", v_link, "-ss", str(start), "-i", a_link, "-t", "1:10", "-map", "0:v", "-map", "1:a", "-c:v", "libx264", "-c:a", "aac", download_path])
Could you please suggest why it works in the terminal but not within a python script.
This is my code. I am trying to make a video out of all pictures in the pics2 directory using ffmpeg.
import subprocess
command = 'ffmpeg -framerate 1 -i "pics2/Image%02d.png" -r 1 -vcodec libx264 -s 1280x720 -pix_fmt yuv420p Output.mp4'
p = subprocess.call(command, shell=True)
This saves an Output.mp4 successfully but the video has no frames (It is completely black)
How do I solve this?
Your player does not like 1 fps
Many players behave similarly.
You can still provide a low frame rate for the input, but give a more normal frame rate for the output. ffmpeg will simply duplicate the frames. The output video won't look different than your original command, but it will play.
ffmpeg -framerate 1 -i "pics2/Image%02d.png" -r 10 -vcodec libx264 -s 1280x720 -pix_fmt yuv420p Output.mp4
The problem with your original script occurs when setting both -framerate and -r. Here is more information about it. Try using this as your command:
command = "ffmpeg -framerate 1 -i 'pics2/Image%02d.png' -vcodec libx264 -s 1280x720 -pix_fmt yuv420p Output.mp4"
Brief explanation (based on Saaru Lindestøkke's answer):
ffmpeg <- call ffmpeg
-framerate 1 <- set the input framerate to 1
-i 'pics2/Image%02d.png' <- read PNG images with filename Image01, Image02, etc...
-vcodec libx264 <- Set the codec to libx264
-s 1280x720 <- Set the resolution to 1280x720
-pix_fmt yuv420p <- Set the pixel format to planar YUV 4:2:0, 12bpp
Output.mp4 <- the output filename
I wrote in cmd:
ffmpeg -y -re -acodec pcm_s16le -rtsp_transport tcp -i
rtsp://192.168.1.200:554/11 -vcodec copy -af asetrate=22050 -acodec
aac -b:a 96k test.mp4
and I got a video file. If I write this command in python:
import os
os.system("ffmpeg -y -re -acodec pcm_s16le -rtsp_transport tcp -i
rtsp://192.168.1.200:554/11 -vcodec copy -af asetrate=22050 -acodec aac -b:a 96k test.mp4")
I get this error:
"ffmpeg" �� ���� ����७��� ��� ���譥� ��������, �ᯮ��塞�� �ணࠬ��� ���
������ 䠩���.
I tried to create a bat file and run it from python, but I get the same error. If I don't run it in python, all is good.
How do I start recording a video stream from an ip camera 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")
I have a ffmpeg pipeline in a shell script that is launched as a subprocess in Python. For some reason, when Python launches the script, my video streams perfectly and then freezes at about six minutes every time. After it freezes, if I tried to run the script manually, it gives me this error:
Invalid MIT-MAGIC-COOKIE-1 keyxcb_connection_has_error() returned true
Failed to symlink
/root/.pulse/65f3ded611649c6dcf9ebae20000046d-runtime to
/tmp/pulse-PKdhtXMmr18n: Input/output error [alsa # 0x4b2f0] cannot
open audio device hw:0,0 (Device or resource busy) hw:0,0:
Input/output error
However, if I restart and run the script manually, the audio & video will play fine indefinitely.
Does anyone know why this is happening? Thanks.
Here is my pipline.sh file:
sudo ffmpeg -f video4linux2 -video_size 640x480 -framerate 30 -input_format yuyv422 -i /dev/video7 -f alsa -i hw:0,0 -map 0:0 -map 1:0 -b:v 120k -bufsize 120k -vcodec libx264 -preset ultrafast -crf 28 -acodec aac -strict -2 -f flv -metadata streamName= StreamName tcp://71.192.1.22
And this is the subprocess I'm using in Python:
subprocess.Popen("sudo ./ffmpeg_script.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
My guess: the script isn't draining the stdout pipe quickly enough, and it's filling up with debugging output to the point that ffmpeg's stdout runs out of buffer space and it freezes.