1

In Java, I'm using FFmpeg to convert a WAV file to AAC with the function:

-i input.wav -ac 1 -ab 64000 -ar 22050 output.aac

When I open these two files in Audacity, I can clearly see that the AAC file is 0.05 seconds longer, with 0.05 seconds of silence added to the beginning.

This is an issue when I try to mux this AAC file with a video file - the audio doesn't sync up.

Is there some sort of parameter to prevent this behavior or another way to get around this issue?


Edit - Console output while running the function:

10-28 17:47:10.845: I/System.out(14784): FFmpeg: Running the function: -i "input.wav" -ac 1 -ab 64000 -ar 22050 "output.aac"
10-28 17:47:10.845: I/System.out(14784): FFmpeg function: onStart()
10-28 17:47:10.865: D/FFmpeg(14784): Running publishing updates method
10-28 17:47:10.865: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.875: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.875: I/System.out(14784): ffmpeg version n2.4.2 Copyright (c) 2000-2014 the FFmpeg developers
10-28 17:47:10.875: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.875: I/System.out(14784):   built on Oct  7 2014 15:08:46 with gcc 4.8 (GCC)
10-28 17:47:10.875: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.875: I/System.out(14784):   configuration: --target-os=linux --cross-prefix=/home/sb/Source-Code/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/sb/Source-Code/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/sb/Source-Code/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/sb/Source-Code/ffmpeg-android/build/armeabi-v7a-neon --extra-cflags='-I/home/sb/Source-Code/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all -mfpu=neon' --extra-ldflags='-L/home/sb/Source-Code/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
10-28 17:47:10.875: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.875: I/System.out(14784):   libavutil      54.  7.100 / 54.  7.100
10-28 17:47:10.875: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.875: I/System.out(14784):   libavcodec     56.  1.100 / 56.  1.100
10-28 17:47:10.875: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.875: I/System.out(14784):   libavformat    56.  4.101 / 56.  4.101
10-28 17:47:10.880: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.880: I/System.out(14784):   libavdevice    56.  0.100 / 56.  0.100
10-28 17:47:10.880: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.880: I/System.out(14784):   libavfilter     5.  1.100 /  5.  1.100
10-28 17:47:10.880: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.880: I/System.out(14784):   libswscale      3.  0.100 /  3.  0.100
10-28 17:47:10.880: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.880: I/System.out(14784):   libswresample   1.  1.100 /  1.  1.100
10-28 17:47:10.880: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.880: I/System.out(14784):   libpostproc    53.  0.100 / 53.  0.100
10-28 17:47:10.895: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.895: I/System.out(14784): Guessed Channel Layout for  Input Stream #0.0 : mono
10-28 17:47:10.895: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.895: I/System.out(14784): Input #0, wav, from 'input.wav':
10-28 17:47:10.895: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.900: I/System.out(14784):   Duration: 00:00:02.50, bitrate: 352 kb/s
10-28 17:47:10.900: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.900: I/System.out(14784):     Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 22050 Hz, 1 channels, s16, 352 kb/s
10-28 17:47:10.935: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.935: I/System.out(14784): Output #0, adts, to 'output.aac':
10-28 17:47:10.935: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.935: I/System.out(14784):   Metadata:
10-28 17:47:10.935: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.935: I/System.out(14784):     encoder         : Lavf56.4.101
10-28 17:47:10.935: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.935: I/System.out(14784):     Stream #0:0: Audio: aac, 22050 Hz, mono, fltp, 64 kb/s
10-28 17:47:10.935: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.935: I/System.out(14784):     Metadata:
10-28 17:47:10.935: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.935: I/System.out(14784):       encoder         : Lavc56.1.100 aac
10-28 17:47:10.940: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.940: I/System.out(14784): Stream mapping:
10-28 17:47:10.940: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.940: I/System.out(14784):   Stream #0:0 -> #0:0 (pcm_s16le (native) -> aac (native))
10-28 17:47:10.940: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:10.940: I/System.out(14784): Press [q] to stop, [?] for help
10-28 17:47:11.395: D/dalvikvm(14784): JIT unchain all for threadid=16
10-28 17:47:11.420: D/dalvikvm(14784): GC_FOR_ALLOC freed 4698K, 40% free 7190K/11964K, paused 271ms, total 272ms
10-28 17:47:11.420: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:11.420: I/System.out(14784): size=      12kB time=00:00:02.50 bitrate=  37.7kbits/s    
10-28 17:47:11.455: I/System.out(14784): FFmpeg function: onProgress()...
10-28 17:47:11.455: I/System.out(14784): video:0kB audio:11kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.363914%
10-28 17:47:11.470: I/System.out(14784): FFmpeg function: onSuccess(), message:
10-28 17:47:11.470: I/System.out(14784): ffmpeg version n2.4.2 Copyright (c) 2000-2014 the FFmpeg developers
10-28 17:47:11.470: I/System.out(14784):   built on Oct  7 2014 15:08:46 with gcc 4.8 (GCC)
10-28 17:47:11.470: I/System.out(14784):   configuration: --target-os=linux --cross-prefix=/home/sb/Source-Code/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/sb/Source-Code/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/sb/Source-Code/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/sb/Source-Code/ffmpeg-android/build/armeabi-v7a-neon --extra-cflags='-I/home/sb/Source-Code/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all -mfpu=neon' --extra-ldflags='-L/home/sb/Source-Code/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
10-28 17:47:11.470: I/System.out(14784):   libavutil      54.  7.100 / 54.  7.100
10-28 17:47:11.470: I/System.out(14784):   libavcodec     56.  1.100 / 56.  1.100
10-28 17:47:11.470: I/System.out(14784):   libavformat    56.  4.101 / 56.  4.101
10-28 17:47:11.470: I/System.out(14784):   libavdevice    56.  0.100 / 56.  0.100
10-28 17:47:11.470: I/System.out(14784):   libavfilter     5.  1.100 /  5.  1.100
10-28 17:47:11.470: I/System.out(14784):   libswscale      3.  0.100 /  3.  0.100
10-28 17:47:11.470: I/System.out(14784):   libswresample   1.  1.100 /  1.  1.100
10-28 17:47:11.470: I/System.out(14784):   libpostproc    53.  0.100 / 53.  0.100
10-28 17:47:11.470: I/System.out(14784): Guessed Channel Layout for  Input Stream #0.0 : mono
10-28 17:47:11.470: I/System.out(14784): Input #0, wav, from 'input.wav':
10-28 17:47:11.470: I/System.out(14784):   Duration: 00:00:02.50, bitrate: 352 kb/s
10-28 17:47:11.475: I/System.out(14784):     Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 22050 Hz, 1 channels, s16, 352 kb/s
10-28 17:47:11.475: I/System.out(14784): Output #0, adts, to 'output.aac':
10-28 17:47:11.475: I/System.out(14784):   Metadata:
10-28 17:47:11.475: I/System.out(14784):     encoder         : Lavf56.4.101
10-28 17:47:11.475: I/System.out(14784):     Stream #0:0: Audio: aac, 22050 Hz, mono, fltp, 64 kb/s
10-28 17:47:11.475: I/System.out(14784):     Metadata:
10-28 17:47:11.475: I/System.out(14784):       encoder         : Lavc56.1.100 aac
10-28 17:47:11.475: I/System.out(14784): Stream mapping:
10-28 17:47:11.475: I/System.out(14784):   Stream #0:0 -> #0:0 (pcm_s16le (native) -> aac (native))
10-28 17:47:11.475: I/System.out(14784): Press [q] to stop, [?] for help
10-28 17:47:11.475: I/System.out(14784): size=      12kB time=00:00:02.50 bitrate=  37.7kbits/s    
10-28 17:47:11.475: I/System.out(14784): video:0kB audio:11kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.363914%
10-28 17:47:11.475: I/System.out(14784): FFmpeg function: onFinish()
5
  • Please show the complete console output from your command.
    – llogan
    Oct 28, 2015 at 21:33
  • @LordNeckbeard Yeah, should have included that, edited it into the OP. Oct 28, 2015 at 21:56
  • What is adding all of that "pollution" to the ffmpeg console output?
    – llogan
    Oct 28, 2015 at 22:53
  • @LordNeckbeard Well it's an Android FFmpeg library, so the onStart(), onProgress(), onFinish() stuff is coming from the Android classes that interact with FFmpeg. Otherwise I'm not sure what a "standard" FFmpeg console output looks like, so I'm not sure of any other differences. Oct 29, 2015 at 12:10
  • I was curious because I see this on occasion. Is there a way to keep it from adding that junk?
    – llogan
    Oct 29, 2015 at 18:58

