[FFmpeg-user] Is it possible to add audio without re-encoding?

Lou lou at lrcd.com
Sat Feb 4 20:34:33 CET 2012


Please keep your replies within the relevant thread if possible.

On Sat, 4 Feb 2012 18:47:06 +0200
Andrew Gora <master at io.ua> wrote:

> >> Hi,
> >> 
> >> 
> >> There are 30.000 videos converted to H.264 without audio by mistake.
> >> 
> >> Would you please advice, is it possible to take just audio from
> >> original videos and add to the converted ones without re-encoding?
> >> 
> >> 
> >> BIG thanks in advance,
> >> Andrew
> > 
> > Yes. This example will take the first video stream from video.mp4 and
> > the first audio stream from audio.mp4 and mux them into out.mp4 without
> > re-encoding.
> > 
> >     ffmpeg -i video.mp4 -i audio.mp4 -map 0:0 -map 1:1 -c copy out.mp4
> > 
> > You may want to add the "-shortest" option if the durations of the
> > video and audio inputs vary for some reason. The -map option has seen
> > some syntax updates lately. See Changelog for more detailed information.
> 
> Thank You! But something goes wrong, and result file is 0 bytes weight.

This is because the first stream in 00030229.flv is actually audio, not
video. This is not usually the case, but isn't exactly rare. See the
stream info below.

> 00030229.mp4 has audio:
> 
> /usr/local/ffmpeg-0.10/bin/ffmpeg -i /farm-video/img_v/mp4/0003/02/00030229.flv -i /farm-video/img_v/mp4/0003/02/00030229.mp4 -map 0:0 -map 1:1 -c copy -y /farm-video/img_v/mp4/0003/02/00030229_1.flv
> 
> ffmpeg version 0.10 Copyright (c) 2000-2012 the FFmpeg developers
>   built on Jan 30 2012 11:57:43 with gcc 4.4.5 20110214 (Red Hat 4.4.5-6)
>   configuration: --prefix=/usr/local/ffmpeg-0.10 --enable-shared --enable-pthreads --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-nonfree --enable-gpl --enable-libmp3lame --enable-libxvid --enable-libvorbis --enable-libfaac --enable-libx264 --enable-avfilter --enable-postproc --enable-version3
>   libavutil      51. 34.101 / 51. 34.101
>   libavcodec     53. 60.100 / 53. 60.100
>   libavformat    53. 31.100 / 53. 31.100
>   libavdevice    53.  4.100 / 53.  4.100
>   libavfilter     2. 60.100 /  2. 60.100
>   libswscale      2.  1.100 /  2.  1.100
>   libswresample   0.  6.100 /  0.  6.100
>   libpostproc    52.  0.100 / 52.  0.100
> [flv @ 0xd8f3a0] Stream discovered after head already parsed
> [flv @ 0xd8f3a0] Could not find codec parameters (Audio: none, 0 channels)
> Input #0, flv, from '/farm-video/img_v/mp4/0003/02/00030229.flv':

Your first input, 00030229.flv, is called 0 in terms of -map.

>   Metadata:
>     lasttimestamp   : 480
>     audiosize       : 7632317
>     minor_version   : 512
>     datasize        : 47072783
>     compatible_brands: isomiso2avc1mp41
>     totalframes     : 11998
>     creation_time   : 1970-01-01 00:00:00
>     hasAudio        : true
>     hasCuePoints    : false
>     hasKeyframes    : true
>     hasMetadata     : true
>     hasVideo        : true
>     metadatacreator : flvtool++ (Facebook, Motion project, dweatherford)
>     videosize       : 38936044
>     canSeekToEnd    : true
>     major_brand     : isom
>     encoder         : Lavf53.31.100
>   Duration: 00:07:59.92, start: 0.080000, bitrate: 652 kb/s
>     Stream #0:0: Audio: none, 0 channels

Stream 0:0 is "audio". The 0:0 means "first input file:first
stream".

>     Stream #0:1: Video: h264 (Main), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 25 tbr, 1k tbn, 50 tbc

Stream 0:1 is your desired video.

> Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '/farm-video/img_v/mp4/0003/02/00030229.mp4':
>   Metadata:
>     major_brand     : isom
>     minor_version   : 512
>     compatible_brands: isomiso2avc1mp41
>     creation_time   : 1970-01-01 00:00:00
>     encoder         : Lavf52.31.0
>   Duration: 00:07:59.88, start: 0.000000, bitrate: 997 kb/s
>     Stream #1:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 720x480 [SAR 8:9 DAR 4:3], 864 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc
>     Metadata:
>       creation_time   : 1970-01-01 00:00:00
>       handler_name    : .VideoHandler
>     Stream #1:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, s16, 127 kb/s

Stream 1:1 is your desired audio for your second input.

>     Metadata:
>       creation_time   : 1970-01-01 00:00:00
>       handler_name    : 
> [flv @ 0xf5ef60] sample rate not set
> Output #0, flv, to '/farm-video/img_v/mp4/0003/02/00030229_1.flv':
>   Metadata:
>     lasttimestamp   : 480
>     audiosize       : 7632317
>     minor_version   : 512
>     datasize        : 47072783
>     compatible_brands: isomiso2avc1mp41
>     totalframes     : 11998
>     creation_time   : 1970-01-01 00:00:00
>     hasAudio        : true
>     hasCuePoints    : false
>     hasKeyframes    : true
>     hasMetadata     : true
>     hasVideo        : true
>     metadatacreator : flvtool++ (Facebook, Motion project, dweatherford)
>     videosize       : 38936044
>     canSeekToEnd    : true
>     major_brand     : isom
>     encoder         : Lavf53.31.100
>     Stream #0:0: Audio: none, 0 channels
>     Stream #0:1(und): Audio: aac, 44100 Hz, stereo, 127 kb/s
>     Metadata:
>       creation_time   : 1970-01-01 00:00:00
>       handler_name    : 
> Stream mapping:
>   Stream #0:0 -> #0:0 (copy)
>   Stream #1:1 -> #0:1 (copy)
> Could not write header for output file #0 (incorrect codec parameters ?)

So your command will be:
     ffmpeg -i video.mp4 -i audio.mp4 -map 0:1 -map 1:1 -c copy out.mp4


More information about the ffmpeg-user mailing list