I’m trying to stream FFmpeg with audio.
I will show my code below:
Import module
JavaScript
x
2
1
import subprocess as sp
2
Create variables
JavaScript
1
9
1
rtmpUrl = "rtmp://a.rtmp.youtube.com/live2/key"
2
camera_path = "BigBuckBunny.mp4"
3
cap = cv.VideoCapture(camera_path)
4
5
# Get video information
6
fps = int(cap.get(cv.CAP_PROP_FPS))
7
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
8
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
9
Command param
JavaScript
1
15
15
1
# ffmpeg command
2
command = ['ffmpeg',
3
'-y',
4
'-f', 'rawvideo',
5
'-vcodec','rawvideo',
6
'-pix_fmt', 'bgr24',
7
'-s', "{}x{}".format(width, height),
8
'-r', str(fps),
9
'-i', '-',
10
'-c:v', 'libx264',
11
'-pix_fmt', 'yuv420p',
12
'-preset', 'ultrafast',
13
'-f', 'flv',
14
rtmpUrl]
15
Create subprocess to ffmpeg command
JavaScript
1
3
1
# Pipeline configuration
2
p = sp.Popen(command, stdin=sp.PIPE)
3
Send frame to RTMP server
JavaScript
1
10
10
1
# read webcamera
2
while(cap.isOpened()):
3
ret, frame = cap.read()
4
if not ret:
5
print("Opening camera is failed")
6
break
7
8
# write to pipe
9
p.stdin.write(frame.tobytes())
10
I hope you can help me to be able to live stream via FFmpeg over RTMP with audio. Thanks!
Advertisement
Answer
Assuming you actually need to use OpenCV for the video, you have to add the audio directly to FFmpeg as Gyan commented, because OpenCV does not support audio.
-re
argument is probably required for live streaming.
For testing, I modified the RTMP URL from YouTube to localhost.
FFplay sub-process is used for capturing the stream (for testing).
Complete code sample:
JavaScript
1
55
55
1
import subprocess as sp
2
import cv2
3
4
#rtmpUrl = "rtmp://a.rtmp.youtube.com/live2/key"
5
rtmp_url = "rtmp://127.0.0.1:1935/live/test" # Use localhost for testing
6
camera_path = "BigBuckBunny.mp4"
7
cap = cv2.VideoCapture(camera_path)
8
9
# Get video information
10
fps = int(cap.get(cv2.CAP_PROP_FPS))
11
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
12
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
13
14
# Start the TCP server first, before the sending client (for testing).
15
ffplay_process = sp.Popen(['ffplay', '-listen', '1', '-i', rtmp_url]) # Use FFplay sub-process for receiving the RTMP video.
16
17
# ffmpeg command
18
# OpenCV does not support audio.
19
command = ['ffmpeg',
20
'-y',
21
'-re', # '-re' is requiered when streaming in "real-time"
22
'-f', 'rawvideo',
23
#'-thread_queue_size', '1024', # May help https://stackoverflow.com/questions/61723571/correct-usage-of-thread-queue-size-in-ffmpeg
24
'-vcodec','rawvideo',
25
'-pix_fmt', 'bgr24',
26
'-s', "{}x{}".format(width, height),
27
'-r', str(fps),
28
'-i', '-',
29
'-vn', '-i', camera_path, # Get the audio stream without using OpenCV
30
'-c:v', 'libx264',
31
'-pix_fmt', 'yuv420p',
32
'-preset', 'ultrafast',
33
# '-c:a', 'aac', # Select audio codec
34
'-bufsize', '64M', # Buffering is probably required
35
'-f', 'flv',
36
rtmp_url]
37
38
# Pipeline configuration
39
p = sp.Popen(command, stdin=sp.PIPE)
40
41
# read webcamera
42
while (cap.isOpened()):
43
ret, frame = cap.read()
44
if not ret:
45
print("End of input file")
46
break
47
48
# write to pipe
49
p.stdin.write(frame.tobytes())
50
51
p.stdin.close() # Close stdin pipe
52
p.wait()
53
54
ffplay_process.kill() # Forcefully close FFplay sub-process
55