<p>This command, which used <code>t</code> reference for <code>aselect</code>, worked for me:</p>

<pre><code>ffmpeg -i AV36_1.avi -vf "select=between(n\,200\,399),setpts=PTS-STARTPTS" -af "aselect=between(t\,(200/15)\,(399/15)),asetpts=PTS-STARTPTS" -y test.mkv
</code></pre>

<p>If you want to use <code>n</code> (frames), then you will need to know the frame size of the audio codec e.g. 1024 samples for AAC, variable for MP3..etc</p>

CC BY-SA 3.0
2015-12-16T10:08:23.640Z
Cool, but this conversion won't work if the video has variable framerate. The info about audio frame sample sizes is interesting, I thought the sampling rate (22050Hz in this case) was always constant over time.
jiggunjer
CC BY-SA 3.0
2015-12-16T10:14:07.380Z
Sampling rate is constant, but compressed audio streams chunk `n` samples into `frames` which don't correspond to video frame boundaries. Your method won't work for VFR either, as the audio frame rate you're getting from ffprobe is CFR. A path to a general solution is to obtain the PTS of the first and last selected video frames and then supply the equivalent times to the aselect filter.
Gyan
CC BY-SA 3.0
2015-12-16T10:28:43.600Z
Then it seems timestamps are the safest way. So would an mp3 frame with 100 samples last 10 times longer than a frame with 10 samples? If `aselect` can only select entire audio frames this could limit accuracy. But my gut is telling me that `aselect` selects samples instead of frames (when using timestamps at least).
jiggunjer
CC BY-SA 3.0
2015-12-16T10:38:31.547Z
No. Depends on the sampling rate. If you have a 48000 Hz rate, then a typical 1152 samples MP3 frame is 0.04166 seconds of audio. If 24000 Hz, then 0.0833 s.
Gyan
CC BY-SA 3.0
2015-12-16T12:45:16.013Z
but a typical MP3 frame does not exist, as they have a variable frame size...so some frames will have 1500 samples, others 900, etc. Ah but the timestamps might not be as linear as with video.
jiggunjer
CC BY-SA 3.0
2015-12-16T09:54:38.243Z
2015-12-16T10:05:50.763Z
2015-12-16T10:05:50.763Z
Gyan
<p>How do I select the audio corresponding to a set of filter-selected videoframes? </p>

<p>E.g. at the moment I am sampling 1 in every 100 frames of a video (running at 15fps) using the FFMpeg <code>select</code> filter. When I try to select the audio of those frames with the <code>aselect</code> filter, the total audio duration is not equal to the video duration (2 or 3 seconds difference depending on which frames I selected). The audio sampling rate is listed as 22050Hz.</p>

<p>I tried using various <code>-vsync</code> and <code>-async</code> combinations but it made no difference. Is there a more accurate way of using <code>aselect</code>?</p>

<p>A quick example command:</p>

<p><code>ffmpeg -i %INPUT% -vf select='between(n\,200\,399)',setpts='PTS-STARTPTS' -af aselect='between(n\,200\,399)',asetpts='PTS-STARTPTS'-y test.mkv</code></p>

<p>where 'INPUT' is the name of some file containing compressed video and audio. I am testing with the file AV36_1.avi found on <a href="http://samples.mplayerhq.hu/avi/" rel="nofollow">this</a> site hosting video samples. I get a video duration of 13.3 seconds and audio duration of 12 seconds.</p>

<p>output: </p>

