英文:
How to generate an empty .wav file (duration = 0, no silence/blank .wav) in ffmpeg?
问题 {#heading}
背景 {#heading-1}
我们需要在逻辑中生成静音的音频文件,并使用以下的ffmpeg命令:
ffmpeg -f lavfi -t $duration -i anullsrc=channel_layout=stereo:sample_rate=48000 -y $output
一切都正常,直到我们在生产环境中遇到异常:
生成静音音频时出错,长度:0,目标文件:xxxx.wav,
错误:av_interleaved_write_frame():设备上没有剩余空间
[wav @ 0x681fb80] 文件大小 1258523197518 对于 wav 无效,
输出文件将损坏 错误写入尾部
如同之前的日志所述,这是因为当 $duration == 0
时,-t 0
会导致ffmpeg生成无限长度的文件,直到耗尽磁盘空间,然后出现异常。
问题 {#heading-2}
- 为什么
-t 0
会生成无限长度的音频,而不是一个持续时间为0的wav文件? - 如何在ffmpeg中生成一个空的
.wav
/音频文件。
PS,我找到了这个 https://stackoverflow.com/questions/10464669/objective-c-generating-empty-wav-file ,但我想在ffmpeg中实现。我只找到了与"生成静音"相关的问题,没有找到任何关于
duration=0
的问题。也许在实际情况中很少遇到这种情况? 英文:
Background {#background}
We need to generate silence wav in our logic, and using the following ffmpeg command:
ffmpeg -f lavfi -t $duration -i anullsrc=channel_layout=stereo:sample_rate=48000 -y $output
All is ok untile we got a excetion in production environment:
Error generating silence audio, length: 0, target: xxxx.wav,
error: av_interleaved_write_frame(): No space left on device
[wav @ 0x681fb80] Filesize 1258523197518 invalid for wav,
output file will be broken Error writing trailer of
As in previous log, it's because -t 0
will cause the ffmpeg generating unlimited file untile it blows up the disk when $duration == 0
, then exception.
Question {#question}
- Why
-t 0
generate unlimited-length audio instead of aduration=0
wav? - And how to generate a epmty
.wav
/audio file in ffmpeg.
> PS, I found it https://stackoverflow.com/questions/10464669/objective-c-generating-empty-wav-file , but I want do do it in ffmpeg. I only get the silence generating
questions and haven't search any question about duration=0. May be it's too rare to meet in real condition?
答案1 {#1}
得分: 1
这将为您生成一个没有音频数据的WAV文件。
ffmpeg -f lavfi -t 1 -i anullsrc -bsf noise=drop=1 empty.wav
如果您计划以后追加媒体,
-
确保您的anullsrc属性与您将要追加的数据匹配(通道布局、比特深度等)。基本上,ffmpeg写入的头部应该与最终的有效负载匹配。
-
注意从末尾的第4个字节的偏移量。从那里开始的4个字节存储了PCM数据的字节大小,按小端顺序排列。在上面生成的文件中,它将为零。在完成追加后,请替换它。
英文:
This will get you a WAV with no audio data.
ffmpeg -f lavfi -t 1 -i anullsrc -bsf noise=drop=1 empty.wav
If you plan to append media later on,
-
make sure your anullsrc attributes match that of the data you will be appending (channel layout, bit depth..etc). Basically the header that ffmpeg writes should match the ultimate payload.
-
Note the offset of the 4th byte from the end. The 4 bytes starting there store the byte size of the PCM data in little endian order. It will be zero in the file generated above. Replace it after you are done appending.
答案2 {#2}
得分: 0
-t 0 指示 lavfi 创建时长为 0 秒的内容。
anullsrc 没有明确的持续时间。
出于某种原因,FFmpeg 选择了 anullsrc 的未定义长度,并进入了无限流渲染,直到磁盘因巨大的文件大小而满。
将零持续时间指令添加到 anullsrc,以下是带有修改的命令:
ffmpeg -f lavfi -t $duration -i anullsrc=channel_layout=stereo:sample_rate=48000:d=$duration -y $output
顺便说一下,不建议使用 0 持续时间,因为没有音频样本写入到 $output。
静音意味着振幅为 0 的样本。
如果文件中没有音频被渲染,很难预测您打算在后续的处理中使用此文件会发生什么。
英文:
the -t 0 directs lavfi to create a 0 seconds.
the anullsrc has no explicit duration.
for some reason, ffmpeg chooses the undefined length of anullsrc and enters an endless stream rendering that ends when the disk is full due to the huge file size.
add a zero duration instruction to anullsrc, here's your command with the modification:
ffmpeg -f lavfi -t $duration -i anullsrc=channel_layout=stereo:sample_rate=48000:d=$duration -y $output
btw - 0 duration is not recommended, since there are no audio samples written into $output.
silence means samples at 0 amplitude.
if no audio is rendered into the file it is hard to predict what will happen later in the process you're intending using this file.