Can't use -shortest parameter when using multiple audio streams ffmpeg - audio

I want to add a second audio stream to an mp4 video file already containing sound.
The second audio stream is a little longer than the video, but I want the final product to be the same length.
I tried using the -shortest feature but the second audio stream I wanted to add want not audible at all.
I think -shortest only allows for one stream, so what can I do to keep the video the same length and keep both audio streams?
Here is the full command I used before asking this question:
ffmpeg -i input.mp4 -i input.m4a -map 0:v -map 0:a -shortest output.mp4
Output of 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 : Lavf58.45.100
Duration: 00:00:32.08, start: 0.000000, bitrate: 1248 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x600 [SAR 1:1 DAR 4:5], 1113 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler

You have to map the audio from the 2nd input as well.
ffmpeg -i input.mp4 -i input.m4a -map 0:v -map 0:a -map 1:a -shortest -fflags +shortest -max_interleave_delta 100M output.mp4
See https://stackoverflow.com/a/55804507/ for explanation of how to make shortest effective.

Related

FFMPEG: flac audio file duration in metadata is 0

I have a flac audio file with incorrect duration in the metadata. The audio has a length of 55 minutes but the metadata has a duration of 0. Is it possible to use an ffmpeg command to fix this?
Below are the outputs of an ffprobe:
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Audio: flac, 44100 Hz, stereo, s16
The FLAC header is missing or incomplete. Re-encoding will re-write it:
ffmpeg -i input.flac -c:v copy -c:a flac output.flac
There will be no quality loss as FLAC is lossless.

Having audio sync issues after concatenating several mp4 with and without sound

