2

I'm struggling with the ffmpeg concat demuxer.

Say I have two files: ones.wav and zeros.wav. They both are 5 second long, in ones.wav all samples are 1 and in zeros.wav all samples are 0. Both files' sampling rate is 44100Hz

Now I want to take the first 0.5s of ones.wav and concatenate it with the first 0.001s of zeros.wav. I use following concat.txt file:

file ones.wav
outpoint 0.5

file zeros.wav
outpoint 0.001

And following command:

ffmpeg -f concat -i concat.txt out.wav

The output file then contains ±0.51084s or 22528 samples of ones, and ±0.02320s or 1023 samples of zeros

How do I make these cuts exactly to the desired length? And how would this anwser change if I were to use compressed audio or wanted to concatenate video clips?

If it's of any use, this is command output:

ffmpeg version n4.1.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181127
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-nvdec --enable-nvenc --enable-omx --enable-shared --enable-version3
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, concat, from 'concat.txt':
  Duration: N/A, start: 0.000000, bitrate: 1411 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'out.wav':
  Metadata:
    ISFT            : Lavf58.20.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
    Metadata:
      encoder         : Lavc58.35.100 pcm_s16le
size=      92kB time=00:00:00.52 bitrate=1441.6kbits/s speed=74.3x    
video:0kB audio:92kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.082796%
1

Compressed audio is chunked into frames e.g. AAC is typically chunked into 1024 samples per frame. Although uncompressed audio isn't stored that way. ffmpeg will still encapsulate samples because the framework is designed to work with frames.

You need to redefine frame size so that trimming can work with more accuracy.

Try,

ffmpeg -f concat -segment_time_metadata 1 -i concat.txt -af asetnsamples=44,aselect=concatdec_select out.wav

  • Well, I guess I should have noticed multiples of 1024... Anyway, this command looks like it should fix my problem, but the resulting file has the same problem as before. Do you have any idea why? – Brych May 8 at 7:32
  • I'll test this myself and get back. – Gyan May 8 at 7:39
  • I forgot to set an option for the concat demuxer which forwards the desired time range to the select filter. – Gyan May 8 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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