- ]! e" u: g- x! A
为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的* F3 d' ^' @: o) T( o- t2 m0 p
sampleRate为16000的格式。
; J0 S' N A n$ C8 e本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。
4 t* w) [0 L+ S9 B执行后,会在目标目录生成和源文件相同目录结构的wave文件,
, M5 \- E# m3 g1 V' ]. _" y8 B供语音识别之用。6 Z; W' P7 @4 b) S, k0 ^4 L. d
* n4 p- G1 y/ v* f O8 p
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,8 s+ w4 i& {$ v% U6 y8 A
可以从以下软件中获得。
- m% e" D; G; |; N* p! J+ W& f% \https://softaro.net/ffmpeggui/
6 x0 k2 N8 K8 h9 E5 C) W& H+ z% C# }$ m1 m
工具代码如下,vs2022编译即可1 F2 i- A- X4 k& D" R- M
- using System;
e4 \& r0 ^) X. C- U. q - using System.Diagnostics;1 n( i8 b/ p& m/ Y
- using System.IO;. Q* a, V5 L7 Y$ J1 z
- % ~2 r$ v1 R3 S1 y- f6 s) m
- class Program
7 G x5 ]0 S' H! z - {, Q, G: u! w9 `- D. A
- static void Main(string[] args)/ [6 Z5 T+ y z- b
- {
; c7 v) E* [( U - Console.WriteLine("cvtWave");
4 u: M; T3 z$ S5 ?# m* A - Console.WriteLine("请输入源文件夹路径 (folder1):");. A2 o0 q) h5 ?+ K* g% h
- string sourceFolder = Console.ReadLine();$ l) H& K. Y! m! c% B0 U1 G/ |
- Console.WriteLine("请输入目标文件夹路径 (folder2):");- J/ M3 g0 U6 R& _& z! _: C
- string destinationFolder = Console.ReadLine();
7 F/ W; W" q2 O
% Z8 z5 b/ k+ R' t9 @$ `4 V- ConvertFilesToWAV(sourceFolder, destinationFolder);9 W2 E/ y8 \- }
- 9 A4 t7 D" { E3 |6 V2 G. U3 q
- Console.WriteLine("转换完成.");6 i3 ^! m5 g4 u
- }! T1 D' O6 K+ X& Z+ U: V# o! c
# K( w2 M( P$ L& `- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)' L0 x. J% B0 Z( x. _6 V! n5 s. T
- {% ^3 c: l, T" r+ N ?& J* E( g
- foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))# `8 I4 m% ~2 |' n- e
- {+ L7 ^( w# J3 ~( o3 b! n
- string extension = Path.GetExtension(sourceFilePath).ToLower();
, o5 @. O; ?/ n6 r) ^. M6 N; S - 1 B7 l, X& C: y4 ?+ [
- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")% w8 N3 ?5 \2 { F/ e" }
- { d' Z ~/ D$ l8 O$ ?3 W
- string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);
0 V A" @/ b+ R. Z. b4 Y* k: X7 M - string destinationFilePath = Path.Combine(destinationFolder, relativePath);
: }) w7 c r# M7 l5 c4 S - destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");! U" [% c1 Z9 K* j, p
- 6 }6 ?9 X; z2 }: }- U3 z0 w) ?
- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
+ D% [' \5 f% c1 V" L6 g
3 Z' F" g4 b6 M- B/ v/ }4 v- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";
- [. P" y2 K% H' @
- M' K7 {6 t( q- Process ffmpegProcess = new Process" @! G) u7 S8 z [7 Y: w8 u3 U
- {+ i Y( i9 Q x- C
- StartInfo = new ProcessStartInfo0 e2 Q. p( w+ B$ I1 u# l% h
- { P# a9 F; h4 b8 S' |# f2 {0 F3 C
- FileName = "ffmpeg64.exe",
- l- z) N+ B* h9 R - Arguments = ffmpegArgs,& p2 f0 l+ i6 r4 e Z
- UseShellExecute = false,
M Y: {# r8 p3 K4 U+ ? - RedirectStandardError = true,, @$ I6 a( S0 F0 ^& \$ }" y
- CreateNoWindow = true
]; H0 w5 u% C+ B3 r# Q - }" [4 |0 c- Q+ C) H$ h
- };
/ u* X% l( G% b* g
9 v! ?2 W. J& J6 r- ffmpegProcess.Start();6 L+ s& `) g2 A" x7 A8 N
0 N% s% l6 J: r# d/ x. N- while (!ffmpegProcess.StandardError.EndOfStream)7 z0 u$ ^! y$ s6 V9 M
- {" D4 @9 d- y6 g4 E/ @% g
- string line = ffmpegProcess.StandardError.ReadLine();
% o% F! |# Y3 [ - Console.WriteLine(line);
* L- E/ E1 B% B - }
2 t* w0 I' ]% q% N6 p3 ] - / x# B: A$ {& V+ q. h3 H: @* {1 C
- ffmpegProcess.WaitForExit();2 F9 n* ^0 }0 `/ x
- }0 d! w: H! z" x6 D" Z
- }
) ?0 @& \5 B" u - }: I( M* Q# r8 y+ Q1 X ^1 d
- }% P2 t( |( j. x
复制代码
$ y u4 e, F7 R$ N* |/ H
1 d! L9 K- ~9 ^* h; |) ~" j! u5 U$ `& [( m# r
. n$ Q# J6 b, U3 y3 w
|