How does MPG determine its default audio track? - ffmpeg

I have some mpg files that I transcoded from DVDs I bought a long time ago (maybe 20 years ago). ffprobe:
Input #0, mpeg, from 'da-orig.mpg':
Duration: 00:06:59.44, start: 0.044100, bitrate: 6354 kb/s
Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p(tv, progressive), 720x480 [SAR 8:9 DAR 4:3], Closed Captions, 31 fps, 59.94 tbr, 90k tbn
Side data:
cpb: bitrate max/min/avg: 7500000/0/0 buffer size: 1835008 vbv_delay: N/A
Stream #0:1[0x85]: Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s
Stream #0:2[0x83]: Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s
Stream #0:3[0x81]: Audio: ac3, 48000 Hz, mono, fltp, 192 kb/s
Stream #0:4[0x80]: Audio: ac3, 48000 Hz, mono, fltp, 192 kb/s
This shows there are 4 audio streams. When I play this file in VLC / QuickTime it seems that Audio Track 4 is the default. I'd like to understand how this is chosen. Is it something within the mpg container format or are players choosing the stream that has the lowest id (0x80) ?
More background, when I try to turn this into a mp4 file with the following command:
ffmpeg -i da-orig.mpg -c copy -map 0 da-copy.mp4
I get roughly the same size file, but the default audio track is stream #0:1[0x85].
What I want is an equivalent mp4 file (so the same audio track chosen).

as written in this guide
"The -map option is used to choose which streams from the input(s)
should be included in the output(s)."
in your case you have only one input so it would be -map 0
if you have 2 inputs and want video from one and audio from the other it would be -map 0:v -map 1:a
since your input is a container selecting the video would be -map 0:v
and the second audio stream would be -map 0:a:2
ffmpeg -i da-orig.mpg -map 0:v -c:v h264 -crf 17 -preset 'veryslow' -map 0:a:2 -c:a copy output.mp4
to answer to your comment the sequence can help you
after you rename your collection with sequential numbers 'collection-name_0000' then
ffmpeg -i collection_name_#04d.mpg -map 0:v -c:v h264 -crf 17 -preset 'veryslow' -map 0:a:2 -c:a copy output-#04d.mp4
this iterate through the videos if they have the same number of streams

Related

Concatenate no audio video with other videos containing audio,

I have a txt file which contains several videos:
file '01.mp4' # No audio, only video
file '02.mp4' # Video with audio
...
All my videos are using same video codecs:
Stream #0:0[0x1](und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 720x1280, 3167 kb/s, 30 fps, 30 tbr, 15360 tbn (default)
And all my video that contains audio are these codecs:
Stream #0:1[0x2](und): Audio: aac (HE-AAC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 43 kb/s (default)
I tried to add silent audio using:
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -i 01.mp4 -c:v copy -c:a aac -shortest 01-silentaudio.mp4
But when concatening all my videos, it results in no audio for all videos.
What should I do?
Maybe I can reencode all my video to a specific codec, including my video without sound, in order to have the same codecs for all? What do you think?
Thank you

ffmpeg can't transcode DVD ac3 audio stream, but VLC can play it

