Audio Slowly Desynchronizing When Segmenting - audio

I use ffmpeg's ability to segment video while I record so I can record constantly without my hard drive filling up.
It works really well, expect the audio desynchronizes from the video when the file segments. The video seems to be uninterrupted but I can actually hear a tiny jump in the audio when I join segments later on. One would think that ffmpeg would store packets in a queue during segmentation so nothing is lost but that doesn't seem to be the case... Any way I could force it to do something like that?
Here is my current block:
ffmpeg -y -thread_queue_size 5096 -f dshow -video_size 3440x1440 -rtbufsize 2147.48M -framerate 100 -pixel_format nv12 ^
-itsoffset 00:00:00.012 -i video="Video (00 Pro Capture HDMI 4K+)" -thread_queue_size 5096 -guess_layout_max 0 -f dshow ^
-rtbufsize 2147.48M -i audio="SPDIF/ADAT (1+2) (RME Fireface UC)" -map 0:0,1:0 -map 1:0 -c:v h264_nvenc -preset: llhp ^
-pix_fmt nv12 -b:v 250M -minrate 250M -maxrate 250M -bufsize 250M -b:a 384k -ac 2 -r 100 -vsync 1 ^
-max_muxing_queue_size 5096 -segment_time 600 -segment_wrap 9 -f segment C:\Users\djcim\Videos\PC\PC\PC%02d.mp4
I am delaying the video stream because right out the gate it's a little bit ahead of the audio.
PS: aresample or async seem to have no effect or at least not a desirable one.

Using -reset_timestamps in conjunction with encoding .ts instead of .mp4 has mostly solved this issue. -reset_timestamps does not appear to work when encoding .mp4, not sure why, maybe a bug?
I say mostly because audio still drifts around a frame after the first segment, but not exponentially. I find audio de-synced by one frame acceptable. Although I should mention now when I try to concat the clips back together I have audio drift issues, not always, but sometimes. Using aresample=async=250 fixes the drift after concat but you can hear the audio stretch a bit when doing so. Can't expect everything to work perfectly.
ffmpeg - y -thread_queue_size 9999 -indexmem 9999 -guess_layout_max 0 -f dshow -video_size 3440x1440 -rtbufsize 2147.48M ^
-framerate 200 -pixel_format nv12 -i video="Video (00 Pro Capture HDMI 4K+)":audio="SPDIF/ADAT (1+2) (RME Fireface UC)" ^
-map 0:0,0:1 -map 0:1 -flags +cgop -force_key_frames expr:gte(t,n_forced*2) -c:v h264_nvenc -preset: llhp -pix_fmt nv12 ^
-b:v 250M -minrate 250M -maxrate 250M -bufsize 250M -c:a aac -ar 44100 -b:a 384k -ac 2 -r 100 ^
-af "atrim=0.038, asetpts=PTS-STARTPTS, aresample=async=250" -vsync 1 -ss 00:00:01.096 -max_muxing_queue_size 9999 ^
-f segment -segment_time 600 -segment_wrap 9 -reset_timestamps 1 C:\Users\djcim\Videos\PC\PC\PC%02d.ts

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

FFmpeg to Azure Media Services Smooth Streaming Input

