ffmpeg keeps converting second ac3 audio stream to mp2 - ffmpeg

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.

Related

How to Transcode ALL Audio streams from input to output using ffmpeg?

I have an input MPEG TS file 'unit_test.ts'. This file has following content (shown by ffprobe):
Input #0, mpegts, from 'unit_test.ts':
Duration: 00:00:57.23, start: 73674.049844, bitrate: 2401 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x31]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(progressive), 852x480 [SAR 640:639 DAR 16:9], Closed Captions, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc
Stream #0:1[0x34](eng): Audio: ac3 ([129][0][0][0] / 0x0081), 48000 Hz, 5.1(side), fltp, 448 kb/s
Stream #0:2[0x35](spa): Audio: ac3 ([129][0][0][0] / 0x0081), 48000 Hz, stereo, fltp, 192 kb/s
I want to convert it into another MPEG TS file. Requirement is that the Video stream of the input should be directly copied to the output whereas ALL the audio streams should be transcoded "aac" format.
I tried this command:
ffmpeg -i unit_test.ts -map 0 -c copy -c:a aac maud_test.ts
It converted it into 'maud_test.ts' with following contents (shown by ffprobe)
Input #0, mpegts, from 'maud_test.ts':
Duration: 00:00:57.25, start: 1.400000, bitrate: 2211 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(progressive), 852x480 [SAR 640:639 DAR 16:9], Closed Captions, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc
Stream #0:1[0x101](eng): Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, 6 channels, fltp, 391 kb/s
Stream #0:2[0x102](spa): Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 133 kb/s
So it appeared as if the command worked....However when I play the maud_test.ts file in vlc player I can see both audio streams listed in the menu; but Stream 1 (eng) remains silent............whereas Stream 2 (spa) has proper audio. (Original TS file has both audio streams properly audible)
I have tried this with different input files and have seen that same problem occurs in each case.
What that I am doing is not right?
How should I get this done? (I can write explicit stream by stream map and channel arguments to get that done; however I want the command line to be generic, in that the input file could be having any configuration with one Video and several Audios with different formats. The configuration will not be known beforehand.)

Video transcode and codec issue

I'm converting some old videos to play on my Roku via a dlna server. I'm trying to understand the MP4 container better to optimize conversions. I have an ogm video:
Duration: 01:00:38.22, start: 0.000000, bitrate: 1056 kb/s
Stream #0:0: Video: mpeg4 (XVID / 0x44495658), yuv420p, 576x324 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 23.98 tbn, 23.98 tbc
Stream #0:1(English): Audio: aac, 48000 Hz, stereo, fltp, 74 kb/s
Stream #0:2(Japanese): Audio: aac, 48000 Hz, stereo, fltp, 73 kb/s
Stream #0:3(English): Subtitle: text
From what I understand, MP4 container can contain MP4 video and aac audio. I used
-c:a copy -c:v copy
And it worked, but the video won't play. Obviously something's wrong. What I don't understand is why, the video looks like it's MP4 and the audio is aac. My guess is it's the Xvid but why?
Thank you.
Todd
MP4 is only the container but your right MP4 usually contain AAC audio and MPEG4 video. Your input file seems to be mpeg4 Xvid which is a mpeg4 derived codec but might not be supported by Roku.
Try change -c:v copy to -c:v h264 to use a more common MPEG4 based video codec.

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 add text frames to the start of video

