Why is my ffmpeg stream subprocess freezing at 6 minutes? - python

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.

Related

FFMPEG not working from python subprocess but works from terminal

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.

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.

Combining audio file and image with ffmpeg in python

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

os.system() returns error code 32512 - Python

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.

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.

Categories

Resources