在 ffmpeg 中采样准确的音频切片? [英] Sample accurate audio slicing in ffmpeg?
问题描述
我需要将 .wav 格式的音频文件切成 10 秒的块.这些块需要正好是 10 秒,而不是 10.04799988232 秒.
I need to slice an audio file in .wav format into 10 second chunks. These chunks need to be exactly 10 seconds, not 10.04799988232 seconds.
我当前使用的代码是
ffmpeg -i test.wav -ss 0 -to 10 -c:a libfdk_aac -b:a 80k aac/test.aac
ffmpeg version 3.2.2 Copyright (c) 2000-2016 the FFmpeg developers
built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --disable-lzma --enable-nonfree --enable-vda
libavutil 55. 34.100 / 55. 34.100
libavcodec 57. 64.101 / 57. 64.101
libavformat 57. 56.100 / 57. 56.100
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libavresample 3. 1. 0 / 3. 1. 0
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, wav, from '/Users/chris/Repos/mithc/client/assets/audio/wav/test.wav':
Duration: 00:04:37.62, bitrate: 2307 kb/s
Stream #0:0: Audio: pcm_s24le ([1][0][0][0] / 0x0001), 48000 Hz, stereo, s32 (24 bit), 2304 kb/s
Output #0, adts, to '/Users/chris/Repos/mithc/client/assets/audio/aac/test.aac':
Metadata:
encoder : Lavf57.56.100
Stream #0:0: Audio: aac (libfdk_aac), 48000 Hz, stereo, s16, 80 kb/s
Metadata:
encoder : Lavc57.64.101 libfdk_aac
Stream mapping:
Stream #0:0 -> #0:0 (pcm_s24le (native) -> aac (libfdk_aac))
Press [q] to stop, [?] for help
size= 148kB time=00:00:15.01 bitrate= 80.6kbits/s speed=40.9x
video:0kB audio:148kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
此代码不会产生精确的切片,任何想法如何实现?
This code does not produce exact slices, any ideas how can this be accomplished?
推荐答案
不可能*.AAC 音频存储在解码为 1024 个样本的帧中.因此,对于 48000 Hz 的馈送,每帧的持续时间为 0.02133
秒.
Not possible*. AAC audio is stored in frames which decode to 1024 samples. So, for a 48000 Hz feed, each frame has a duration of 0.02133
seconds.
如果您将音频存储在 M4A 等容器中,该容器指示每个数据包的持续时间,则调整最后一帧的持续时间以满足指定的 t
/ss-to
.但是最后一帧仍然包含完整的 1024 个样本.请参阅下面对 M4A 中指定为 10 秒的无声流的最后 3 帧的读数.比较数据包大小与持续时间.
If you store the audio in a container like M4A which indicates duration per-packet, the duration of the last frame is adjusted to satisfy the specified t
/ss-to
. But the last frame still contains the full 1024 samples. See the readout below of the last 3 frames of a silent stream specified to be 10 seconds in a M4A. Compare the packet size(s) vis-a-vis the duration.
stream #0:
keyframe=1
duration=0.021
dts=9.941 pts=9.941
size=213
stream #0:
keyframe=1
duration=0.021
dts=9.963 pts=9.963
size=213
stream #0:
keyframe=1
duration=0.016
dts=9.984 pts=9.984
size=214
如果此流最初存储在 .aac
中,则总持续时间不会是 10.00
秒.现在,M4A 是否适合您将取决于您的播放器.
If this stream were originally stored in .aac
, total duration would not be 10.00
seconds. Now whether M4A does the trick for you will depend on your player.
*有一种 AAC 变体,可解码为 960 个样本.因此,可以将 48 kHz 音频编码为恰好 10 秒长的流.FFmpeg 不支持这样的 AAC 编码器.AFAIK,包括 iTunes 在内的许多应用程序都无法正确播放此类文件.如果您想按照此规范进行编码,可以在 https://github.com/Opendigitalradio/ODR 上找到一个编码器-AudioEnc
*there is a variant of AAC which decodes to 960 samples. So, a 48 kHz audio could be encoded to a stream exactly 10 seconds long. FFmpeg does not sport such an AAC encoder. AFAIK, many apps including itunes will not play such a file correctly. If you want to encode to this spec, there's an encoder available at https://github.com/Opendigitalradio/ODR-AudioEnc
这篇关于在 ffmpeg 中采样准确的音频切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!