失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 用ffmpeg批量转换WAV文件采样率

用ffmpeg批量转换WAV文件采样率

时间:2022-11-18 16:38:17

相关推荐

用ffmpeg批量转换WAV文件采样率

问题由来:有一批客服提供的配音文件,好几千个,需要用作我写的程序合成,发现其尺寸差异较大,遂检查了一遍,发现果然里面10%的文件格式有问题,和其他不一致:

Option ExplicitPublic Type RIFFID As String * 4 '0x00 4Byte 大端 'RIFF' (0x52494646)Size As Long '0x04 4Byte 小端 fileSize - 8Type As String * 4 '0x08 4Byte 大端 'WAVE'(0x57415645)End TypePublic Type FORMATID As String * 4 ' 0x00 4Byte 大端 'fmt ' (0x666D7420)Size As Long ' 0x04 4Byte 小端 16AudioFormat As Integer ' 0x08 2Byte 小端 音频格式NumChannels As Integer ' 0x0A 2Byte 小端 声道数SampleRate As Long' 0x0C 4Byte 小端 采样率ByteRate As Long ' 0x10 4Byte 小端 每秒数据字节数BlockAlign As Integer ' 0x14 2Byte 小端 数据块对齐BitsPerSample As Integer ' 0x16 2Byte 小端 采样位数End TypePublic Type dataID As String * 4 ' 0x00 4Byte 大端 'data' (0x64617461)Size As Long ' 0x04 4Byte 小端 NdataBlock() As IntegerEnd TypePublic Type HP1data As String * 14 '0x00 4Byte 大端 Lavf58.29.100End TypePublic Type HPID As String * 8 '0x00 4Byte 大端 INFOISFTSize As Long '0x04 4Byte 小端data As HP1End TypePublic Type LIST '-------------ffmpeg 转换出来的 WAV 文件,文件头会多出 LIST 块,所以单独定义个判断下,以免忘记了。ID As String * 4 '0x00 4Byte 大端 LISTSize As Long '0x04 4Byte 小端data As HPEnd TypePublic Function apppath()apppath = ActiveWorkbook.Path & "\"End FunctionPublic Sub chkWavFormat()Dim P1 As RIFF, P2 As FORMAT, P3 As data, P2x As LISTDim P0 As RIFF, AR As Long, dataBlock() As IntegerDim i As Integer, fn As String, fpath As String, fp As Integer, col As IntegerDim Headsize As Longfpath = apppath & "基本语音\wav\"fn = Dir(fpath & "*.wav")Do'-----------读取文件格式fp = FreeFileOpen fpath & fn For Binary As #fpGet #fp, , P1Get #fp, , P2Get #fp, , P0AR = Seek(fp) - 12Seek #fp, ARIf P0.ID <> "data" ThenGet #fp, , P2xHeadsize = 12 + 8 + P2.Size + 8 + P2x.Size + 8ElseHeadsize = 12 + 8 + P2.Size + 8End If' Get #fp, , P3.ID' Get #fp, , P3.SizeIf P2.BlockAlign = 1 Then'ReDim dataBlock(1 To P3.Size) As ByteElseIf P2.BlockAlign = 2 Then'ReDim dataBlock(1 To P3.Size / 2) As IntegerElse'Close #fp'MsgBox "不处理32位音频!"'Exit SubEnd IfClose #fp'------------------i = i + 1col = 1Worksheets("Sheet3").Cells(10 + i, col).Value = i: col = col + 1Worksheets("Sheet3").Cells(10 + i, col).Value = fn: col = col + 1With P2Worksheets("Sheet3").Cells(10 + i, col).Value = .AudioFormat: col = col + 1Worksheets("Sheet3").Cells(10 + i, col).Value = .NumChannels: col = col + 1Worksheets("Sheet3").Cells(10 + i, col).Value = .SampleRate: col = col + 1Worksheets("Sheet3").Cells(10 + i, col).Value = .ByteRate: col = col + 1Worksheets("Sheet3").Cells(10 + i, col).Value = .BlockAlign: col = col + 1Worksheets("Sheet3").Cells(10 + i, col).Value = .BitsPerSample: col = col + 1End WithWorksheets("Sheet3").Cells(10 + i, col).Value = Headsize: col = col + 1'——----------DoEventsfn = DirLoop Until Len(fn) < 4MsgBox i & "个文件处理完毕"End Sub

而且发现所有文件采样率和我程序要求的都不一样。

所以想将其格式全部转换为我设定的一致,用 ffpmeg 试了一下,发现直接 wav 转 wav 转出来得到是 PCM 格式文件,并不能得到 wav 格式文件:

ffmpeg -y -i 12.wav -f s16le -ac 1 -ar 16000 -acodec pcm_s16le 12.ok.wav

图中可将,wav 文件头没了,将扩展名改为 PCM 用 goldwave 打开,填入之前转换的参数,播放正确:

说明格式采样率转换已经正确完成,只是 ffpmeg 只输出了数据部分,没带 wav文件头,那么接下来就简单了,从 pcm 转为 wav 加上文件头,就可以得到完整的 wav 文件了:

ffmpeg -y -f s16le -ac 1 -ar 16000 -acodec pcm_s16le -i 12.ok.pcm test.wav

至此,转换方案可以确定,即,将各种不同格式的 wav 文件先转为 16位16K单声 pcm 格式,然后,在按这个格式转换为 wav 格式即可,加上批处理批量转换两次就完成了:

rem 不同格式的 wav 文件批量转换为 16 位 16K 单声道的 pcm 格式e:cd E:\Studio\web\xfttsWeb\DOC\基本语音for %%a in (".\all\*.wav") do "ffmpeg.exe" -y -i "%%a" -f s16le -ac 1 -ar 16000 -acodec pcm_s16le ".\pcm\%%~na.pcm"

rem pcm 文件批量转换为 16 位 16K 单声道的 wav 格式e:cd E:\Studio\web\xfttsWeb\DOC\基本语音for %%a in (".\pcm\*.pcm") do "ffmpeg.exe" -y -f s16le -ac 1 -ar 16000 -acodec pcm_s16le -i "%%a" ".\wav\%%~na.wav"

这样,几分钟就把一堆不同采样率的文件一起转换好了。

转换后的文件再次检测格式,都已经全部为制定格式:

此记!

ps:ffmpg 转换 wav 文件采样率时,不能直接转为 wav 格式,需要用 pcm 格式过渡一次。

如果觉得《用ffmpeg批量转换WAV文件采样率》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。