I'm attempting to transcode a DVD to a single MKV file. I've had success in the past with other DVDs, but I'm running into an error I haven't seen before.
First I concatenate the VOB files I want to transcode:
cat VTS_02_1.VOB VTS_02_2.VOB VTS_02_3.VOB > WMAV.VOB
ffprobe output:
$ ffprobe -analyzeduration 100M -probesize 100M WMAV.VOB Input #0, mpeg, from 'WMAV.VOB':
Duration: 01:05:19.42, start: 0.300300, bitrate: 5686 kb/s
Stream #0:0[0x1bf]: Data: dvd_nav_packet
Stream #0:1[0x1e0]: Video: mpeg2video (Main), yuv420p(tv, smpte170m, top first), 720x480 [SAR 32:27 DAR 16:9], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:2[0x80]: Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s
Unsupported codec with id 100357 for input stream 0
Then I run this command to transcode the file:
ffmpeg -analyzeduration 100M -probesize 100M \
-i WMAV.VOB \
-map 0:1 -map 0:2 \
-c:v libx264 -preset slow -tune film -crf 21 \
-c:a aac -b:a 192k \
wmav.mkv
However, when I include -c:a aac, I get thousands of errors like this:
Error while decoding stream #0:2: Error number -16976906 occurred
[ac3 # 000002bd24d8eec0] expacc 127 is out-of-range
[ac3 # 000002bd24d8eec0] error decoding the audio block
There doesn't seem to be any issue with the audio stream since it plays back fine in VLC. The transcode succeeds if I use -c:a copy.
What is causing this error and how could I fix the problem?

duration change after transcode ts

i have a problem about transcode with ffmpeg
i want to cover m3u8 to mp4, so i transcode every ts file first, and then concat them to a mp4, but i found that the duration will be bigger than source file.
source file is :
http://oc7iy3eta.bkt.clouddn.com/src_20.ts
after transcode, test file is:
http://oc7iy3eta.bkt.clouddn.com/test_20.ts
i use the command as bellow to change to 5fps, and 400k bitrate:
sudo ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -nostdin -y -v warning -i ./src_20.ts -threads 3 -movflags faststart -metadata:s:v rotate=0 -chunk_duration 520000 -video_track_timescale 25000 -pix_fmt yuv420p -copytb 1 -vcodec libx264 -b:v 400000 -minrate 400000 -maxrate 400000 -bufsize 500k -force_key_frames "expr:gte(t,n_forced*2)" -vsync 1 -r 5 -s 544*960 -acodec libfaac -async 1 ./test_20.ts
i use ffprobe command to see video info:
source file info:
Duration: 00:00:01.26, start: 28.346989, bitrate: 921 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Audio: aac ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 23 kb/s
Stream #0:1[0x101]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 544x960, 10.67 tbr, 90k tbn, 180k tbc
test file:
Input #0, mpegts, from 'test_20.ts':
Duration: 00:00:01.62, start: 1.576778, bitrate: 447 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 544x960, 5 fps, 5 tbr, 90k tbn, 10 tbc
Stream #0:1[0x101]: Audio: aac ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 5 kb/s
=======================================================================
question
so , we can see that the duration of src file is 1.26s , but after transcode, the test file is 1.62s.
why? can anybody help
I suggest you save the m3u8 to a single TS and then transcode that to MP4.
ffmpeg -i in.m3u8 -c copy src.ts
Your current command is transcoding each TS to CFR at half the rate but your source timestamps have some jitter, so due to PTS quantization, there will be a mismatch. A single file transcode will minimize it.

ffmpeg - whatsapp: video format not supported

I have two videos (.mp4) files. One uploads to whatsapp and another does not.
Using ffmpeg I checked their properties:
a) Properties of video which uploads:
Duration: 00:00:56.45, start: 0.148000, bitrate: 1404 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1080x1080, 1359 kb/s, 23.98 fps, 23.98 tbr, 90k tbn, 47.95 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: aac (HE-AACv2) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 47 kb/s (default)
Metadata:
handler_name : SoundHandler
At least one output file must be specified
b) video which does not upload to whatsapp (because its says format not supported)
Duration: 00:00:56.10, start: 0.000000, bitrate: 543 kb/s
Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p, 1080x1080 [SAR 1:1 DAR 1:1], 464 kb/s, 23.98 fps, 23.98 tbr, 23.98 tbn, 47.95 tbc
Stream #0:1: Audio: aac (LC) ([255][0][0][0] / 0x00FF), 48000 Hz, stereo, fltp, 56 kb/s
The difference in video I noticed:
(avc1 / 0x31637661) vs (H264 / 0x34363248)
1359 kb/s vs 464 kb/s
90k tbn vs 23.98 tbn
What can be the reason?
Also the second video is not being played in Android.
The link for the video is https://drive.google.com/open?id=0B4UM6vTHw4pyMExQQ1lxZGp0N2c
There are some options for a better compatibility:
ffmpeg -i broken.mp4 -c:v libx264 -profile:v baseline -level 3.0 -pix_fmt yuv420p working.mp4
With -profile:v baseline -level 3.0 you make the file more compatible with most older players, including WhatsApp ;). Although, this disables some advanced features.
-pix_fmt yuv420p is necessary to compile to baseline (YUV planar color space with 4:2:0 chroma subsampling).
Also, you can adjust other options as bitrate, framerate, audio, etc.
Source: H.264 docs
Copied from https://www.reddit.com/r/ffmpeg/comments/564kyc/ffmpeg_whatsapp_video_format_not_supported/?st=ivjxdi0v&sh=848ce7eb
ffmpeg -i brokenvideo.mp4 -c:v libx264 -c:a aac fixedvideo.mp4
Also had to apply this fix: FFMPEG (libx264) "height not divisible by 2"
This is worked for me in 2020
ffmpeg -i broken.mp4 -c:v libx264 -profile:v high -level 3.0 -pix_fmt yuv420p -brand mp42 fixed.mp4
I tried all previous commands and I got some errors. I was able to encode my video using this command and here is the explanation and why I set it up like this for a better compatibility:
ffmpeg -i input.mp4 \
-c:v libx264 -pix_fmt yuv420p \
-profile:v baseline -level 3.0 \
-vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -vb 1024k \
-acodec aac -ar 44100 -ac 2\
-minrate 1024k -maxrate 1024k -bufsize 1024k \
-movflags +faststart \
output.mp4
If your input contains AAC audio you can stream copy instead of re-encoding by changing -acodec aac -ar 44100 -ac 2 to -acodec copy to preserve the audio quality.
option
explanation
-vcodec libx264
Chooses video encoder libx264
-pix_fmt yuv420p
Ensures YUV 4:2:0 chroma subsampling for compatibility
-profile:v baseline
Set the encoding profile to baseline. Used primarily for low-cost applications that require additional data loss robustness
-level 3.0
Set the operating point level to 3.0 which is necessary to have compatibility with WhatsApp
-vf "pad=ceil(iw/2)*2:ceil(ih/2)*2"
If you get not divisible by 2 error see
-acodec aac
Chooses audio encoder aac
-minrate 1024k
set min bitrate tolerance to 1024k (in bits/s). It is of little use elsewise
-maxrate 1024k
set max bitrate tolerance to 1024k (in bits/s). Requires bufsize to be set
-bufsize 1024k
set rate-control buffer size to 1024k (in bits)
-movflags +faststart
enables fast start for streaming
Note about faststart
Normally, a MP4 file has all its metadata packets stored at the end of the file, in data units named atoms. The mdat atom is located before the moov atom. If the file is created by adding the -movflags faststart, the moov atom is moved at the beginning of the MP4 file. By using this option, the moov atom is located before the mdat atom. This allows video playback to begin before the file has been completely downloaded.
2023-01-22 I used the most upvoted answer format and it worked for video, but audio was not working on iPhones. Here's what worked for me:
ffmpeg -i broken.mp4 -c:v libx264 -profile:v baseline -level 3.0 -pix_fmt yuv420p -ac 2 working.mp4
I had to add -ac 2 because the audio format I had wasn't seen as stereo by iOS.