I would like to ask about ffmpeg config or command to to mp4 fragment to Azure Media Service live event using smooth streaming / isml protocol. The AMS is not getting any input yet from ffmpeg.
This is my current running command:
ffmpeg -f dshow -i video="Webcam" -movflags isml+frag_keyframe -f isml -r 10 http://endpoint/ingest.isml/streaming1
When I am using RTMP with Wirecast is running well.
Any suggestion on ffmpeg command with isml protocol?
Thank you
it is possibly the way you are formatting the ingest URL. The Smooth ingest protocol expects the name of /Streams(yourtrackname-identifier) after it.
See the Smooth ingest specification for details
Here is an FFMPEG command line that I had sitting around that worked for me on a Raspberry Pi at one time
ffmpeg -i /dev/video1 -pix_fmt yuv420p -f ismv -movflags isml+frag_keyframe -video_track_timescale 10000000 -frag_duration 2000000 -framerate 30 -r 30 -c:v h264_omx -preset ultrafast -map 0:v:0 -b:v:0 2000k -minrate:v:0 2000k -maxrate:v:0 2000k -bufsize 2500k -s:v:0 640x360 -map 0:v:0 -b:v:1 500k -minrate:v:1 500k -maxrate:v:1 500k -s:v:1 480x360 -g 60 -keyint_min 60 -sc_threshold 0 -c:a libfaac -ab 48k -map 0:a? -threads 0 "http://johndeu-nimbuspm.channel.mediaservices.windows.net/ingest.isml/Streams(video)"
Note that i used the following stream identifier - ingest.isml/Streams(video)
Here are a couple more commands that may help.
fmpeg -v debug -y -re -i "file.wmv" -movflags isml+frag_keyframe -video_track_timescale 10000000 -frag_duration 2000000 -f ismv -threads 0 -c:a libvo_aacenc -ac 2 -b:a 20k -c:v libx264 -preset fast -profile:v baseline -g 48 -keyint_min 48 -b:v 200k -s:v 320x240 http://xxxx.userid.channel.mediaservices.windows.net/ingest.isml/Streams(video)
Multi-bitrate encoding and ingest
ffmpeg -re -stream_loop -1 -i C:\Video\tears_of_steel_1080p.mov -movflags isml+frag_keyframe -f ismv -threads 0 -c:a aac -ac 2 -b:a 64k -c:v libx264 -preset fast -profile:v main -g 48 -keyint_min 48 -sc_threshold 0 -map 0:v -b:v:0 5000k -minrate:v:0 5000k -maxrate:v:0 5000k -s:v:0 1920x1080 -map 0:v -b:v:1 3000k -minrate:v:1 3000k -maxrate:v:1 3000k -s:v:1 1280x720 -map 0:v -b:v:2 1800k -minrate:v:2 1800k -maxrate:v:2 1800k -s:v:2 854x480 -map 0:v -b:v:3 1000k -minrate:v:3 1000k -maxrate:v:3 1000k -s:v:3 640x480 -map 0:v -b:v:4 600k -minrate:v:4 600k -maxrate:v:4 600k -s:v:4 480x360 -map 0:a:0
http://.myradarmedia.channel.mediaservices.windows.net/ingest.isml/Streams(stream0^)
EXPLANATION OF WHAT IS GOING ON ABOVE ON THE FFMPEG COMMAND LINE.
ffmpeg
-re **READ INPUT AT NATIVE FRAMERATE
-stream_loop -1 **LOOP INFINITE
-i C:\Video\tears_of_steel_1080p.mov **INPUT FILE IS THIS MOV FILE
-movflags isml+frag_keyframe **OUTPUT IS SMOOTH STREAMING THIS SETS THE FLAGS
-f ismv **OUTPUT ISMV SMOOTH
-threads 0 ** SETS THE THREAD COUNT TO USE FOR ALL STREAMS. YOU CAN USE A STREAM SPECIFIC COUNT AS WELL
-c:a aac ** SET TO AAC CODEC
-ac 2 ** SET THE OUTPUT TO STEREO
-b:a 64k ** SET THE BITRATE FOR THE AUDIO
-c:v libx264 ** SET THE VIDEO CODEC
-preset fast ** USE THE FAST PRESET FOR X246
-profile:v main **USE THE MAIN PROFILE
-g 48 ** GOP SIZE IS 48 frames
-keyint_min 48 ** KEY INTERVAL IS SET TO 48 FRAMES
-sc_threshold 0 ** NOT SURE!
-map 0:v ** MAP THE FIRST VIDEO TRACK OF THE FIRST INPUT FILE
-b:v:0 5000k **SET THE OUTPUT TRACK 0 BITRATE
-minrate:v:0 5000k ** SET OUTPUT TRACK 0 MIN RATE TO SIMULATE CBR
-maxrate:v:0 5000k ** SET OUTPUT TRACK 0 MAX RATE TO SIMULATE CBR
-s:v:0 1920x1080 **SCALE THE OUTPUT OF TRACK 0 to 1920x1080.
-map 0:v ** MAP THE FIRST VIDEO TRACK OF THE FIRST INPUT FILE
-b:v:1 3000k ** SET THE OUTPUT TRACK 1 BITRATE TO 3Mbps
-minrate:v:1 3000k -maxrate:v:1 3000k ** SET THE MIN AND MAX RATE TO SIMULATE CBR OUTPU
-s:v:1 1280x720 ** SCALE THE OUTPUT OF TRACK 1 to 1280x720
-map 0:v -b:v:2 1800k ** REPEAT THE ABOVE STEPS FOR THE REST OF THE OUTPUT TRACKS
-minrate:v:2 1800k -maxrate:v:2 1800k -s:v:2 854x480
-map 0:v -b:v:3 1000k -minrate:v:3 1000k -maxrate:v:3 1000k -s:v:3 640x480
-map 0:v -b:v:4 600k -minrate:v:4 600k -maxrate:v:4 600k -s:v:4 480x360
-map 0:a:0 ** FINALLY TAKE THE SOURCE AUDIO FROM THE FIRST SOURCE AUDIO TRACK.
http://.myradarmedia.channel.mediaservices.windows.net/ingest.isml/Streams(stream0^)
The URL above is part of the output o the command... formatting issue.

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

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

FFMpeg Shorten Audio Input

I'm trying to syncronise some audio with video - the audio is recorded via an App in the M4U format, at the same time as the recording starts 2 cameras are triggered and start recording. When the recording stops both the audio and cameras stop recording. Now these are out by quite a bit, like at least a second. The file time lengths are the same, but the audio clearly starts recording earlier then the video.
I'm trying to syncronise these and I can do it manually in Audacity, but I'd like to try and get it close via FFMEG.
I've been having a good look around and can find commands for delaying the start of the audio track, but not cutting off the first few seconds of the Audio. I'm trying something like this
ffmpeg -framerate 24 -i .\seq\%%04d.jpg -itsoffset -3 -i audio.m4u -map 0:v -map 1:a -c:v libx264 -pix_fmt yuv420p -c:v libx264 -crf 23 -profile:v high -movflags faststart -b 5000k -shortest out.mp4
Any clues how to remove the first few seconds form the audio input?
The atrim filter does just that:
ffmpeg -framerate 24 -i .\seq\%%04d.jpg -i audio.m4u -map 0:v -map 1:a -af atrim=3,asetpts=N/SR/TB -c:v libx264 -pix_fmt yuv420p -c:v libx264 -crf 23 -profile:v high -movflags faststart -shortest out.mp4
The asetpts is added to reset the timestamps of the trimmed audio.

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.

Resources