I concatenated multiple videos using the following ffmpeg command:
ffmpeg -i video1.avi video2.avi -f concat -c copy -safe 0 -o concat.mov
Are there any chance that original files could be splitted back from concat.mov? Since this is a concat and copy, are there any markers in concat.mov that I could utilize?
Update: original video codecs:
Input #0, avi, from 'DVR___2017-08-10_09.17.56.AVI':
Metadata:
encoder : DVR ZIR32 SW: 1.1.001
Duration: 00:20:00.00, start: 0.000000, bitrate: 897 kb/s
Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj422p(pc, bt470bg/unknown/unknown), 640x360 [SAR 1:1 DAR 16:9], 703 kb/s, 5 fps, 5 tbr, 5 tbn, 5 tbc
Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 12000 Hz, mono, s16, 192 kb/s
Concatenated file:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'DVR___2017-08-07_19.55.36.AVI.mov':
Metadata:
major_brand : qt
minor_version : 512
compatible_brands: qt
encoder : Lavf57.71.100
Duration: 00:52:58.80, start: 0.000000, bitrate: 875 kb/s
Stream #0:0(eng): Video: mjpeg (jpeg / 0x6765706A), yuvj422p(pc, bt470bg/unknown/unknown), 640x360 [SAR 1:1 DAR 16:9], 682 kb/s, 5 fps, 5 tbr, 10240 tbn, 10240 tbc (default)
Metadata:
handler_name : DataHandler
Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 12000 Hz, mono, s16, 192 kb/s (default)
Metadata:
handler_name : DataHandler
Related
Env: Windows7, ffmpeg 4.2.2
copy the first 10 seconds from long.mp4 to UNO.mp4
copy sec 120 to sec 130 from long.mp4 to DUE.mp4
create List.txt with
file 'C:\Users\Admin\Videos\UNO.mp4'
file 'C:\Users\Admin\Videos\DUE.mp4'
here the ffprobe output of the two files:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'UNO.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:10.04, start: 0.000000, bitrate: 671 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt709), 640x360 [SAR 1:1 DAR 16:9], 536 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : ISO Media file produced by Google Inc. Created on: 09/18/2020.
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : ISO Media file produced by Google Inc. Created on: 09/18/2020.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'DUE.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:11.96, start: 0.000000, bitrate: 657 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt709), 640x360 [SAR 1:1 DAR 16:9], 524 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : ISO Media file produced by Google Inc. Created on: 09/18/2020.
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s (default)
Metadata:
handler_name : ISO Media file produced by Google Inc. Created on: 09/18/2020.
use the two files as input to
ffmpeg -f concat -safe 0 -i "C:\Users\Admin\Videos\List.txt" -c copy "C:\Users\Admin\Videos\mergedVideo.mp4"
it outputs:
mov,mp4,m4a,3gp,3g2,mj2 # 00be7f80] Auto-inserting h264_mp4toannexb bitstream filter
nput #0, concat, from 'C:\Users\Admin\Videos\List.txt':
Duration: N/A, start: 0.000000, bitrate: 825 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 624x352 [SAR 1:1 DAR 39:22], 697 kb/s, 25 fps, 25
tbr, 90k tbn, 50 tbc
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
handler_name : SoundHandler
utput #0, mp4, to 'C:\Users\Admin\Videos\mergedVideo.mp4':
Metadata:
encoder : Lavf58.29.100
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 624x352 [SAR 1:1 DAR 39:22], q=2-31, 697 kb/s, 25
fps, 25 tbr, 90k tbn, 90k tbc
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
handler_name : SoundHandler
tream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
then a long series of :
mp4 # 00c43c40] Non-monotonous DTS in output stream 0:0; previous: 899370, current: 720000; changing to 899371. This may result in incorrect timestamps in the output file.
at end as last messages :
rame= 552 fps=528 q=-1.0 Lsize= 2220kB time=00:00:20.01 bitrate= 908.4kbits/s speed=19.2x
ideo:1873kB audio:329kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.827539%
When i play the output mergedVideo.mp4 with ffplay, it does not gove errors
it is only a time stamp problem, or some frames or some audio are lost ?
in the latter case, is there a parameter to correct the problem, or i have to re-encode ?
(to further test, i tried with avidemux an it concats UNO.mp4 and DUE.mp4 without re-encoding)
Thanks in advance to all for the support
Maurizio
First of all, unless you call FFmpeg with the -xerror option (which you didn't) "Non-monotonous DTS..." message is not an error. It's just a warning. It is just informing what it's doing for you, and thankfully its automatic adjustment indeed worked for your case base on your description.
It is making the adjustment because you are concatenate-and-copying streams, of which data frame carries timestamp (unlike WAV or MPEG streams). The concat demuxer likely cannot adjust the timestamp in the stream data on the fly (to avoid the warnings/corrections from the main ffmpeg program).
The bottom line. If the warning messages must go, change the log level to ignore warning: -loglevel error.
Reference ffmpeg.c Lines 817-834
My procedure is as follows:
convert the videos to 1920x1080 at 60 FPS (some videos had only 30 FPS)
save the converted videos in a text file
merging the video by an FFMPEG concat
After the videos are merged, the audio is out of sync with the video.
To convert the videos I use the following command: ffmpeg -i input.mp4 -vf scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:-1:-1,setsar=1 -r 60 output.mp4
(got it from here: How can I upscale videos with FFmpeg to a fixed resolution?)
My FFMPEG command to concat the videos: ffmpeg -f concat -safe 0 -i videolist.txt -c copy final.mp4
When I concatonate the videos, I sometimes get a lot of these warnings:
[mp4 # 0x55740b62ad40] Non-monotonous DTS in output stream 0:1; previous: 336921, current: 336064; changing to 336922. This may result in incorrect timestamps in the output file.
A question for the FFMPEG professionals, how can I make sure that video and audio are in sync?
UPDATE:
Here are the details of the files being listed in the videolist.txt
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'intro.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.20.100
Duration: 00:00:07.04, start: 0.000000, bitrate: 291 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 187 kb/s, 60 fps, 60 tbr, 15360 tbn, 120 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 91 kb/s (default)
Metadata:
handler_name : SoundHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '4t6Brqa4E5BnD6vC5inICusEtPtd3nU88NgbmUJ.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.20.100
Duration: 00:00:37.66, start: 0.000000, bitrate: 1608 kb/s
Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 1532 kb/s, 60 fps, 60 tbr, 15360 tbn, 120 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 69 kb/s (default)
Metadata:
handler_name : SoundHandler
Input #2, mov,mp4,m4a,3gp,3g2,mj2, from 'cut.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.20.100
Duration: 00:00:00.65, start: 0.000000, bitrate: 7128 kb/s
Stream #2:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 7576 kb/s, 60 fps, 60 tbr, 15360 tbn, 120 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #2:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 115 kb/s (default)
Metadata:
handler_name : IsoMedia File Produced by Google, 5-11-2011
Input #3, mov,mp4,m4a,3gp,3g2,mj2, from '80SEh7RoTyVPNj3zTZSaixxATvUYXMNyzu15evs.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.20.100
Duration: 00:00:07.75, start: 0.000000, bitrate: 1191 kb/s
Stream #3:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 1106 kb/s, 60 fps, 60 tbr, 15360 tbn, 120 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #3:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 70 kb/s (default)
Metadata:
handler_name : SoundHandler
Input #4, mov,mp4,m4a,3gp,3g2,mj2, from 'outro.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.20.100
Duration: 00:00:15.17, start: 0.000000, bitrate: 3417 kb/s
Stream #4:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 3286 kb/s, 60 fps, 60 tbr, 15360 tbn, 120 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #4:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
All attributes must be the same, but your audio has varying channel layouts (stereo & mono) and sample rates (48000 & 44100).
Add -ar 48000 -ac 2 to your command:
ffmpeg -i input.mp4 -vf scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:-1:-1,setsar=1 -r 60 -ar 48000 -ac 2 output.mp4
Alternatively, if you want to avoid any temporary files you can do everything in one command using the concat filter as shown in How to concatenate videos in ffmpeg with different attributes?.
I'm trying to encode an m2ts (MPEG-2 Transport Stream) file to mp4 (H.264) and get the ssim value.
I did get some SSIM values, but the results were contrary to my expectations.
Are there wrong command options for ffmpeg?
Encoding and SSIM calculation commands
# encode
$ ffmpeg -hide_banner -fflags +discardcorrupt -i input.m2ts \
-c:v libx264 -crf <CRF> -preset:v medium \
-c:a copy -bsf:a aac_adtstoasc \
output_ff_crf-<CRF>.mp4
# calculate ssim
$ ffmpeg -hide_banner -i <A> -i <B> \
-lavfi "[0:v]settb=AVTB,setpts=PTS-STARTPTS[main];[1:v]settb=AVTB,setpts=PTS-STARTPTS[ref];[main][ref]ssim" \
-f null -
The results of the SSIM
(a) A=input.m2ts, B=input.m2ts, ssim=0.973266
(b) A=input.m2ts, B=output_ff_crf-0.mp4, ssim=0.813347
(c) A=input.m2ts, B=output_ff_crf-30.mp4, ssim=0.819897
(d) A=output_ff_crf-0.mp4, B=output_ff_crf-0.mp4, ssim=1.000000
(e) A=output_ff_crf-0.mp4, B=output_ff_crf-30.mp4, ssim=0.972911
(d)(e): These are what I expected.
(a): The files are the same, but ssim≠1.
(b)(c): SSIMs with CRF=0 and CRF=30 have almost the same value, although the image quality is different.
In the case of HandBrakeCLI
To determine if there was a problem with input.m2ts, I ran HandBrakeCLI with almost the same parameters as ffmpeg.
# encode
HandBrakeCLI --verbose --format av_mp4 --encoder x264 --quality <CRF> --x264-preset medium \
--aencoder copy \
--input input.m2ts --output output_hb_crf-<CRF>.mp4
# calculate ssim (same as ffmpeg)
$ ffmpeg -hide_banner -i <A> -i <B> \
-lavfi "[0:v]settb=AVTB,setpts=PTS-STARTPTS[main];[1:v]settb=AVTB,setpts=PTS-STARTPTS[ref];[main][ref]ssim" \
-f null -
(b') A=input.m2ts, B=output_hb_crf-0.mp4, ssim=0.999999
(c') A=input.m2ts, B=output_hb_crf-30.mp4, ssim=0.972886
(d') A=output_hb_crf-0.mp4, B=output_hb_crf-0.mp4, ssim=1.000000
(e') A=output_hb_crf-0.mp4, B=output_hb_crf-30.mp4, ssim=0.972886
It's all as I expected. (although A is not ssim=1.0)
Therefore, I don't see a problem with input.m2ts.
Informations about video files and tools
Results of the ffprobe
input.m2ts
[mpeg2video # 0x5655577c1680] Invalid frame dimensions 0x0.
Last message repeated 1 times
[mpegts # 0x5655577bd080] start time for stream 2 is not set in estimate_timings_from_pts
[mpegts # 0x5655577bd080] PES packet size mismatch
Input #0, mpegts, from 'input.m2ts':
Duration: 00:30:02.68, start: 39593.392600, bitrate: 19019 kb/s
Program 211
Stream #0:0[0x140]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv, bt709, top first), 1920x1080 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:1[0x141]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 255 kb/s
Stream #0:2[0x138]: Data: bin_data ([6][0][0][0] / 0x0006)
Unsupported codec with id 100359 for input stream 2
output_ff_crf-0.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output_ff_crf-0.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.83.100
Duration: 00:30:02.67, start: 0.000000, bitrate: 109301 kb/s
Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 109040 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 252 kb/s (default)
Metadata:
handler_name : SoundHandler
output_hb_crf-0.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output_hb_crf-0.mp4':
Metadata:
major_brand : mp42
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 2020-05-17T06:22:06.000000Z
encoder : HandBrake 1.1.0 2018042400
Duration: 00:30:02.22, start: 0.000000, bitrate: 109661 kb/s
Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 109405 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2020-05-17T06:22:06.000000Z
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 252 kb/s (default)
Metadata:
creation_time : 2020-05-17T06:22:06.000000Z
handler_name : Stereo
Tools
version
ffmpeg: 3.4.6-0ubuntu0.18.04.1
HandBrakeCLI: 1.1.0
ldd
$ ldd /usr/bin/ffmpeg
...
libx264.so.152 => /usr/lib/x86_64-linux-gnu/libx264.so.152 (0x00007efbf1f33000)
...
$ ldd /usr/bin/HandBrakeCLI
...
libx264.so.152 => /usr/lib/x86_64-linux-gnu/libx264.so.152 (0x00007efbfb38f000)
...
ffmpeg and HandBrakeCLI are using the same libx264.
I recorded the audio and video stream from a streaming with ffmpeg. when I go to re-encode the file using libx264, the video in the movements looks bad and grainy as in the image.
Where did I go wrong?
this is the code I used
ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4
and these are mediainfo of original file
Metadata:
major_brand : isom
minor_version : 1
compatible_brands: isom
creation_time : 2020-03-19T22:43:32.000000Z
Duration: 00:39:51.99, start: 0.000000, bitrate: 1300 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x1080 [SAR 3:2 DAR 16:9], Closed Captions, 1268 kb/s, 59.94 fps, 59.94 tbr, 90k tbn, 59.94 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(spa): Audio: aac (HE-AACv2) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 22 kb/s (default)
Metadata:
handler_name : SoundHandler
mediainfo of the file recoded
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:39:51.99, start: 0.000000, bitrate: 7924 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], Closed Captions, 7892 kb/s, 59.94 fps, 59.94 tbr, 60k tbn, 119.88 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(spa): Audio: aac (HE-AACv2) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 22 kb/s (default)
Metadata:
handler_name : SoundHandler
How can I recode the file without loss of quality? (sorry for my poor english)
I have recently converted many videos for web playback, but all videos from one "series" all show up in firefox as "Video can't be played because the file is corrupt." (They work for google chrome and VLC. Not for firefox or edge.) All files have been converted with this command:
ffmpeg -i "file.mkv" -vcodec h264 -movflags +faststart -map 0 -vf subtitles="file.mkv" "file.mp4"
ffmpeg output from one of the non-working files:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Episode_7.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.3.100
Duration: 00:23:53.52, start: -0.001333, bitrate: 1900 kb/s
Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p, 1920x1080 [SAR 1:1 DAR 16:9], 1763 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s (default)
Metadata:
handler_name : SoundHandler
ffmpeg output from a working file:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Episode_7.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.3.100
Duration: 00:23:40.06, start: 0.000000, bitrate: 1848 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 1710 kb/s, 23.81 fps, 23.81 tbr, 16k tbn, 47.62 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(jpn): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 131 kb/s (default)
Metadata:
handler_name : SoundHandler
Of a collection of over 100 files, only 12 episodes of a single series show up as corrupted in firefox. I see some differences in the information about the two files, but I don't know why one file works and the other does not. What do I need to tell ffmpeg to make the videos playable in firefox?
It's the 4:4:4 chroma sampling. Add -pix_fmt yuv420p to the output file.