I have some videos either in mp4 or webm format, and I'd like to use ffmpeg to add 4 seconds to the start of each video to display some text in the center with no sound.
Some other requirements:
try to avoid re-encoding the video
need to maintain the quality (resolution, bitrate, etc)
(optional) to make the text fade in/out
I am new to ffmpeg and any help will be appreciated.
thanks in advance
Example ffprobe information for mp4 below:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.33.100
Duration: 00:00:03.84, start: 0.042667, bitrate: 1117 kb/s
Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 1021 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 140 kb/s (default)
Metadata:
handler_name : SoundHandler
Example webm
Input #0, matroska,webm, from 'input.webm':
Metadata:
encoder : Lavf55.33.100
Duration: 00:00:03.80, start: 0.000000, bitrate: 1060 kb/s
Stream #0:0(eng): Video: vp8, yuv420p, 1280x720, SAR 1:1 DAR 16:9, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
Stream #0:1(eng): Audio: vorbis, 48000 Hz, stereo, fltp (default)
Screenshot from joined.mp4
Screenshot for step 3 console
You'll have to generate a 4 second video with dummy audio matching the parameters of the existing video, including timebase, and then use the concat demuxer with streamcopy.
For the sample files shown in Q:
Step 1 Generate text video
ffmpeg -f lavfi -r 30 -i color=black:1280x720 -f lavfi -i anullsrc -vf "drawtext=fontfile='/path/to/font.ttf':fontcolor=FFFFFF:fontsize=50:text='Your text':x='(main_w-text_w)/2':y='(main_h-text_h)/2',fade=t=in:st=0:d=1,fade=t=out:st=3:d=1" -c:v libx264 -b:v 1000k -pix_fmt yuv420p -video_track_timescale 15360 -c:a aac -ar 48000 -ac 2 -sample_fmt fltp -t 4 intro.mp4
For WebM, replace -c:v libx264 with -c:v libvpx, -c:a aac with -c:a libvorbis and intro.mp4 with intro.webm. You may remove the -video_track_timescale 15360 since WebMs tend to use a single timescale, that I've seen.
Step 2 Prepare concat file, say, list.txt
file 'intro.mp4'
file 'input.mp4'
Step 3 Concat
ffmpeg -f concat -i list.txt -c copy -fflags +genpts joined.mp4
The variables important here are video size 1280x720, frame rate -r 30, -pix_fmt yuv420p, sample rate -ar 48000, format -sample_fmt fltp, channel layout -ac 2 and of course, codecs.
Short answer is that you cannot encode new data as mp4 or webm and insert it at the front of the video stream. Those formats simply do not work like that. Both of these encoding formats are lossy, so if you decode and encode them again then additional information will be lost/changed by the second encoding. You could do something else, but what you are trying to do will not work.

Demux audio (AMR_WB) and video(H264) from mp4 file using ffmpeg

I want to demux audio (AMR_WB) and video(H264) from an mp4 file. I need to write a program which does this using ffmpeg libraries.
In demuxing.c file which is there in FFMPEG examples i was able to get only the raw formats as the output.
Can i somehow modify that code to get H264 and AMR_WB in encoded format from the mp4 file?
Run ffmpeg twice , each time specify that just 1 track be copy to output.
Example on diff mp4 will provide most of the idea which u will need to adapt to your specific track types for the respective video/audio in your container...
MP4 example : demux h264 and aac tracks to separate outputs (tout1, tout2 )
Whats in input?
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'phoneCam_20120902_112701.mp4':
Metadata:
major_brand : isom
minor_version : 0
compatible_brands: isom3gp4
creation_time : 2012-09-02 18:27:14
Duration: 00:00:12.65, start: 0.000000, bitrate: 8011 kb/s
Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 7707 kb/s, SAR 65536:65536 DAR 16:9, 28.64 fps, 29.83 tbr, 90k tbn, 180k tbc
Metadata:
creation_time : 2012-09-02 18:27:14
handler_name : VideoHandle
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, mono, s16, 96 kb/s
Pass 1, just get the Vid
ffmpeg -i phoneCam_20120902_112701.mp4 -map 0:0 -c copy tout1.mp4
Pass2 just get the aud
ffmpeg -i phoneCam_20120902_112701.mp4 -map 0:1 -c aac -ar 48000 -ab
48000 -strict -2 tout2.3gp
In your program, just run ffmpeg from the CLI or call main() in ffmpeg.c

Resources