[FFmpeg-user] Issues with concatenating specific MP4s from a Unifi camera

Davison Long dlong at nextgentech.net
Sun Mar 19 23:30:54 EET 2023

-----Original Message-----
From: ffmpeg-user <ffmpeg-user-bounces at ffmpeg.org> On Behalf Of Davison Long
Sent: Wednesday, March 8, 2023 3:47 AM
To: ffmpeg-user at ffmpeg.org
Subject: [FFmpeg-user] Issues with concatenating specific MP4s from a Unifi camera

I have a Unifi Video server storing video data from a UVC G3 Pro camera (in the form of 2 second MP4 files).  I've been creating time-lapses of each day using ffmpeg and the vast majority of days work perfectly fine.  However, some days the source MP4s must get malformed somehow such that the concatenation phase doesn't work well at all and produces a broken file that freezes after the first couple seconds.

My procedure is to first concatenate and speed up all the video for a given day and then transcode the resultant H264 MP4 into a H265 MKV file.  To simplify things for diagnostics I've boiled the problematic command down to just the first step without any speedup or transcoding.  I've uploaded to Dropbox eight files for each set (bad source files and good source files).  The expected result (which occurs for the good source files) is a 16 second video.  The bad source files produce a video that MPC-HC or VLC show as a 2 second duration.  In MPC-HC the video portion freezes after 2 seconds and then the audio continues for the remainder of the 16 seconds.  In VLC both the video and audio cut out after 2 seconds.

The two sets of source files along with my resulting good and bad resultant files are on Dropbox here:

Here are the commands I've used to generate the good and bad resultant videos from the files:
# Run the concatenation on the bad source files ffmpeg -safe 0 -f concat -i <(find /mnt/input/bad_source_files -type f -name '*.mp4' -printf "file '%p'\n" | sort) -vcodec copy -acodec copy /mnt/output/bad_video.mp4

# Run the concatenation on the good source files ffmpeg -safe 0 -f concat -i <(find /mnt/input/good_source_files -type f -name '*.mp4' -printf "file '%p'\n" | sort) -vcodec copy -acodec copy /mnt/output/good_video.mp4

When the command is used on the bad set of files I get hundreds of notices like this:
[mp4 @ 0x560b694b4700] Packet duration: 2223883790 / dts: 2223981070 is out of range
[mp4 @ 0x560b694b4700] pts has no value

When the command is run on the good set of files I also get a bunch of different notices like these:
DTS 35273693825, next:2041445 st:0 invalid dropping PTS 35273693825, next:2041445 invalid dropping st:0

However, even with those messages the "good" source files end up producing a resultant file that works perfectly fine.

I've used a bunch of different versions of ffmpeg.  Currently I'm using version 6.0 on Ubuntu 22.04.

I've tried literally dozens of variations of options to try to get good output from the "bad" source files (all sorts of fflags like genpts, igndts, ignidx, discardcorrupt and options like use_wallclock_as_timestamps, avoid_negative_ts, etc) and I just can't seem to get anywhere.  I figured I'd just post the data here and see if anyone has suggestions or a solution.  Any help would be greatly appreciated.

-Davison Long

As a follow-up, I've resolved this issue by extracting the raw bitstreams for video and audio and then recombining them.  I still haven't figured out what is actually wrong with the "bad" files that causes the issue, but the below commands work to produce a good video from the bad source files.  I've included a command that produces a good resultant MP4 as well as a command that takes the raw bitstreams to concurrently speed up and encode in h265/HEVC.

# Extract and concat video and audio to separate bitstream files
ffmpeg -safe 0 -f concat -i <(find /mnt/input/bad_source_files -type f -name '*.mp4' -printf "file '%p'\n" | sort) -map 0:v -c:v copy -f h264 /mnt/output/intermediate-video.h264 -map 0:a -c:a copy /mnt/output/intermediate-audio.aac

# Recombine video and audio into working AVC MP4
ffmpeg -r 15 -i /mnt/output/intermediate-video.h264 -i /mnt/output/intermediate-audio.aac -c:v copy -c:a copy /mnt/test/working/output/t1/fixed_bad_video.mp4

# Recombine video and audio (and speed up 25x) and encode in HEVC MKV
ffmpeg -r 15 -i /mnt/output/intermediate-video.h264 -i /mnt/output/intermediate-audio.aac -filter_complex "[0:v]setpts=0.04*PTS[v];[1:a]atempo=25.0[a]" -map "[v]" -map "[a]" -c:v libx265 -vtag hvc1 -crf 31 /mnt/test/working/output/t1/fixed_bad_video.mkv


More information about the ffmpeg-user mailing list