I am trying to concatenate ac3 and aac using ffmpeg.
ffprobe of jpn_op.m4a:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'jpn_op.m4a':
Metadata:
major_brand : M4A
minor_version : 512
compatible_brands: M4A isomiso2
encoder : Lavf58.76.100
Duration: 00:01:17.28, start: 0.000000, bitrate: 224 kb/s
Stream #0:0(jpn): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, stereo, fltp, 224 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Side data:
audio service type: main
ffprobe of eng_nop.m4a
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'eng_nop.m4a':
Metadata:
major_brand : M4A
minor_version : 512
compatible_brands: M4A isomiso2
encoder : Lavf58.76.100
Duration: 00:22:09.26, start: 0.023000, bitrate: 41 kb/s
Stream #0:0(und): Audio: aac (HE-AAC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 39 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
I tried this:
ffmpeg -f concat -i concat_files.txt -c copy final.m4a -y
concat_files.txt contains:
file 'jpn_op.m4a'
file 'eng_nop.m4a'
ffprobe of final.m4a
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'final.m4a':
Metadata:
major_brand : M4A
minor_version : 512
compatible_brands: M4A isomiso2
encoder : Lavf58.76.100
Duration: 00:23:26.50, start: 0.000000, bitrate: 51 kb/s
Stream #0:0(jpn): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, stereo, fltp, 224 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Side data:
audio service type: main
But, there is silent in place of 2nd file in final.m4a
Formats and attributes must be the same for concat demuxer to work. Re-encode one of the files to match the other then concatenate.
Or use the concat filter instead:
ffmpeg -i jpn_op.m4a -i eng_nop.m4a -filter_complex "[0:a][1:a]concat=n=2:v=0:a=1" final.mp4
Related
ffmpeg -framerate 1 -loop -1 -i image.jpg -i audio.wav -shortest out.mp4
Using ffmpeg version 2022-02-24-git-8ef03c2ff1-full_build-www.gyan.dev
Here is the ffprobe for audio.wav
Input #0, wav, from 'audio.wav': Duration: 00:00:20.21, bitrate:
1411 kb/s Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001),
44100 Hz, 2 channels, s16, 1411 kb/s
and out.mp4 (note that the duration is 1:20, instead of the 20 second duration of the audio file).
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'out.mp4': Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf59.17.102 Duration: 00:01:20.00, start: 0.000000, bitrate: 156 kb/s Stream #0:00x1: Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuvj444p(pc,
bt470bg/unknown/unknown, progressive), 1200x1443 [SAR 1:1 DAR
400:481], 120 kb/s, 1 fps, 1 tbr, 16384 tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0] Stream #0:10x2: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 139 kb/s
(default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
In my case I have 3 .webm files the first one is audio only, the second one is video only, the third one is audio and video.
I want to concatenate them into a single file which shows black screen for audio only parts, video for video only parts, and plays both for the parts that have audio and video.
The video codec is VP8, the audio codec is Opus.
concat.txt contains the entries for the three files
I am using the following command to concatenate them.
ffmpeg -f concat -safe 0 -i ./concat.txt -c copy -y output.webm
This command creates the output file, when I play it it only plays the first audio only part and crashes when it reaches the video only part.
I also tried to add a dummy picture to the audio only files but the command fails when I try to concatenate.
Any and all help/critique is welcome.
Thank you!
More Info On the Input files
Input #0, matroska,webm, from 'original1.webm':
Metadata:
title : -
ENCODER : Lavf58.45.100
Duration: 00:00:09.99, start: 0.000000, bitrate: 34 kb/s
Stream #0:0: Audio: opus, 48000 Hz, stereo, fltp (default)
Metadata:
DURATION : 00:00:09.990000000
Input #1, matroska,webm, from 'original2.webm':
Metadata:
title : -
ENCODER : Lavf58.45.100
Duration: 00:00:09.75, start: 0.000000, bitrate: 954 kb/s
Stream #1:0: Video: vp8, yuv420p(tv, bt470bg/unknown/unknown, progressive), 1680x1050, SAR 1:1 DAR 8:5, 1k tbr, 1k tbn, 1k tbc (default)
Metadata:
DURATION : 00:00:09.754000000
Input #2, matroska,webm, from 'original3.webm':
Metadata:
title : -
ENCODER : Lavf58.45.100
Duration: 00:00:10.02, start: 0.000000, bitrate: 912 kb/s
Stream #2:0: Audio: opus, 48000 Hz, stereo, fltp (default)
Metadata:
DURATION : 00:00:10.023000000
Stream #2:1: Video: vp8, yuv420p(tv, bt470bg/unknown/unknown, progressive), 1680x1050, SAR 1:1 DAR 8:5, 1k tbr, 1k tbn, 1k tbc (default)
Metadata:
DURATION : 00:00:09.965000000
All files to be concatenated must have the same attributes and stream order.
Add black video to audio only file:
ffmpeg -i audio.webm -f lavfi -i color=s=1680x1050 -r 1000 -map 0 -map 1 -c:a copy -c:v libvpx -shortest output1.webm
Add silent audio to video only file:
ffmpeg -f lavfi -i anullsrc=r=48000:cl=stereo -i video.webm -map 0 -map 1 -c:a libopus -c:v copy -shortest output2.webm
Make input.txt with the following contents:
file 'output1.webm'
file 'output2.webm'
file 'original3.webm'
Concatenate with the concat demuxer:
ffmpeg -f concat -safe 0 -i concat.txt -c copy output.webm
I am trying to concatenate multiple files together using ffmpeg concatenate demuxer. However final video is out of sync.
First parts are coming from static image which are converted to 5-seconds video in the following way:
ffmpeg -r 30 -i 1.png -vf loop=loop=150:size=1:start=0 -pix_fmt yuv420p -c:v libx264 -preset superfast -tune stillimage loop.mp4
Then I add silent audio stream:
ffmpeg -i loop.mp4 -f lavfi -i anullsrc -map 0:v -map 1:a -ar 44100 -ac 2 -c:v copy -c:a aac -shortest silent.mp4
ffrobe for the silent.mp4:
ffprobe -v quiet -show_entries stream=start_time,duration silent.mp4
[STREAM]
start_time=0.000000
duration=5.733333
[/STREAM]
[STREAM]
start_time=0.000000
duration=5.665011
[/STREAM]
Which already shows that audio and video streams have different duration.
Then I prepare concat input file, last line is the video with same dimension and framerate, also it has existing audio stream (44.1kHz, stereo)
file silent.mp4
file silent.mp4
... (multiple lines, say 10)
file silent.mp4
file video.mp4
To make sure that inputs have same parameters:
ffmpeg -i silent.mp4 -i video.mp4
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'silent.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:05.03, start: 0.000000, bitrate: 32 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 19 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 2 kb/s (default)
Metadata:
handler_name : SoundHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:43.03, start: 0.000000, bitrate: 1622 kb/s
Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 1484 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
Now, after concatenation final video is out of sync with audio (audio stream of the last part starts before static part is finished)
ffmpeg -f concat -i concat.txt -c copy result.mp4 (no warnings)
I have tried to pad audio stream with silence in both loop.mp4 and video.mp4, but it did not help - it randomly modifies duration and start time of both video and audio streams and audio is again out of sync after concat.
Also, I am not able to increase the duration of static part (so that number of entries in concat.txt can be decreased) because each static part can be different - this is just an example.
I have an mp4 file like this(same format but longer):
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'N1.2.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42mp41
creation_time : 2018-10-31T13:44:21.000000Z
Duration: 00:28:54.21, start: 0.000000, bitrate: 10295 kb/s
Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 9972 kb/s, 50 fps, 50 tbr, 50k tbn, 100 tbc (default)
Metadata:
creation_time : 2018-10-31T13:44:21.000000Z
handler_name : ?Mainconcept Video Media Handler
encoder : AVC Coding
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 317 kb/s (default)
Metadata:
creation_time : 2018-10-31T13:44:21.000000Z
handler_name : #Mainconcept MP4 Sound Media Handler
I also have another video file that is 3 minutes long. and has no audio. What is the fastest way to encode the other video in a way that it is encoded like my main video and then replace the last three minutes of the video track of my original video with this?
In other words.
I have video A that is 1 hour long. With the encoding shown above.
I have video B that is 3 minutes long with no audio. with a random encoding.
I want to have video C with the same encoding and same audio as A. But it's video track would be the first 57 minutes of A + B(which is 3 minutes).
I want to do this as fast as possible so I would like to not re encode A.
I know how to concatenate two videos, I use this command:
ffmpeg -f concat -i files.txt -c copy res.mp4
Make end video using parameters of main video:
ffmpeg -i videob.mp4 -f lavfi -i anullsrc=sample_rate=48000:channel_layout=stereo -filter_complex "[0:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1,format=yuv420p,fps=50[v]" -map "[v]" -map 1:a -c:v libx264 -profile:v main -c:a aac -video_track_timescale 50000 -shortest videob2.mp4
Get duration of main video:
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 main.mp4
Make files.txt which is needed for concat demuxer:
file 'main.mp4'
outpoint 3420
file 'videob2.mp4'
In this example outpoint is main video duration minus end video duration.
Concatenate:
ffmpeg -f concat -i files.txt -i main.mp4 -map 0:v -map 1:a -c copy -shortest output.mp4
I have 3 files: video.mp4, audionl.mp4 & audioeng.mp4.
I tried adding them all together in 1 output.mp4 file like this:
ffmpeg -y -i video.mp4 -i audionl.mp4 -i audioeng.mp4 -map 0:v -map 1:a -map 2:a -metadata:s:a:0 language=nl -metadata:s:a:0 title="NL" -metadata:s:a:1 language=eng -metadata:s:a:1 title="ENG" -acodec copy -bsf:a aac_adtstoasc -vcodec copy -movflags faststart output.mp4 2>&1
This is the output I see from ffmpeg:
ffmpeg version 2.6.9 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.9.2 (Debian 4.9.2-10)
configuration: --prefix=/usr --extra-cflags='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security ' --extra-ldflags='-Wl,-z,relro' --cc='ccache cc' --enable-shared --enable-libmp3lame --enable-gpl --enable-nonfree --enable-libvorbis --enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc --enable-x11grab --enable-libgsm --enable-libtheora --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libspeex --enable-nonfree --disable-stripping --enable-libvpx --enable-libschroedinger --disable-encoder=libschroedinger --enable-version3 --enable-libopenjpeg --enable-librtmp --enable-avfilter --enable-libfreetype --enable-libvo-aacenc --disable-decoder=amrnb --enable-libvo-amrwbenc --enable-libaacplus --libdir=/usr/lib/x86_64-linux-gnu --disable-vda --enable-libbluray --enable-libcdio --enable-gnutls --enable-frei0r --enable-openssl --enable-libass --enable-libopus --enable-fontconfig --enable-libpulse --disable-mips32r2 --disable-mipsdspr1 --disable-mipsdspr2 --enable-libvidstab --enable-libzvbi --enable-avresample --disable-htmlpages --disable-podpages --enable-libutvideo --enable-libfdk-aac --enable-libx265 --enable-libiec61883 --enable-vaapi --enable-libdc1394 --disable-altivec --shlibdir=/usr/lib/x86_64-linux-gnu
libavutil 54. 20.100 / 54. 20.100
libavcodec 56. 26.100 / 56. 26.100
libavformat 56. 25.101 / 56. 25.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 11.102 / 5. 11.102
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 3.100 / 53. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
Metadata:
major_brand : isom
minor_version : 0
compatible_brands: isomavc1mp42dash
creation_time : 2017-06-04 06:35:06
Duration: 00:20:52.80, start: 0.000000, bitrate: 1172 kb/s
Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 1168 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc (default)
Metadata:
creation_time : 2017-06-04 06:35:06
handler_name : VideoHandler
encoder : VideoHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'audionl.mp4':
Metadata:
major_brand : isom
minor_version : 0
compatible_brands: isomM4A mp42dash
creation_time : 2017-06-04 06:35:06
Duration: 00:20:52.80, start: 0.000000, bitrate: 96 kb/s
Stream #1:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 93 kb/s (default)
Metadata:
creation_time : 2017-06-04 06:35:06
handler_name : SoundHandler
Input #2, mov,mp4,m4a,3gp,3g2,mj2, from 'audioeng.mp4':
Metadata:
major_brand : isom
minor_version : 0
compatible_brands: isomM4A mp42dash
creation_time : 2017-06-04 06:35:06
Duration: 00:20:52.78, start: 0.000000, bitrate: 64 kb/s
Stream #2:0(eng): Audio: aac (HE-AAC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 62 kb/s (default)
Metadata:
creation_time : 2017-06-04 06:35:06
handler_name : SoundHandler
Output #0, mp4, to 'output.mp4':
Metadata:
major_brand : isom
minor_version : 0
compatible_brands: isomavc1mp42dash
encoder : Lavf56.25.101
Stream #0:0(eng): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 640x360 [SAR 1:1 DAR 16:9], q=2-31, 1168 kb/s, 25 fps, 25 tbr, 90k tbn, 90k tbc (default)
Metadata:
creation_time : 2017-06-04 06:35:06
handler_name : VideoHandler
encoder : VideoHandler
Stream #0:1(nl): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 93 kb/s (default)
Metadata:
creation_time : 2017-06-04 06:35:06
handler_name : SoundHandler
title : NL
Stream #0:2(eng): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 62 kb/s (default)
Metadata:
creation_time : 2017-06-04 06:35:06
handler_name : SoundHandler
title : ENG
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #1:0 -> #0:1 (copy)
Stream #2:0 -> #0:2 (copy)
Press [q] to stop, [?] for help
[mp4 # 0xbb1320] Starting second pass: moving the moov atom to the beginning of the file
frame=31320 fps=20993 q=-1.0 Lsize= 204217kB time=00:20:52.79 bitrate=1335.4kbits/s
video:178734kB audio:23867kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.797284%
As you can see in there: "Stream #0:0(eng)", "Stream #0:1(nl)" & "Stream #0:2(eng)".
So that looks fine.
But when I open it in VLC, I see under Audio tracks:
NL - [English]
ENG - [English]
So I went back to check on ffmpeg with "ffmpeg -i output.mp4":
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.25.101
Duration: 00:20:52.80, start: 0.000000, bitrate: 1335 kb/s
Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 1168 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 93 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream #0:2(eng): Audio: aac (HE-AAC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 62 kb/s (default)
Metadata:
handler_name : SoundHandler
And here they are English again...
Afterwards I tried this:
ffmpeg -y -i audionl.mp4 -c:a copy -metadata:s:a:0 language=nl output.mp4
But also that failed to set the correct language on the output file...
I'm at a loss here and hope someone can help me set these languages correctly.
Thanks a lot!
FFmpeg expects ISO 639-2 codes for languages, so for Dutch, it's either
-metadata:s:a:0 language=dut
or
-metadata:s:a:0 language=nld