I'm trying to use the concat protocol in ffmpeg as described in the ffmpeg docs:
https://trac.ffmpeg.org/wiki/Concatenate
However I'm getting lots of errors about corrupt packets when running the concat, so I'm worried that this isn't the best approach. My actual use case will involve running unsupervised with a ton of different source videos, so I want to be sure that it's solid.
The concat demuxer approach succeeds without errors but takes about 10 times as long.
Steps to reproduce
Download Big Buck Bunny:
wget https://download.blender.org/demo/movies/BBB/bbb_sunflower_1080p_30fps_normal.mp4
Transcode a 30 second chunk:
ffmpeg -i bbb_sunflower_1080p_30fps_normal.mp4 -ss '00:06:30' -t 30 -c:v libx264 -crf 18 bbb30.mp4
Create one second parts:
mkdir -p parts;
for i in $(seq -f "%02g" 0 29); do \
ffmpeg \
-i bbb30.mp4 \
-ss "00:00:$i" -t 1 \
-c:v libx264 -pix_fmt yuv420p -crf 18 \
-bsf:v h264_mp4toannexb \
-f mpegts \
-y parts/$i.ts;
done
Combine all the parts into a new output mp4:
ffmpeg -y \
-i "concat:parts/00.ts|parts/01.ts|parts/02.ts|parts/03.ts|parts/04.ts|parts/05.ts|parts/06.ts|parts/07.ts|parts/08.ts|parts/09.ts|parts/10.ts|parts/11.ts|parts/12.ts|parts/13.ts|parts/14.ts|parts/15.ts|parts/16.ts|parts/17.ts|parts/18.ts|parts/19.ts|parts/20.ts|parts/21.ts|parts/22.ts|parts/23.ts|parts/24.ts|parts/25.ts|parts/26.ts|parts/27.ts|parts/28.ts|parts/29.ts" \
-c copy \
output.mp4
Stderr has lots of warnings about corrupt packets (in this case always at dts = 21300):
[mpegts # 0x555d172daa00] Packet corrupt (stream = 0, dts = 213000).
concat:parts/00.ts|parts/01.ts|parts/02.ts|parts/03.ts|parts/04.ts|parts/05.ts|parts/06.ts|parts/07.ts|parts/08.ts|parts/09.ts|parts/10.ts|parts/11.ts|parts/12.ts|parts/13.ts|parts/14.ts|parts/15.ts|parts/16.ts|parts/17.ts|parts/18.ts|parts/19.ts|parts/20.ts|parts/21.ts|parts/22.ts|parts/23.ts|parts/24.ts|parts/25.ts|parts/26.ts|parts/27.ts|parts/28.ts|parts/29.ts: corrupt input packet in stream 0
The resulting mp4 looks ok to my eye, but obviously ffmpeg isn't happy about something. Any ideas?
You can ignore these warnings.
Packets in a MPEG-TS container have a counter field which increments with each packet. These are expected to be continuous. Since you encoded your TS files in separate instances, that counter will start with 0 when switching from one TS input to another. But this has no salience in this use case. This happens with the concat protocol because a single TS demuxer instance is used to read all inputs as an amalgamated whole. The concat demuxer open a fresh TS demuxer for each input and then stitches all packets as a single stream.
Related
According to the ffmpeg documentation
-vsync parameter
Video sync method. For compatibility reasons old values can be specified as numbers. Newly added values will have to be
specified as strings always.
drop
As passthrough but destroys all timestamps, making the muxer
generate fresh timestamps based on frame-rate.
It appears that the mpegts mux does not regenerate the timestamps correctly (PTS/DTS); however, piping the output after vsync drop to a second process as raw h264 does force mpegts to regenerate the PTS.
Generate test stream
ffmpeg -f lavfi -i testsrc=duration=20:size=1280x720:rate=50 -pix_fmt yuv420p -c:v libx264 -b:v 4000000 -x264-params ref=1:bframes=0:vbv-maxrate=4500:vbv-bufsize=4000:nal-hrd=cbr:aud=1:bframes=0:intra-refresh=1:keyint=30:min-keyint=30:scenecut=0 -f mpegts -muxrate 5985920 -pcr_period 20 video.ts -y
Generate output ts that has correctly spaced PTS values
ffmpeg -i video.ts -vsync drop -c:v copy -bsf:v h264_mp4toannexb -f h264 - | ffmpeg -fflags +igndts -fflags +nofillin -fflags +genpts -r 50 -i - -c:v copy -f mpegts -muxrate 5985920 video_all_pts_ok.ts -y
Generate output ts where all PTS are zero
ffmpeg -i video.ts -vsync drop -c:v copy -bsf:v h264_mp4toannexb -f mpegts - | ffmpeg -fflags +igndts -fflags +nofillin -fflags +genpts -r 50 -i - -c:v copy -f mpegts -muxrate 5985920 video_all_pts_zero.ts -y
It appears that vsync drop does destroy them but the mpegts doesn't regenerate them? Any ideas on what needs adding to get it to work as a single ffmpeg command?
Tested on both Linux and Windows with the same result
Try recoding the video just using -vsync 1, without -fflags +genpts. I found some good information here. This guy talking about streaming video. So highest quality isn't his objective. But there is useful info.
https://videoblerg.wordpress.com/2017/11/10/ffmpeg-and-how-to-use-it-wrong/
Section one – Constant frame rate
"-r is used to specify the output frame rate. This must be the same as the input frame rate to eliminate judder. This is used in conjunction with the -vsync parameter using the 1 option which will retime the PTS/DTS timestamps accordingly"
Section six – Audio [Has some good advice too]
"-af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" helps to keep your audio lined up with the beginning of your video. It is common for a container to have the beginning of the video and the beginning of the audio start at different points. By using this your container should have little to no audio drift or offset as it will pad the audio with silence or trim audio with negative PTS timestamps if the audio does not actually start at the beginning of the video."
I haven't tried this yet, no videos with sync problems at the moment.
I can't find any answer to this scenarios:
Produce a continuous ts file that initially contains silence (or blank video), and as I add files to a text file I wish them to be queued in the output ts stream.
Think of it as a radio or tv station.
I found everything on how to stream anything to anything,
but not a tv/radio broadcast like stream.
A good start seems to be this:
ffmpeg -re -y -nostats -nostdin -hide_banner -loglevel quiet -fflags +genpts -f concat -i list.txt -map 0:a? -map 0:v? -map 0:s? -strict -2 -dn -c copy -hls_flags delete_segments -hls_time 10 -hls_list_size 6 /var/www/html/showname_.m3u8
list.txt example
file '/mnt/vusolo-tv/show/Season 1/S01E01.mp4'
file '/mnt/vusolo-tv/show/Season 1/S01E02.mp4'
But i think there can be better ways...
This is not a good answer either: From multiple video files to single output
because "All files must have streams with identical encoding. Timebase for streams should be the same. Duration of all streams within a file should be the same, in order to maintain sync."
And I want to be able to add arbitrary files and produce a TS stream playable by any TV (once broadcasted in dvb).
I'm trying to concatenate a 15 second clip of a video (MOVIE.mp4) with 5 seconds (no audio) of an image (IMAGE.jpg) using FFmpeg.
Something seems to be wrong with my filtergraph, although I'm unable to determine what. The command I've put together is the following:
ffmpeg \
-loop 1 -t 5 -I IMAGE.jpg \
-t 15 -I MOVIE.mp4 \
-filter_complex "[0:v]scale=480:640[1_v];anullsrc[1_a];[1:v][1:a][1_v][1_a]concat=n=2:v=1:a=1[out]" \
-map "[out]" \
-strict experimental tst_full.mp4
Unfortunately, this seems to be creating some strange results:
On my personal computer (FFmpeg 4.2.1) it correctly concatenates the movie with the static image; however, the static image lasts for an unbounded length of time. (After entering ctrl-C, the movie is still viewable, but is of an extremely long length--e.g., 35 min--depending on when I interrupt the process.)
On a remote machine where I need to do the ultimate video processing (FFmpeg 2.8.15-0ubuntu0.16.04.1), the command does not terminate, and instead, I get cascading errors of the following form:
Past duration 0.611458 too large
...
[output stream 0:0 # 0x21135a0] 100 buffers queued in output stream 0:0, something may be wrong.
...
[output stream 0:0 # 0x21135a0] 100000 buffers queued in output stream 0:0, something may be wrong.
I haven't been able to find much documentation that elucidates what these errors mean, so I don't know what's going wrong.
As Gyan pointed out, you only have to add atrim to your audio:
anullsrc,atrim=0:5[silent-audio]
Instead of scale you could use scale2ref and setsar to automatically make your image the same size and aspect ratio as the video.
ffmpeg \
-loop 1 -t 5 -i IMAGE.jpg \
-t 15 -i MOVIE.mp4 \
-filter_complex "[0:v][1:v]scale2ref[img][v];[img]setsar=1[img]; \
anullsrc,atrim=0:5[silent-audio];[v][1:a][img]
[silent-audio]concat=n=2:v=1:a=1[out]" \
-map "[out]" \
-strict experimental tst_full.mp4
Alternatively you could use anullsrc as a 3rd input:
ffmpeg \
-t 15 -i MOVIE.mp4 \
-loop 1 -t 5 -i IMAGE.jpg \
-f lavfi -t 5 -i anullsrc \
-filter_complex "[1:v][0:v]scale2ref[img][v];\
[img]setsar=1[img];[v][0:a][img][2:a]concat=n=2:v=1:a=1[out]" \
-map "[out]" \
-strict experimental tst_full.mp4
I am using ffmpeg to encode livestreams for use in a tvheadend server. ffmpeg and hls discontinuities dont work, but ive fixed that using streamlink to read the hls stream then pipe that into ffmpeg.
Sometimes the audio has gaps in the live stream and the audio goes out of sync from that point on, I have managed to fix this using aresample=async=1. ffmpeg inserts silence for the gaps and audio stays synced.
Tvheadend doesnt like dts discontinuities and the stream will freeze whenever one is encountered. I have also fixed this with dts_delta_threshold 1. With this option the stream plays seamlessly without any freezes
Here is where my problem comes in when using dts_delta_threshold 1 the aresample command no longer works, I assume because there are no more gaps so it cant insert the silence. Ive tried various combinations and ordering of options.
Is there any way to apply the aresample=async=1 and also the dts_delta_threshold 1 command after.
This is my current command
streamlink -l warning --ringbuffer-size 64M --hls-timeout 100000000 --hls-live-restart hls://192.168.10.1/play/$1.$2.m3u8 best -O | \
ffmpeg -loglevel fatal -err_detect ignore_err \
-f mpegts -i - \
-filter_complex "eq=contrast=${3:-1.0}" \
-c:v libx264 -crf 18 -preset superfast -tune zerolatency -pix_fmt yuv420p -force_key_frames "expr:gte(t,n_forced*2)" \
-c:a aac -b:a 256k -ac 2 -af aresample=async=1 \
-metadata service_provider=$1 -metadata service_name="$1.$2" -f mpegts pipe:1
Ive tried putting the dts_delta_threshold before and after input, same thing audio goes out of sync if there is a gap in audio. Ive tried putting async 1 before input but that doesnt work either
I am trying to convert a source VBR SPTS MPEG-2 TS file into CBR using ffmpeg. The code I am using is the following:
#!/bin/bash
pkill ffmpeg
ffmpeg \
-re -i source.ts -c copy \
-muxrate 18000K \
-f mpegts \
udp://destination_ip:1234?pkt_size=1316
The source VPID bitrate is ~ 10Mbps and the APID is 296Kbps. So according to my understanding this code should deliver 18Mbps CBR where the difference between the muxrate and the bitrate of all the PIDs is filled with null packets.
The problem is that the output is far from perfect. The overall bitrate is semi-CBR at best. It ranges between 12Mbps and 15Mbps and I see a lot of PCR accuracy and PCR repetition errors along with CC errors both on the VPID and APID.
Some ideas:
make sure you have a recent version of ffmpeg because at some point there was a bug which messed up PCR insertion when stream copying
if you want constant UDP output you must use the bitrate option like:
-flush_packets 0 -f mpegts "udp://destination_ip:1234?pkt_size=1316&bitrate=18000000"
UDP is an unreliable protocol and you might experience packet loss (unfortunately the bitrate option only works for UDP for now AFAIK)
if you have a dedicated connection but still experience CC errors check the destination OS max UDP buffer sizes and make sure it can handle 18 Mbps
specify -minrate and -maxrate too.
use -bufsize bigger than bitrate.
set -muxrate value like bufsize.
The final command:
ffmpeg \
-re -i source.ts \
-b:v 10500k \
-minrate 10500k \
-maxrate 10500k \
-bufsize 18000k \
-muxrate 18000k \
-f mpegts \
udp://destination_ip:1234?pkt_size=1316