<pre><code>ffmpeg version N-77045-ga16243a Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 5.2.0 (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-libbs2b --enable-libcaca --
enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-l
ibilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enab
le-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --en
able-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --ena
ble-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc
 --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enabl
e-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --
enable-lzma --enable-decklink --enable-zlib
  libavutil      55.  9.100 / 55.  9.100
  libavcodec     57. 16.101 / 57. 16.101
  libavformat    57. 19.100 / 57. 19.100
  libavdevice    57.  0.100 / 57.  0.100
  libavfilter     6. 20.100 /  6. 20.100
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Guessed Channel Layout for  Input Stream #0.1 : stereo
Input #0, avi, from 'AV36_1.avi':
  Duration: 00:00:32.93, start: 0.000000, bitrate: 2372 kb/s
    Stream #0:0: Video: indeo5 (IV50 / 0x30355649), yuv410p, 320x240, 2058 kb/s,
 15 fps, 15 tbr, 15 tbn, 15 tbc
    Metadata:
      title           : Steyr.avi ┬ΦΣσε #1
    Stream #0:1: Audio: adpcm_ms ([2][0][0][0] / 0x0002), 22050 Hz, 2 channels,
s16, 176 kb/s
    Metadata:
      title           : Sound Forge 4.0 Audio
Output #0, avi, to 'test.avi':
  Metadata:
    ISFT            : Lavf57.19.100
    Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 320x240, q=2-31, 200
 kb/s, 15 fps, 15 tbn, 15 tbc
    Metadata:
      title           : Steyr.avi ┬ΦΣσε #1
      encoder         : Lavc57.16.101 mpeg4
    Stream #0:1: Audio: mp3 (libmp3lame) (U[0][0][0] / 0x0055), 22050 Hz, stereo
, s16p
    Metadata:
      title           : Sound Forge 4.0 Audio
      encoder         : Lavc57.16.101 libmp3lame
Stream mapping:
  Stream #0:0 -&gt; #0:0 (indeo5 (native) -&gt; mpeg4 (native))
  Stream #0:1 -&gt; #0:1 (adpcm_ms (native) -&gt; mp3 (libmp3lame))
Press [q] to stop, [?] for help
frame=   98 fps=0.0 q=9.6 size=     294kB time=00:00:09.03 bitrate= 266.1kbits/s
frame=  200 fps=0.0 q=11.7 Lsize=     525kB time=00:00:13.33 bitrate= 322.5kbits
/s
video:429kB audio:72kB subtitle:0kB other streams:0kB global headers:0kB muxing
overhead: 4.625268%
</code></pre>

<p>I think I'm using the aselect filter wrong...</p>

CC BY-SA 3.0
2015-12-16T04:43:04.590Z
Paste your command line used and console output.
Gyan
CC BY-SA 3.0
2015-12-16T05:29:32.557Z
@Mulvya done :)
jiggunjer
CC BY-SA 3.0
2015-12-16T05:58:13.223Z
Please reformat the console properly, like first box [here](http://video.stackexchange.com/q/16987/1871).
Gyan
CC BY-SA 3.0
2015-12-16T06:03:50.987Z
Your command is selecting all frames from #200 to #399, not 1 of 100, and your sample file is indicated as "Very strange avi sample containing some stray LIST fixed by r20118" Maybe try with a sane sample. I'll test myself in the evening and get back
Gyan
CC BY-SA 3.0
2015-12-16T06:36:06.380Z
The exact selection doesn't matter, the point is that the corresponding audio selection has a large duration discrepancy. This trivial example should involve less possible complications than a more involved selection of frames (like `not(mod(n,100)) -vsync 0`). It would also be harder to select the corresponding audio frames.
jiggunjer
CC BY-SA 3.0
2015-12-16T07:34:53.413Z
Looks like a known problem: http://superuser.com/q/866144/114058
Gyan
CC BY-SA 3.0
2015-12-16T08:32:44.570Z
@Mulvya Meanwhile I think I discovered it is because of the audio framerate difference. I assumed it equal to the video framerate. The problem in your link assumes the audio framerate is 48fps because the sampling frequency was 48kHz. But it seems both assumptions are dangerous. In this case the audio framerate is about 21.8fps. By correcting the `aselect` frames with a factor `21.8/15` I get a much more accurate audio track. A few milliseconds difference. :D
jiggunjer
CC BY-SA 3.0
2015-12-16T09:36:56.270Z
If aselect is using audio frames for reference, then try 22.05/15 and if that makes it more accurate
Gyan
CC BY-SA 3.0
2015-12-16T09:53:02.537Z
@Mulvya No, the audio framerate is not 22.05fps. According to ffprobe it is 22311/1024.
jiggunjer
<ffmpeg><editing><audio><filter>
Accurate audio selection
jiggunjer
# Accurate audio selection

(Asked by: username_0 on 2015-12-16)

How do I select the audio corresponding to a set of filter-selected videoframes? 
E.g. at the moment I am sampling 1 in every 100 frames of a video (running at 15fps) using the FFMpeg select filter. When I try to select the audio of those frames with the aselect filter, the total audio duration is not equal to the video duration (2 or 3 seconds difference depending on which frames I selected). The audio sampling rate is listed as 22050Hz.
I tried using various -vsync and -async combinations but it made no difference. Is there a more accurate way of using aselect?
A quick example command:
ffmpeg -i %INPUT% -vf select='between(n\,200\,399)',setpts='PTS-STARTPTS' -af aselect='between(n\,200\,399)',asetpts='PTS-STARTPTS'-y test.mkv
where 'INPUT' is the name of some file containing compressed video and audio. I am testing with the file AV36_1.avi found on this site hosting video samples. I get a video duration of 13.3 seconds and audio duration of 12 seconds.
output: 
ffmpeg version N-77045-ga16243a Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 5.2.0 (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-libbs2b --enable-libcaca --
enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-l
ibilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enab
le-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --en
able-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --ena
ble-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc
 --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enabl
e-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --
enable-lzma --enable-decklink --enable-zlib
  libavutil      55.  9.100 / 55.  9.100
  libavcodec     57. 16.101 / 57. 16.101
  libavformat    57. 19.100 / 57. 19.100
  libavdevice    57.  0.100 / 57.  0.100
  libavfilter     6. 20.100 /  6. 20.100
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Guessed Channel Layout for  Input Stream #0.1 : stereo
Input #0, avi, from 'AV36_1.avi':
  Duration: 00:00:32.93, start: 0.000000, bitrate: 2372 kb/s
    Stream #0:0: Video: indeo5 (IV50 / 0x30355649), yuv410p, 320x240, 2058 kb/s,
 15 fps, 15 tbr, 15 tbn, 15 tbc
    Metadata:
      title           : Steyr.avi ┬ΦΣσε #1
    Stream #0:1: Audio: adpcm_ms ([2][0][0][0] / 0x0002), 22050 Hz, 2 channels,
s16, 176 kb/s
    Metadata:
      title           : Sound Forge 4.0 Audio
Output #0, avi, to 'test.avi':
  Metadata:
    ISFT            : Lavf57.19.100
    Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 320x240, q=2-31, 200
 kb/s, 15 fps, 15 tbn, 15 tbc
    Metadata:
      title           : Steyr.avi ┬ΦΣσε #1
      encoder         : Lavc57.16.101 mpeg4
    Stream #0:1: Audio: mp3 (libmp3lame) (U[0][0][0] / 0x0055), 22050 Hz, stereo
, s16p
    Metadata:
      title           : Sound Forge 4.0 Audio
      encoder         : Lavc57.16.101 libmp3lame
Stream mapping:
  Stream #0:0 -> #0:0 (indeo5 (native) -> mpeg4 (native))
  Stream #0:1 -> #0:1 (adpcm_ms (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
frame=   98 fps=0.0 q=9.6 size=     294kB time=00:00:09.03 bitrate= 266.1kbits/s
frame=  200 fps=0.0 q=11.7 Lsize=     525kB time=00:00:13.33 bitrate= 322.5kbits
/s
video:429kB audio:72kB subtitle:0kB other streams:0kB global headers:0kB muxing
overhead: 4.625268%

I think I'm using the aselect filter wrong...

**Tags:** ffmpeg, editing, audio, filter

> **Comments:**
> **username_1**: Paste your command line used and console output.  
> **username_0**: @Mulvya done :)  
> **username_1**: Please reformat the console properly, like first box [here](http://video.stackexchange.com/q/16987/1871).  
> **username_1**: Your command is selecting all frames from #200 to #399, not 1 of 100, and your sample file is indicated as "Very strange avi sample containing some stray LIST fixed by r20118" Maybe try with a sane sample. I'll test myself in the evening and get back  
> **username_0**: The exact selection doesn't matter, the point is that the corresponding audio selection has a large duration discrepancy. This trivial example should involve less possible complications than a more involved selection of frames (like `not(mod(n,100)) -vsync 0`). It would also be harder to select the corresponding audio frames.  
> **username_1**: Looks like a known problem: http://superuser.com/q/866144/114058  
> **username_0**: @Mulvya Meanwhile I think I discovered it is because of the audio framerate difference. I assumed it equal to the video framerate. The problem in your link assumes the audio framerate is 48fps because the sampling frequency was 48kHz. But it seems both assumptions are dangerous. In this case the audio framerate is about 21.8fps. By correcting the `aselect` frames with a factor `21.8/15` I get a much more accurate audio track. A few milliseconds difference. :D  
> **username_1**: If aselect is using audio frames for reference, then try 22.05/15 and if that makes it more accurate  
> **username_0**: @Mulvya No, the audio framerate is not 22.05fps. According to ffprobe it is 22311/1024.  

---

## 1 answers

### Answer by username_1 on 2015-12-16. Score: 2 | Accepted answer

This command, which used t reference for aselect, worked for me:
ffmpeg -i AV36_1.avi -vf "select=between(n\,200\,399),setpts=PTS-STARTPTS" -af "aselect=between(t\,(200/15)\,(399/15)),asetpts=PTS-STARTPTS" -y test.mkv

If you want to use n (frames), then you will need to know the frame size of the audio codec e.g. 1024 samples for AAC, variable for MP3..etc

> **Comments:**
> **username_0**: Cool, but this conversion won't work if the video has variable framerate. The info about audio frame sample sizes is interesting, I thought the sampling rate (22050Hz in this case) was always constant over time.  
> **username_1**: Sampling rate is constant, but compressed audio streams chunk `n` samples into `frames` which don't correspond to video frame boundaries. Your method won't work for VFR either, as the audio frame rate you're getting from ffprobe is CFR. A path to a general solution is to obtain the PTS of the first and last selected video frames and then supply the equivalent times to the aselect filter.  
> **username_0**: Then it seems timestamps are the safest way. So would an mp3 frame with 100 samples last 10 times longer than a frame with 10 samples? If `aselect` can only select entire audio frames this could limit accuracy. But my gut is telling me that `aselect` selects samples instead of frames (when using timestamps at least).  
> **username_1**: No. Depends on the sampling rate. If you have a 48000 Hz rate, then a typical 1152 samples MP3 frame is 0.04166 seconds of audio. If 24000 Hz, then 0.0833 s.  
> **username_0**: but a typical MP3 frame does not exist, as they have a variable frame size...so some frames will have 1500 samples, others 900, etc. Ah but the timestamps might not be as linear as with video.  

---

