8 w/ j& l# H' P1 D3 [9 C为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的9 e8 Y; w/ T$ _# @* x1 _
sampleRate为16000的格式。
9 Q, z$ Z9 o: s, {1 F2 ]( d本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。# \ Q# W4 P8 w1 ]' {
执行后,会在目标目录生成和源文件相同目录结构的wave文件,
( k8 |$ V; }3 V1 F9 X$ T供语音识别之用。
4 F3 j$ R: i% A! f
4 g* Y$ x% o9 G: B) w8 T本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,1 h, I" [; H, I# u
可以从以下软件中获得。
3 L( M( L$ P7 h7 u2 shttps://softaro.net/ffmpeggui/ Z+ n& I& F2 E& h8 z( k
: a/ F% Q, d9 `% z
工具代码如下,vs2022编译即可9 }! W( k% N4 Y$ S# {
- using System;9 g% Z+ [& F8 ^$ O
- using System.Diagnostics;
7 H% m p! ] r - using System.IO;; U# A! P2 k. ~6 ^7 M
- 6 r& a: m+ j b+ @& ~
- class Program6 e. a8 }( B# q) v& A: n
- {
3 b5 T! m8 f' [ x I - static void Main(string[] args) J+ x! ~4 P$ L- }6 h; d9 O" B+ @
- {
9 R# _" [( x/ w& A+ } - Console.WriteLine("cvtWave");9 H) b" V# R$ ^' N
- Console.WriteLine("请输入源文件夹路径 (folder1):");8 @3 k) z7 z, x3 u8 r$ G7 G
- string sourceFolder = Console.ReadLine();
0 T. H3 j% H o - Console.WriteLine("请输入目标文件夹路径 (folder2):");
( U4 \( g3 D/ p1 _ - string destinationFolder = Console.ReadLine();5 Y2 f6 l1 `/ ~0 X6 R `
/ l. x: }& h5 O$ p2 @+ {/ [- ConvertFilesToWAV(sourceFolder, destinationFolder);2 z( m5 V8 l3 ^
& D9 T2 X( N; j5 x; s: P- Console.WriteLine("转换完成.");* ^. V7 b3 {# H, d0 G# Y
- }
N$ n% q4 x4 o' o
2 F4 E+ O* {; e: q/ [, ~) n S2 m- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
8 m8 e$ r& V1 U1 O - {
; |. B4 _: m( m6 D" u' ]+ f - foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))4 T8 b7 F0 }0 I- X
- {
3 k" M/ A+ ]- L9 \& |3 V - string extension = Path.GetExtension(sourceFilePath).ToLower();
, Y6 v$ M% o4 R4 p% z - 7 ^+ v* h+ ^2 a3 E }- i3 T, {
- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")0 P# l7 e& c+ Q/ U) U
- {- b5 h( B2 N- r% Z$ C
- string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);, r; \ a6 ?( g& ? `- I
- string destinationFilePath = Path.Combine(destinationFolder, relativePath);
5 l9 o: z( V: [1 i! b9 l, m( d4 i - destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");% y, p. Z! ~4 g! ?
- + x; ^3 L* Y' z" d( i
- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
# J. y3 x: k. t4 ~( S - 9 r3 D" x5 `; l
- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";
+ R- ~5 {1 c% N2 A - J$ X# H" C, w5 d+ A6 S7 Q
- Process ffmpegProcess = new Process
" ]# ]* A! l% J2 b* u - {
+ ?" i) q4 F- r- \' T% V - StartInfo = new ProcessStartInfo5 \2 s' c+ D$ r1 K6 `
- {
' M& G% c& Y2 R: W - FileName = "ffmpeg64.exe",
2 m2 l0 u. L- A6 H) c3 j8 L - Arguments = ffmpegArgs,
+ n! b1 ]# L: r' S: e' N - UseShellExecute = false,, W* }1 l: G/ u/ Y- v2 V
- RedirectStandardError = true,
0 U6 A/ ~7 _% Q, C. Q4 J; ~ - CreateNoWindow = true6 h0 ^# l( m" H7 Q/ B! B- @
- }2 E0 `& b& e7 D% s
- };3 t) g, o Q6 r
- . V' a V5 r* J) Z6 c
- ffmpegProcess.Start();' k v$ B$ c \/ T$ f
6 L0 X0 U- Q1 W. }- while (!ffmpegProcess.StandardError.EndOfStream)+ Y6 W2 L L5 T/ N; d& u/ }
- {$ ]! y/ I0 M" X
- string line = ffmpegProcess.StandardError.ReadLine();
# ^, O) e) n* I/ t% @* N$ _2 N - Console.WriteLine(line);
5 z% o5 w/ C7 _( ?) A/ ]% { - }" b( ?' Y4 M+ S) k/ @
3 T, ~1 f" Q2 O6 g) T- k- ffmpegProcess.WaitForExit();
( {. a4 ^5 l; G! N0 y - }* b3 R- @1 w" D
- }' P% t7 Y* h! c( y# I$ r9 ^, Q
- }% h+ K6 w' F4 S' |! f
- }
3 E8 p& d) z, y$ ?; f
复制代码
2 C1 D8 K3 j4 C' ]1 a& i0 z: Q: u# n. s' e& W
, x6 m5 T+ Z1 g" ~
' p5 B: Y3 j# y( h- ^ |