Problem with combining a video and an audio stream from USB device - audio

I have two USB devices attached to an RPi, both show up as usual as /dev/video0. Here's some additional info coming from two command line inputs:
Device 1, video only (attached to an RPi4):
ffmpeg -f v4l2 -list_formats all -i /dev/video0 reports
[video4linux2,v4l2 # 0xe5e1c0] Compressed: mjpeg :
Motion-JPEG : 1280x720 640x480 320x240
v4l2-ctl --list-formats-ext reports
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 1280x720
Interval: Stepwise 0.033s - 0.033s with step 0.000s
(30.000-30.000 fps)
Size: Discrete 640x480
Interval: Stepwise 0.033s - 0.033s with step 0.000s
(30.000-30.000 fps)
Size: Discrete 320x240
Interval: Stepwise 0.033s - 0.033s with step 0.000s
(30.000-30.000 fps)
Does work: ffmpeg -f v4l2 -i /dev/video0 -vcodec h264_omx -preset ultrafast -tune zerolatency -g 300 -b:v 1M -mpegts_service_type advanced_codec_digital_hdtv -f mpegts udp://OtherMachine:Port?pkt_size=1316
Device 2, video and audio (attached to an RPi3, but does not work either on the RPi4):
ffmpeg -f v4l2 -list_formats all -i /dev/video0 reports
[video4linux2,v4l2 # 0x2c41210] Compressed: mjpeg :
Motion-JPEG : 1920x1080 1280x720
v4l2-ctl --list-formats-ext reports
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'MJPG' (compressed)
Name : Motion-JPEG
Size: Discrete 1920x1080
Interval: Discrete 0.033s
(30.000 fps)
Interval: Discrete 0.067s
(15.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.033s
(30.000 fps)
Interval: Discrete 0.067s
(15.000 fps)
After quite some tedious work and way too many hours I got this running:
Video only: ffmpeg -f v4l2 -input_format mjpeg -i /dev/video0 -c:v copy -preset ultrafast -tune zerolatency -g 300 -f matroska udp://OtherMachine:Port?pkt_size=1316
Does not work at all: ffmpeg -f v4l2 -input_format mjpeg -i /dev/video0 -c:v copy -preset ultrafast -tune zerolatency -g 300 -f mpegts udp://OtherMachine:Port?pkt_size=1316, on "OtherMachine" I do see that there is an incoming data stream via VLC, but it could not be digested properly.
Audio only: ffmpeg -f alsa -thread_queue_size 1024 -i plughw:1 -c:a mp2 -ac 2 -ar 44100 -preset ultrafast -tune zerolatency -b:a 128K -f mpegts udp://OtherMachine:Port?pkt_size=1316
But this does not work either:
ffmpeg -f v4l2 -input_format mjpeg -i /dev/video0 -f alsa -thread_queue_size
1024 -i plughw:1 -c:v copy -c:a mp2 -ac 2 -ar 44100 -preset ultrafast -tune zerolatency -g 300 -b:a 128K -f mpegts udp://OtherMachine:Port?pkt_size=1316
Could you please provide a hint on how to get these two streams for device 2 working together? Both of them come from the same hardware/device, my guess is that the MJPG video stream is somehow not fully compliant with the mpegts standard (like it is for device 1) since it works with matroska, but not with mpegts. Could that be? What needs to be done in that case?
Another hint, with the same kind of hardware setup I can do this
cvlc -vvv v4l2:///dev/video0 --input-slave=alsa://plughw:1,0 --sout='#transcode{acodec=mpga,ab=128}:std{access=http,mux=asf,dst=:Port}'
So, here my understanding is that video gets passed on unchanged (mjpeg) and audio gets transcoded via vlc's mpga which presumably corresponds to mp2 for ffmpeg. The container format is asf, but I was not able to get that running with ffmpeg for no obvious reason. Anyway, picking up this vlc broadcast stream via http://StreamingMachine:Port on any other machine in my network is working well. But how to achieve that with ffmpeg directly and potentially not as http:// but udp:// or pipe stream?
Alternatively, let me ask this question: Given that I have an incoming mjpeg video stream as well as an incoming mp2 audio stream which kind of container format (ok, it's obviously not mpegts) is the most appropriate one for combined streaming across my LAN or even into a pipe for further processing? Believe me, I tried my very best over a couple of hours to find out how to proceed but with no success. At least to my humble knowledge there is nothing such like a table providing answers to questions of that kind.
I'd be glad to get some insights.
Best

Related

ffmpeg livestream by ip camera with a problem of DTS

I'm a problem with a code of ffmpeg: ffmpeg -rtsp_transport tcp -i "rtsp://admin:passw#xxxxxxxx.ddns.net:554/live/ch0" -deinterlace -vcodec libx264 -pix_fmt yuv420p -preset medium -s 1920x1080 -b:v 5000k -acodec aac -strict -2 -ar 44100 -threads 6 -qscale 3 -b:a 712000 -bufsize 128k -f flv "rtmp://a.rtmp.youtube.com/live2/key"
It's used for a live streaming by a ip camera, but I have this problem
[flv # 0x558333a41100] Non-monotonous DTS in output stream 0:1; previous: 73709, current: 73220; changing to 73709. This may result in incorrect timestamps in the output file.
[aac # 0x558333a0f100] Queue input is backward in time14.43 bitrate=1288.8kbits/s dup=0 drop=5 speed=0.486x
[flv # 0x558333a41100] Non-monotonous DTS in output stream 0:1; previous: 74591, current: 73614; changing to 74591. This may result in incorrect timestamps in the output file.
who can help me?
Especially in live streams, there will occasionally happen some "jumps" on timestamps. Why this happens... well, there are many reasons, including packet losses.
When this happens this will cause momentary distortion in the output.
Here FFmpeg tells you a jump has occurred. I consider this not an error on your sideā€”just a warning.
As the warning says, FFmpeg already handled it (by correcting the timestamps).
There isn't anything much to do, other than sometimes you may need to restart the FFmpeg, because of the unexpected changes on the stream.
thank you so much.
I proceeded to change the code with a simpler one."ffmpeg -rtsp_transport tcp -i "rtsp://xxxxxxxxxxxxxxxxxxx.ddns.net:554/live/ch0" -tune zerolatency -s 1920x1080 -vcodec libx264 -c:v libx264 -c:a aac -preset ultrafast -g 50 -f flv "rtmp://a.rtmp.youtube.com/live2/key"
actually the problem was that the live of yt would end even if ffmper kept processing so suddenly. With the new code the terminal no longer gives the error but the problem persists

How to monitor volume level during ffmpeg capture

I capture sound from soundcard, convert it to mp3 and stream to multicast IP.
Command looks like:
ffmpeg -f alsa -i hw:0 -async 1 -vn -acodec libmp3lame -ac 1 -ar 44100 -b:a 128k -flush_packets 0 -f mpegts -pes_payload_size 426 -mpegts_start_pid 0x44 udp://233.21.215.101:1234?pkt_size=1316
But I need to monitor volume level of capturing. If there is no sound (or noise only) I need to switch to another (reserve) channel.
Can I get volume level in ffmpeg output? How I can do it?
P.S: I can get volume level by "volumedetect" filter but it's not in realtime. I need realtime monitoring.
P.P.S: I run ffmpeg programmatically from my application and can't monitor additional windows (like video frame when I use showvolume filter).
Right now, I can suggest using the showvolume filter
ffmpeg -f alsa -i hw:0
-async 1 -vn -acodec libmp3lame -ac 1 -ar 44100 -b:a 128k
-flush_packets 0 -f mpegts
-pes_payload_size 426 -mpegts_start_pid 0x44 udp://233.21.215.101:1234?pkt_size=1316
-filter_complex showvolume[a] -map [a] -f sdl -
A small video window will pop up with a bar graph display of the current volume and also as text labels. You'll need a ffmpeg build with SDL library linked.
You can't close this video window while your streaming is active.

ffmpeg add all audio tracks to video conversion (mkv)

I have a script that takes in input a video file (generally avi or mp4) and converts it to a "lower quality" mkv video optimized for web streaming.
The ffmpeg command I use is this one:
ffmpeg -fflags +genpts -i file:"$input" -sn -codec:v:0 libx264 -force_key_frames expr:gte\(t,n_forced*5\) -vf "scale=trunc(min(max(iw\,ih*dar)\,1280)/2)*2:trunc(ow/dar/2)*2" -pix_fmt yuv420p -preset superfast -crf 23 -b:v 1680000 -maxrate 1680000 -bufsize 3360000 -vsync vfr -profile:v high -level 41 -map_metadata -1 -threads 8 -codec:a:0 libmp3lame -ac 2 -ab 320000 -af "aresample=async=1" -y "$output"
The problem is that this command only includes the first audio track of my video. I have some dual language videos (italian and english) for which I want to include both languages.
Is there a simple ffmpeg command option that automatically includes all audio tracks found in a video?
Add -map 0:a to include all audio streams.

How to eliminate the distortion in live streaming Nodejs + ffmpeg

I tried Live video Streaming with NodeJS and ffmpeg encoder. It works with a lag of around 2sec and with a distortion as well. Lag does not matter as there is always. But I need to eliminate the video distortion as much as possible. So what would be the suitable bit rates and is there a better encoder to do this? In ffmpeg, it encodes to mpegts so is there a more preferable format than mpegts ? plz help
my encoding code was
ffmpeg -s 640x480 -f dshow -i video="HP HD Webcam":audio="Microphone (Realtek High Definition Audio)" -preset ultrafast -qp 0 -f mpegts -v:b 800 -r 100 http://localhost:8082/abc/640/480/
You didn't set a video codec, so it used mpeg2 (the default for mpegts). You want to use H264, so use -c:v libx264:
ffmpeg -s 640x480 -f dshow -i video="HP HD Webcam":audio="Microphone (Realtek High Definition Audio)" -c:v libx264 -preset ultrafast -qp 0 -f mpegts -v:b 800 -r 100 http://localhost:8082/abc/640/480/
And then it should be fine. In addition, the green boxes sound like bugs (overflows?), so perhaps file a bug about them on the ffmpeg bug tracker.

mplayer can't read udp video stream

Im trying to compare latency between different video codecs using ffmpeg and mplayer's benchmark.
I am using this command line to generate and send the stream:
ffmpeg -s 1280x720 -r 100 -f x11grab -i :0.0 -vcodec mpeg2video -b:v 8000 -f mpegts udp://localhost:4242
And I'm successfully using ffplay to receive and read it in real time:
ffplay -an -sn -i -fflags nobuffer udp://localhost:4242?listen
Now instead of playing the stream with ffplay, i'd like to use the mplayer benchmark to get some information on the latency:
mplayer -msglevel all=6 -benchmark udp://localhost:4242
But I get this output instead:
Playing udp://localhost:4242.
get_path('sub/') -> '/home/XXXXX/.mplayer/sub/'
STREAM_UDP, URL: udp://localhost:4242
Filename for url is now udp://localhost:4242
Listening for traffic on localhost:4242 ...
Timeout! No data from host localhost
udp_streaming_start failed
No stream found to handle url udp://localhost:4242
I tried with rtp protocol instead, didn't work either...
Does anyone have an idea what i'm doing wrong?
Thanks for the answers,
I actually tried a lot of different codecs, especially vp9, h264 and mpeg2, but the best low latency i got were with mpeg2video. Here are 3 of the command lines I used. I read the ffmpeg streaming guide and the different codec's encoding guides to try to get the best parameters for each of them, but the difference is noticeable:
ffmpeg -an -sn -s 1280x720 -r 30 -f x11grab -i :0.0 -vcodec libx264 -crf 18 -tune zerolatency -preset ultrafast -pix_fmt yuv420p -profile:v baseline -b:v 8000 -f mpegts threads 4 udp://127.0.0.1:4242
ffmpeg -s 1280x720 -r 30 -f x11grab -i :0.0 -vcodec mpeg2video -b:v 800k -f mpegts -threads 8 udp://127.0.0.1:4242
ffmpeg -t 5 -s 1280x720 -r 30 -f x11grab -i :0.0 -vcodec libvpx-vp9 -an -crf 18 -b:v 1M -f webm -threads 8 udp://127.0.0.1:4242
On localhost, I'm close to no latency at all with mpeg2video, when I have almost 1sec latency with h264. I heard vp9 could have very low latency too, but I apparently don't know how to use the options in ffmpeg, cuz I get really bad latency values...
Anyway, to get back to the topic, 127.0.0.1 instead of localhost doesn't help, and with ffmpeg://udp://ip:port it doesn't work either... :/ I think I may have wrong configurations on mplayer. maybe I should try to compile it myself.
But actually, I don't even know if mplayer would give me the informations I want (the average number of ms for a codec to encode/decode a frame, so that I can compare my different codecs precisely).
EDIT: Sorry for that... ffmpeg://udp://ip_addr works =) I made a typing mistake... n_n
Thanks a lot. Though, the quality of the video is really aweful compared to ffplay when I use mplayer...

Resources