I'm posting today because I have a problem when I use ffmpeg. I have a small program that creates mp4 files for me, often with sounds and sometimes without, until now I used ffmpeg to concatenate them.
But I just realized that if I have an mp4 file that doesn't contain audio then the whole audio track of the final file goes out of sync. This only happens when I have some mp4 files that don't have audio. I think it's also useful to know that the program I'm using gives me a lot of mp4 (>20) and I can't know in advance which ones have audio or not...
Here is the code I use, I saw on the forum that the mp4 format was badly managed and that it was better to use ts format for concatenation that's why I do this
for f in $(ls *.mp4); do
ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
done
CONCAT=$(echo $(ls *.ts ) | sed -e "s/ /|/g")
ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -i "concat:$CONCAT" -c copy output.ts
ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -i output.ts -acodec copy -vcodec copy output.mp4
I noticed that when the final file goes out of sync this error appears :
Input #0, mpegts, from 'concat:video0.mp4.ts|video01.mp4.ts|video012.mp4.ts|video0123.mp4.ts|video01234.mp4.ts|video012345.mp4.ts|video0123456.mp4.ts|video01234567.mp4.ts|video012345678.mp4.ts|video0123456789.mp4.ts|video012345678910.mp4.ts|video01234567891011.mp4.ts|video0123456789101112.mp4.ts|video012345678910111213.mp4.ts|video01234567891011121314.mp4.ts|video0123456789101112131415.mp4.ts|video012345678910111213141516.mp4.ts|video01234567891011121314151617.mp4.ts|video0123456789101112131415161718.mp4.ts|video012345678910111213141516171819.mp4.ts|zzzz.mp4.ts':
Duration: 00:00:05.02, start: 1.420222, bitrate: 63089 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709, progressive), 640x1136, 30 fps, 240 tbr, 90k tbn, 60 tbc
Stream #0:1[0x101](und): Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, mono, fltp, 145 kb/s
Output #0, mpegts, to 'output.ts':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709, progressive), 640x1136, q=2-31, 30 fps, 240 tbr, 90k tbn, 90k tbc
Stream #0:1(und): Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, mono, fltp, 145 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[mpegts # 0x7f9c68800000] DTS 130180 < 573000 out of order <-- HERE
[mpegts # 0x7f9c68800000] DTS 130180 < 654000 out of order <-- HERE
[mpegts # 0x7f9c68800000] DTS 130180 < 1477180 out of order <-- HERE
I've tried several different combinations of members answer on other topics but nothing worked for me.
I don't know if the problem comes from the moment I convert .mp4 files to .ts and I forget to set appropriate codec, or if it comes from the concat command ?
I'm almost sure that there is a simple way to fix this issue but unfortunately I don't have enough ffmpeg knowledge.
Thanks for your help :)
EDIT 1 :
I changed the for loop of the programme to :
for f in $(ls *.mp4); do
TEST=$(ffprobe -i $f -show_streams -select_streams a -loglevel error)
if [ "$TEST" ]; then
ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
fi
done
It only makes the script to ignore the files that don't have audio, but this way it works, so maybe adding dummy audio to those files could make the job as #Gyan suggested in the comments, but how to do that ?
EDIT 2 SOLVED
I found how to add dummy audio on my silent file, that solved my problem this is my final working for loop :
for f in $(ls *.mp4); do
TEST=$(ffprobe -i $f -show_streams -select_streams a -loglevel error)
if [ "$TEST" ]; then
ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
else
ffmpeg -f lavfi -i anullsrc -i $f -shortest -c:v copy -c:a aac -map 0:a -map 1:v $f.a.ts
ffmpeg -i $f.a.ts -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
rm $f.a.ts
fi
done

How to get separated audio channel from 5.1 video with ffmpeg?

I have a video file with audio stream:
Stream #0:1(eng): Audio: ac3, 48000 Hz, 5.1(side), fltp, 448 kb/s
Stream #0:2(eng): Audio: ac3, 48000 Hz, 5.1(side), fltp, 448 kb/s
And I can do mixdown it to two-channel audio:
ffmpeg -i vid.mkv -map 0:2 -acodec libmp3lame -y output.mp3
Update
It can be splitted by the channelsplit command, but it splits the Stream #0:1 by default and I can't specify Stream #0:2 channel.
./ffmpeg -i vid.mkv -vn -filter_complex 'channelsplit=channel_layout=5.1[FC]' -map '[FC]' front_center.mp3
How do I specify Stream #0:2 5.1 channel and get separated front center channel from this video?

ffmpeg - mux video and audio and trim the audio

I have a long audio part
and a short video part which I want to mux together.
I'm trying the following command to mux:
Video_0-0002.h264 - whole file (2 secs long)
Audio.wav - from 4 till 6 seconds
ffmpeg -y -i /Documents/viz-1/01/Video_0-0002.h264 -i /Documents/viz-1/01/Audio.wav -codec:v copy -f mp4 -af atrim=4:6 -strict experimental -movflags faststart /Documents/viz-1/01/Video_0-0001.mp4
But the audio is messed up...
how can I do it correctly?
Also tried, sounds like there is silence in the end.
ffmpeg -y -i Video_0-0003.h264 -i Audio.wav -c:v copy -af atrim=6:8,asetpts=PTS-STARTPTS -strict experimental -movflags +faststart Video_0-0003.mp4
Input #0, h264, from 'Video_0-0003.h264':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: h264 (Main), yuv420p(progressive), 388x388 [SAR 1:1 DAR 1:1], 30 fps, 30 tbr, 1200k tbn, 60 tbc
Guessed Channel Layout for Input Stream #1.0 : stereo
Input #1, wav, from 'Audio.wav':
Duration: 00:00:16.98, bitrate: 1411 kb/s
Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
Output #0, mp4, to 'Video_0-0003.mp4':
Metadata:
encoder : Lavf57.56.100
Stream #0:0: Video: h264 (Main) ([33][0][0][0] / 0x0021), yuv420p(progressive), 388x388 [SAR 1:1 DAR 1:1], q=2-31, 30 fps, 30 tbr, 1200k tbn, 1200k tbc
Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc57.64.101 aac
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
[mp4 # 0x7fca8f015000] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
[mp4 # 0x7fca8f015000] Starting second pass: moving the moov atom to the beginning of the file
frame= 60 fps=0.0 q=-1.0 Lsize= 242kB time=00:00:02.02 bitrate= 982.2kbits/s speed= 21x
video:207kB audio:32kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.382400%
[aac # 0x7fca8f017400] Qavg: 1076.270
Try
ffmpeg -y -i /Documents/viz-1/01/Video_0-0002.h264 -i /Documents/viz-1/01/Audio.wav -c:v copy -af atrim=4:6,asetpts=PTS-STARTPTS -strict experimental -movflags +faststart /Documents/viz-1/01/Video_0-0001.mp4
You can try to cut audio by video timing, and then marge video and audio track.
Use -vn and -an in separate ffmpeg process.
ffmpeg -i video.mp4 -c:v h264 -an -y video.h264
ffmpeg -i video.mp4 -c:a aac -t 00:01:00 -vn -y audio.aac
And for marge tracks:
ffmpeg -i auido.acc -i video.h264 -c:v copy -c:a copy -f mp4 -y out.mp4

sound acceleration while converting sound with ffmpeg libfaac

i'm trying to convert sound with ffmpeg using comand:
fmpeg -y -i /Users/Artem/Sites/waprik/testing/orig.mp4 -acodec libfaac -b:a 64k -ar 41000 -ac 2 -threads 0 -vn /Users/Artem/Sites/waprik/public/testing.m4a
original sound is 4:18 min, but output sound duration is 4 minutes and it sounds accelerated. How can i fix it ?
by the way, original sound is
Duration: 00:04:18.81
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 191 kb/s
Metadata:
creation_time : 2014-03-07 05:45:06
handler_name : IsoMedia File Produced by Google, 5-11-2011
you mistyped the audio rate. It should be 44100 instead of 41000:
ffmpeg -y -i /Users/Artem/Sites/waprik/testing/orig.mp4 -acodec libfaac -b:a 64k -ar 41000 -ac 2 -threads 0 -vn /Users/Artem/Sites/waprik/public/testing.m4a
Here's the math to prove it! Your initial track is 4 minutes 18 seconds, or 258 seconds. The ratio of your conversion rate to actual rate is 41000/44100, or .9297052. Multiply that ratio by your 258-second track and we end up with a 239.86-second track...or 3 minutes 59.86 seconds.
What was happening is that you were telling ffmpeg that instead of 44100 frames in a second, there were actually only 41000. So, it grabbed 41000 of the 44100 and called that a second, even though it really wasn't. The result is that you end up with a faster/shorter, slightly degraded audio file.

Resources