1 Answer 1

4

The AAC works on a "per frame" basis. The frame is 2048 samples, with 50% overlapp with the previous frame, making the "step" size 1024 samples.

Some codecs insert one or two empty frames at the beggining. If your sampling rate is 22050 Hz, then 1024 samples makes for 0.046 seconds, so this might be the case for you.

Why they insert empty frame? Probably because some look ahead calculation, or the first frame has no previous frame to overlapp with, or similar.

10
  • That's interesting. I do believe the sampling rate is 22050, so would it always be the case where the first (1 or 2) frames can essentially be "ignored" while muxing the AAC with the video? As in, will that be a constant due to the sampling rate, or is it dependent on something else too (the duration of the AAC file)? Oct 29, 2015 at 12:07
  • @FTLRalph Initial silence can also occur with MP3.
    – llogan
    Oct 29, 2015 at 18:57
  • I figured the sampling rate is 22050 from your command line. It's constant for the encoder you are using (one frame in this case, doesn't depend on the sampling rate, even the duration of that one frame is different of course), but different encoder might do different.
    – Danijel
    Oct 30, 2015 at 10:46
  • 2
    AAC codecs have this problem at the end of the file too. Since the input is rarely a multiple of 1024, codecs have to append zeros. Since this became a real problem in the past, ISO tried to address the issue and published a spec on how to encode a non frame-size-multiple number of samples, but I don't think codec developers ever implemented this spec, at least I am not aware of such codecs.
    – Danijel
    Oct 30, 2015 at 10:46
  • @Danijel is there any ways to detect how many empty frames are inserted at the beginning by the decoder? so I can discard them
    – cs guy
    Nov 30, 2020 at 23:37

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.