C#实战小技巧(十三):获取WAV音频文件信息和音频时长

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WZh0316/article/details/88414292

通过读取wav文件流,可获取音频文件时长等信息。
根据wav文件信息格式,构造下面的结构体,用于保存文件信息:

	/// <summary>
    /// WAV文件信息
    /// </summary>
    public struct WavInfo
    {
        public string groupid;
        public string rifftype;
        public long filesize;
        public string chunkid;
        public long chunksize;
        public short wformattag; //记录着此声音的格式代号
        public ushort wchannels; //记录声音的频道数。
        public ulong dwsamplespersec;//记录每秒取样数。
        public ulong dwavgbytespersec;//记录每秒的数据量。
        public ushort wblockalign;//记录区块的对齐单位。
        public ushort wbitspersample;//记录每个取样所需的位元数。
        public string datachunkid;
        public long datasize;
    }

读取wav文件流后,根据信息格式解析出文件信息:

		/// <summary>
        /// 获取WAV文件信息
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <returns></returns>
        private WavInfo GetWavInfo(string fileName)
        {
            WavInfo wavInfo = new WavInfo();
            FileInfo fi = new FileInfo(fileName);
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                if (fs.Length >= 44)
                {
                    byte[] bInfo = new byte[44];
                    fs.Read(bInfo, 0, 44);
                    Encoding.Default.GetString(bInfo, 0, 4);
                    if (Encoding.Default.GetString(bInfo, 0, 4) == "RIFF" && Encoding.Default.GetString(bInfo, 8, 4) == "WAVE" && Encoding.Default.GetString(bInfo, 12, 4) == "fmt ")
                    {
                        wavInfo.groupid = Encoding.Default.GetString(bInfo, 0, 4);
                        System.BitConverter.ToInt32(bInfo, 4);
                        wavInfo.filesize = System.BitConverter.ToInt32(bInfo, 4);
                        wavInfo.rifftype = Encoding.Default.GetString(bInfo, 8, 4);
                        wavInfo.chunkid = Encoding.Default.GetString(bInfo, 12, 4);
                        wavInfo.chunksize = System.BitConverter.ToInt32(bInfo, 16);
                        wavInfo.wformattag = System.BitConverter.ToInt16(bInfo, 20);
                        wavInfo.wchannels = System.BitConverter.ToUInt16(bInfo, 22);
                        wavInfo.dwsamplespersec = System.BitConverter.ToUInt32(bInfo, 24);
                        wavInfo.dwavgbytespersec = System.BitConverter.ToUInt32(bInfo, 28);
                        wavInfo.wblockalign = System.BitConverter.ToUInt16(bInfo, 32);
                        wavInfo.wbitspersample = System.BitConverter.ToUInt16(bInfo, 34);
                        wavInfo.datachunkid = Encoding.Default.GetString(bInfo, 36, 4);
                        wavInfo.datasize = System.BitConverter.ToInt32(bInfo, 40);
                    }
                }
            }

            return wavInfo;
        }

获取wav文件信息后,可计算出音频时长:

		/// <summary>
        /// 获取音频文件长度
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <returns>返回单位(秒)</returns>
        private int GetVoiceDuration(string fileName)
        {
            int duration = 0;
            try
            {
                WavInfo wavInfo = this.GetWavInfo(fileName);
                duration = Convert.ToInt32(wavInfo.datasize / Convert.ToInt64(wavInfo.dwavgbytespersec));
            }
            catch (Exception ex)
            {
                LOG.Error(new StringBuilder("获取音频文件长度出错:").Append(fileName).Append(", ").Append(ex.Message));
            }

            return duration;
        }

猜你喜欢

转载自blog.csdn.net/WZh0316/article/details/88414292
0条评论
添加一条新回复