Ffmpeg merging mp3, mp4, no sound with copy codec

audioffmpegmp3video

I am using ffmpeg to create a video. The source is a set of png images (one per frame) and an mp3 file. I want to create an mp4 with sound.

I am doing it in two stages for now. Step one converts the png to mp4:

%d%\ffmpeg -r 25 -i png\%1-%%06d.png -s 1280x720 -c:v libx264 -pix_fmt yuv420p -b:v 500k -profile:v baseline -movflags faststart temp.mp4

Step two merges the sound and temporary mp4:

%d%\ffmpeg -i temp.mp4 -i %1\%1.mp3 %1.mp4

This works but the sound is very poor. I think it is doing mp3 to aac.

So I tried this:

%d%\ffmpeg -i temp.mp4 -i %1\%1.mp3 -c copy -map 0:0 -map 1:0 %1.mp4

I get no sound. I have tried many other options, different codecs, changing other audio parameters, but I get no sound.

The only way to get sound seems to be to leave audio at default, and that is too poor quality.

EDIT here is the output

C:\ffmpeg\bin\ffmpeg -r 25 -i png\scene2-%06d.png -s 1280x720 -c:v libx264 -pix_fmt yuv420p
 -b:v 500k -profile:v baseline -movflags faststart temp.mp4
ffmpeg version N-57057-g024bf3a Copyright (c) 2000-2013 the FFmpeg developers
  built on Oct 11 2013 18:01:59 with gcc 4.8.1 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetyp
e --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --ena
ble-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-l
ibopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libsp
eex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aa
cenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavp
ack --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      52. 46.101 / 52. 46.101
  libavcodec     55. 35.100 / 55. 35.100
  libavformat    55. 19.100 / 55. 19.100
  libavdevice    55.  4.100 / 55.  4.100
  libavfilter     3. 88.101 /  3. 88.101
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
Input #0, image2, from 'png\scene2-%06d.png':
  Duration: 00:00:13.00, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: png, rgba, 1280x720 [SAR 3779:3779 DAR 16:9], 25 fps, 25
 tbr, 25 tbn, 25 tbc
