如何检测 WAV 文件的标头是 44 字节还是 46 字节? [英] How can I detect whether a WAV file has a 44 or 46-byte header?

查看:128
本文介绍了如何检测 WAV 文件的标头是 44 字节还是 46 字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在样本开始之前假设所有 PCM wav 音频文件都有 44 字节的标头数据是危险的.尽管这很常见,但许多应用程序(例如 ffmpeg)会生成带有 46 字节标头的 wav,并且在处理时忽略这一事实会导致文件损坏且无法读取.但是你怎么能检测到标题实际上有多长?

I've discovered it is dangerous to assume that all PCM wav audio files have 44 bytes of header data before the samples begin. Though this is common, many applications (ffmpeg for example), will generate wavs with a 46-byte header and ignoring this fact while processing will result in a corrupt and unreadable file. But how can you detect how long the header actually is?

显然有一种方法可以做到这一点,但我搜索并发现很少有关于此的讨论.根据作者自己的上下文,很多音频项目都假设为 44(或相反,46).

Obviously there is a way to do this, but I searched and found little discussion about this. A LOT of audio projects out there assume 44 (or conversely, 46) depending on the authors own context.

推荐答案

诀窍是查看Subchunk1Size",它是一个 4 字节整数,从头的第 16 字节开始.在普通的 44 字节 wav 中,该整数将为 16 [10, 0, 0, 0].如果是 46 字节的标头,则该整数将为 18 [12, 0, 0, 0] 或者如果有额外的可扩展元数据(很少见?)甚至更高.

The trick is to look at the "Subchunk1Size", which is a 4-byte integer beginning at byte 16 of the header. In a normal 44-byte wav, this integer will be 16 [10, 0, 0, 0]. If it's a 46-byte header, this integer will be 18 [12, 0, 0, 0] or maybe even higher if there is extra extensible meta data (rare?).

额外数据本身(如果存在)从第 36 字节开始.

The extra data itself (if present), begins in byte 36.

所以一个简单的 C# 程序来检测头部长度应该是这样的:

So a simple C# program to detect the header length would look like this:

static void Main(string[] args)
{
    byte[] bytes = new byte[4];
    FileStream fileStream = new FileStream(args[0], FileMode.Open, FileAccess.Read);
    fileStream.Seek(16, 0);
    fileStream.Read(bytes, 0, 4);
    fileStream.Close();
    int Subchunk1Size = BitConverter.ToInt32(bytes, 0);

    if (Subchunk1Size < 16)
        Console.WriteLine("This is not a valid wav file");
    else
        switch (Subchunk1Size)
        {
            case 16:
                Console.WriteLine("44-byte header");
                break;
            case 18:
                Console.WriteLine("46-byte header");
                break;
            default:
                Console.WriteLine("Header contains extra data and is larger than 46 bytes");
                break;
        }
}

这篇关于如何检测 WAV 文件的标头是 44 字节还是 46 字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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