如何计算由LAME或FFMPEG添加的额外样本的数量 [英] How to compute the number of extra samples added by LAME or FFMPEG

查看:70
本文介绍了如何计算由LAME或FFMPEG添加的额外样本的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Python构建MP3解码器/解析器,以支持由LAME或FFMPEG编码的文件.

I am attempting to build a MP3 decoder / parser in Python which supports files encoded by LAME or FFMPEG.

我的编码shell脚本显示在这里:

My encoding shell script is shown here:

#!/bin/bash
for i in wav/*.wav; do
    i=${i##*/};
    lame --nores --strictly-enforce-ISO -t --cbr -b 64 -h "wav/${i}" "mpeg/lame/${i%.wav}.mp3";
    ffmpeg -i "wav/${i}" -codec:a libmp3lame -qscale:a 2 "mpeg/ffmpeg/${i%.wav}.mp3";
done

此脚本读取位于 ./wav/中的WAVE文件,并在我的 ./mp3/lame/目录中生成64kbps的受控比特率MP3和一个变量我的 ./mp3/ffmpeg/中的质量为2的MP3比特率.

This scripts reads WAVE files located in ./wav/ and produces a controlled-bitrate MP3 of 64kbps in my ./mp3/lame/ directory, and a variable-bitrate MP3 of quality 2 in my ./mp3/ffmpeg/.

我编写了一个Python脚本,该脚本遍历两个最终的MP3,对帧和样本的数量进行计数.LAME和FFMPEG结果(在帧和样本方面)是等效的,但是它们的二进制文件是不同的.

I have written a Python script that iterates through both resultant MP3s, counting the number of frames and samples. Both the LAME and FFMPEG results are equivalent (in terms of frames and samples), but their binary files are different.

LAME/FFMPEG样本计数是通过遍历二进制MP3文件,查找并解析帧头,然后使用MP3规范确定每帧样本数来完成的.

The LAME/FFMPEG sample count was done by iterating through the binary MP3 files, locating and parsing the frame header, then using the MP3 spec to determine the number of samples per frame.

  • MP3数据帧数:112(忽略Xing/Info第一帧)
  • 输出帧数:112 * 576 = 64512

这里是单个4秒输入文件的样本计数的比较:

Here is a comparison of the sample count for a single 4-second input file:

  • 输入的WAV样本数= 62996
  • 输出LAME/FFMPEG的样本数= 64512
  • 差异= 1516

我了解根据LAME FAQ文件,结果MP3文件为零在正面和背面进行填充,以确保正确执行逆MDCT,而且还因为窗口重叠.

I understand that according to the LAME FAQ file, resultant MP3 files are zero padded in the front and back to make sure the inverse MDCT is performed properly, but also because the windows overlap.

从上面的常见问题解答或任何以前的StackOverflow帖子中我无法确定的是如何计算人工添加的样本数.如果我可以确定所有1516个样本均为零,并且可以确定它们在字节流中的位置,那么我希望能够放心地将它们扔掉.由于有1516个额外"样本,并且对于V2LIII编码,每帧有576个样本,因此这里必须有两个以上(但少于三个)错误的MPEG帧.

What I can't ascertain from the above FAQ, or from any previous StackOverflow post, is how to compute the number of artificially added samples. If I can be sure that all 1516 of these samples are zeros, and I can be sure of their position in the bytestream, I'd like to be able to confidently toss them out. Since there are 1516 "extra" samples and there are 576 samples per frame for a V2LIII encoding, then there must be more than two (but less than three) erroneous MPEG frames here.

这里有没有人对MPEG编码/解码有足够的了解,可以知道添加了多少样本,这些样本将位于哪些帧中?换句话说,第一帧和最后一帧将始终包含空白数据,还是会有更多帧?

Is anyone here savvy enough with MPEG encoding/decoding to know how many samples are added, and in which frames those samples will be in? In other words, will the first frame and last frame always contain blank data, or are there more frames?

推荐答案

最简单的方法是使用具有日志级别调试模式的ffmpeg解码生成的MP3.

The easiest way to do this is to decode the resultant MP3s with ffmpeg with loglevel debug mode.

ffmpeg -i file.mp3 -f null - -v 48

在控制台输出中,您将有以下一行

Within the console output, you'll have this line

[mp3 @ 0000000002be28c0] pad 576 1105

这不包括固定的编码器延迟.

This doesn't include the fixed encoder delay.

所以这两行显示了实际跳过的样本数

So the actual skipped sample count is shown by these two lines

在第一帧中开始填充:

[mp3 @ 0000000002e6bb80] skip 1105/1152 samples

在最后一帧结束填充:

[mp3 @ 0000000002e6bb80] discard 576/1152 samples

仅在写入Xing标头时才显示此信息.

This info is only present if the Xing header is written.

这篇关于如何计算由LAME或FFMPEG添加的额外样本的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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