[libx264 @ 003f36a0] using SAR=1/1
[libx264 @ 003f36a0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.1 Cache64
[libx264 @ 003f36a0] profile Constrained Baseline, level 3.1
[libx264 @ 003f36a0] 264 - core 138 r2358 9e941d1 - H.264/MPEG-4 AVC codec - Cop
yleft 2003-2013 - http://www.videolan.org/x264.html - options: cabac=0 ref=3 deb
lock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 m
e_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chro
ma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 i
nterlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 k
eyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate
=500 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'temp.mp4':
  Metadata:
    encoder         : Lavf55.19.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x7
20 [SAR 1:1 DAR 16:9], q=-1--1, 500 kb/s, 12800 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (png -> libx264)
Press [q] to stop, [?] for help
frame=   57 fps= 56 q=35.0 size=      16kB time=00:00:00.40 bitrate= 320.3kbits/
frame=   84 fps= 55 q=26.0 size=      39kB time=00:00:01.48 bitrate= 214.0kbits/
frame=  113 fps= 56 q=19.0 size=      60kB time=00:00:02.64 bitrate= 186.0kbits/
frame=  141 fps= 55 q=21.0 size=     152kB time=00:00:03.76 bitrate= 332.0kbits/
frame=  167 fps= 55 q=24.0 size=     268kB time=00:00:04.80 bitrate= 457.8kbits/
frame=  190 fps= 53 q=25.0 size=     353kB time=00:00:05.72 bitrate= 506.2kbits/
frame=  213 fps= 52 q=26.0 size=     412kB time=00:00:06.64 bitrate= 508.0kbits/
frame=  238 fps= 52 q=23.0 size=     419kB time=00:00:07.64 bitrate= 449.1kbits/
frame=  262 fps= 51 q=22.0 size=     454kB time=00:00:08.60 bitrate= 432.5kbits/
frame=  286 fps= 51 q=22.0 size=     523kB time=00:00:09.56 bitrate= 448.1kbits/
frame=  309 fps= 50 q=25.0 size=     660kB time=00:00:10.48 bitrate= 515.8kbits/
[mp4 @ 003f3160] Starting second pass: moving the moov atom to the beginning of
the file
frame=  325 fps= 47 q=-1.0 Lsize=     794kB time=00:00:13.00 bitrate= 500.5kbits
/s
video:792kB audio:0kB subtitle:0 global headers:0kB muxing overhead 0.256995%
[libx264 @ 003f36a0] frame I:2     Avg QP:13.55  size: 24853
[libx264 @ 003f36a0] frame P:323   Avg QP:15.10  size:  2356
[libx264 @ 003f36a0] mb I  I16..4: 91.3%  0.0%  8.8%
[libx264 @ 003f36a0] mb P  I16..4:  1.5%  0.0%  0.2%  P16..4:  2.4%  1.0%  0.4%
 0.0%  0.0%    skip:94.5%
[libx264 @ 003f36a0] final ratefactor: 19.04
[libx264 @ 003f36a0] coded y,uvDC,uvAC intra: 6.3% 15.8% 12.8% inter: 1.3% 2.5%
1.4%
[libx264 @ 003f36a0] i16 v,h,dc,p: 82% 17%  2%  0%
[libx264 @ 003f36a0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 39% 18% 25%  3%  3%  4%  3%
 2%  3%
[libx264 @ 003f36a0] i8c dc,h,v,p: 69% 23%  8%  1%
[libx264 @ 003f36a0] ref P L0: 75.5% 14.7%  9.8%
[libx264 @ 003f36a0] kb/s:498.84

C:\ffmpeg\bin\ffmpeg -i temp.mp4 -i scene2\scene2.mp3 -c copy -map 0:0 -map 1:0 scene2.mp4

ffmpeg version N-57057-g024bf3a Copyright (c) 2000-2013 the FFmpeg developers
  built on Oct 11 2013 18:01:59 with gcc 4.8.1 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetyp
e --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --ena
ble-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-l
ibopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libsp
eex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aa
cenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavp
ack --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      52. 46.101 / 52. 46.101
  libavcodec     55. 35.100 / 55. 35.100
  libavformat    55. 19.100 / 55. 19.100
  libavdevice    55.  4.100 / 55.  4.100
  libavfilter     3. 88.101 /  3. 88.101
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'temp.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.19.100
  Duration: 00:00:13.00, start: 0.000000, bitrate: 500 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yu
v420p, 1280x720 [SAR 1:1 DAR 16:9], 499 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc
(default)
    Metadata:
      handler_name    : VideoHandler
Input #1, mp3, from 'scene2\scene2.mp3':
  Metadata:
    genre           : Blues
  Duration: 00:00:09.86, start: 0.000000, bitrate: 128 kb/s
    Stream #1:0: Audio: mp3, 16000 Hz, mono, s16p, 128 kb/s
Output #0, mp4, to 'scene2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.19.100
    Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [S
AR 1:1 DAR 16:9], q=2-31, 499 kb/s, 25 fps, 12800 tbn, 12800 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1: Audio: mp3 (i[0][0][0] / 0x0069), 16000 Hz, mono, 128 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=  325 fps=0.0 q=-1.0 Lsize=     951kB time=00:00:12.96 bitrate= 601.4kbits
/s
video:792kB audio:154kB subtitle:0 global headers:0kB muxing overhead 0.540095%

EDIT added output for -i scene2.mp4

C:\ffmpeg\bin\ffmpeg -i scene2.mp4
ffmpeg version N-57057-g024bf3a Copyright (c) 2000-2013 the FFmpeg developers
  built on Oct 11 2013 18:01:59 with gcc 4.8.1 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetyp
e --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --ena
ble-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-l
ibopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libsp
eex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aa
cenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavp
ack --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      52. 46.101 / 52. 46.101
  libavcodec     55. 35.100 / 55. 35.100
  libavformat    55. 19.100 / 55. 19.100
  libavdevice    55.  4.100 / 55.  4.100
  libavfilter     3. 88.101 /  3. 88.101
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'scene2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.19.100
  Duration: 00:00:13.00, start: 0.000000, bitrate: 599 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yu
v420p, 1280x720 [SAR 1:1 DAR 16:9], 499 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc
(default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: mp3 (mp4a / 0x6134706D), 16000 Hz, mono, s16p, 128
kb/s (default)
    Metadata:
      handler_name    : SoundHandler
At least one output file must be specified

Best Answer

Windows Media Player is picky when it comes to MP3 in MP4 container format. It does not like audio sample rates of less than 32 kHz, nor does it like 96 kHz or above (I did not test for an exact upper limit).

QuickTime did not play any sample file that contained MP3 in MP4 container.

You have several options to deal with this limitation:

1. Use a different player

VLC works and is a great player. This option has the advantage that you can stream copy the audio (as you are doing) instead of re-encoding it; thus preserving the quality.

2. Use a different container format

Such as .mov. Like the previous option this will also allow you to stream copy the original audio.

3. Use a different audio format

If you must use WMP, then you'll have to re-encode the audio. Since AAC audio is the norm in MP4 container you can use: -c:a aac. See FFmpeg Wiki: AAC Audio Encoding Guide for more info on that.

4. Change the audio sample rate

If you muse output MP3 then experiment with the -ar option, such as -ar 48000. This should be the last option to try.


Do everything in one step

Now that you have something that works you can do everything at once instead of re-encoding your video with an additional, unnecessarily step.

Stream copy audio

ffmpeg -framerate 25 -i %06d.png -i input.mp3 -c:v libx264 \
-vf scale=1280:-2,format=yuv420p -c:a copy -movflags +faststart output.mp4

Re-encode audio

ffmpeg -framerate 25 -i %06d.png -i input.mp3 -c:v libx264 \
-vf scale=1280:-2,format=yuv420p -c:a aac -movflags +faststart output.mp4

Notes

Related Question