Live streaming with FFmpeg - python

How to use this code in python, please guide in detail i am completely new.
ffmpeg \
-f alsa -ac 2 -i hw:1,0 \
-f v4l2 -r 10 -i /dev/video0 \
-c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 20 -b:v 2500k \
-c:a aac -ar 44100 \
-threads 0 -bufsize 512k \
-f flv rtmp://a.rtmp.youtube.com/live2/YOURSTREAM &> stream.log
i tried writing it in pycharm
when i just pasted in the editor it gave me error as usual
Then i tried it writing in os.system() and subprocess.call() like this
os.system
os.system(ffmpeg \)
os.system(-f alsa -ac 2 -i hw:1,0 \)
os.system(-f v4l2 -r 10 -i /dev/video0 \)
os.system(-c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 20 -b:v 2500k\)
os.system(-c:a aac -ar 44100 \)
os.system(-threads 0 -bufsize 512k \)
os.system(-f flv rtmp://a.rtmp.youtube.com/live2/YOURSTREAM &> stream.log)
subprocess.call()
subprocess.call(ffmpeg \)
subprocess.call(-f alsa -ac 2 -i hw:1,0 \)
subprocess.call(-f v4l2 -r 10 -i /dev/video0 \)
subprocess.call(-c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 20 -b:v 2500k\)
subprocess.call(-c:a aac -ar 44100 \)
subprocess.call(-threads 0 -bufsize 512k \)
subprocess.call(-f flv rtmp://a.rtmp.youtube.com/live2/YOURSTREAM &> stream.log)
both time it gave me errors like
'-i' is not recognized as an internal or external command,
operable program or batch file.
'-vcodec' is not recognized as an internal or external command,
operable program or batch file.
'-acodec' is not recognized as an internal or external command,
operable program or batch file.
'-f' is not recognized as an internal or external command,
operable program or batch file.
Please guide me and if there is any other way to upload a video to youtube live stream through python please let me know

You can try wrapping your entire ffmpeg command into a single subprocess.call, separating the arguments into a list.
This (slightly modified) command is working for me streaming a clip to my facebook page:
subprocess.call(["ffmpeg", "-ac", "2", "-i", "/home/ubuntu/clip.mp4", "-r", "10", "-i", "/home/ubuntu/clip.mp4", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-preset", "ultrafast", "-g", "20", "-b:v", "2500k", "-c:a", "aac", "-ar", "44100", "-threads", "0", "-bufsize", "512k", "-f", "flv", "rtmp://rtmp-api-dev.facebook.com:80/rtmp/10156686554299774?<my stream key>"])

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.

ffmpeg-python outputs black screen when trying to save as mp4

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

Get RTSP stream from ip camera (ffmpeg)

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?

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

Why is my ffmpeg stream subprocess freezing at 6 minutes?

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.

Categories

Resources