ffmpeg keeps converting second ac3 audio stream to mp2

I'm new to this forum, and hope someone can help.
I am trying to add a second audio track (chinese.ac3) to an XVID video (vts_01.avi) that already has an ac3 track.
These audio tracks are encoded to ac3, 48000 Hz, stereo, 128 kb/s, and I would like to keep them that way -- just multiplex the streams without transcoding.
This is the command I am using:
ffmpeg -i vts_01.avi -vcodec copy -i Chinese.ac3 -acodec copy -map 0:0 -map 0:1 -map 1:0 muxed2.avi -newaudio
ffmpeg does its work, except for the fact that it converts the second track to mp2, 64 kb/s.
Here is a relevant excerpt from the output, where the key part is 'Stream #0.2: Audio: mp2, 48000 Hz, stereo, s16, 64 kb/s' where I would like 'Stream #0.2: Audio: ac3, 48000 Hz, stereo, 128 kb/s':
Input #0, avi, from 'vts_01.avi':
Metadata:
encoder : Lavf53.5.0
Duration: 02:03:26.40, start: 0.000000, bitrate: 1954 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 720x352 [PAR 1:1 DAR 45:22], 23.98 tbr,
23.98 tbn, 23.98 tbc
Stream #0.1: Audio: ac3, 48000 Hz, stereo, s16, 128 kb/s
[ac3 # 018A7440] max_analyze_duration 5000000 reached at 5024000
[ac3 # 018A7440] Estimating duration from bitrate, this may be inaccurate
Input #1, ac3, from 'Chinese.ac3':
Duration: 02:03:26.36, start: 0.000000, bitrate: 128 kb/s
Stream #1.0: Audio: ac3, 48000 Hz, stereo, s16, 128 kb/s
File 'muxed2.avi' already exists. Overwrite ? [y/N] y
Output #0, avi, to 'muxed2.avi':
Metadata:
ISFT : Lavf53.5.0
Stream #0.0: Video: mpeg4, yuv420p, 720x352 [PAR 1:1 DAR 45:22], q=2-31, 23.
98 tbn, 23.98 tbc
Stream #0.1: Audio: ac3, 48000 Hz, stereo, 128 kb/s
Stream #0.2: Audio: mp2, 48000 Hz, stereo, s16, 64 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Stream #0.1 -> #0.1
Stream #1.0 -> #0.2
I have tried a number of different combinations, including explicitly forcing the stream to ac3 with:
ffmpeg -i vts_01.avi -vcodec copy -i chinese.ac3 -acodec ac3 -ac 2 -ar 48000 -ab 128k -map 0:0 -map 0:1 -map 1:0 muxed2.avi -newaudio
Same result.
I have also tried to assign a codec to the stream with stream specifier(based on http://ffmpeg.org/ffmpeg.html#toc-Stream-specifiers-1, but these options are not recognized by my ffmpeg.
I am running out of things to try.
(The OP edited the answer into the question. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I think I just found the solution
from http://ffmpeg-users.933282.n4.nabble.com/Encoding-with-multiple-Audio-tracks-td1289403.html There, James Darley says:
So your command line should look like:
ffmpeg -i INPUT [output options] OUTPUT [audio options] -newaudio [subtitle options] -newsubtitle
I then re-arranged my options accordingly, i.e. audio options for the new track after the output:
ffmpeg -i vts_01.avi -vcodec copy -acodec copy -i Chinese.ac3 muxed3.avi -acodec copy -newaudio
And I now gets my two ac3 audio tracks at the right bitrate.

Resources