在ffmpeg中采样准确的音频切片? [英] Sample accurate audio slicing in ffmpeg?

查看:262
本文介绍了在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.

我正在使用的当前代码是

the current code I am using is

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个样本.请参阅下面的静默流的最后3帧的读数,该最后3帧在M4A中指定为10秒.相对于持续时间比较数据包大小.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