! `) u4 d5 d& W3 j2 h为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的9 m- t8 p' U+ e G. }" z L
sampleRate为16000的格式。
5 h7 ` g+ ^+ l+ f* d本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。
' u7 T- G) y4 C* f, e执行后,会在目标目录生成和源文件相同目录结构的wave文件,/ O" V2 ]( }, r; c3 M5 U6 n
供语音识别之用。: C( W) t& E5 A% p: @4 _8 s- K: O
) q E* ?9 Q" V. X& h I! x8 H9 C
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,
' x. q5 _/ s6 V) T, k6 z: K- P4 R可以从以下软件中获得。
2 x$ h& A2 @ Y f& i0 s3 d( w8 ohttps://softaro.net/ffmpeggui/# z! p. Q* n' ?# w2 K( Z
9 G" r" {& \( w1 [$ n工具代码如下,vs2022编译即可
) x. @) q% z9 V- `3 l1 j- using System;
# t6 k, i3 r! r7 b7 E$ | - using System.Diagnostics;7 n5 V0 E" r8 R% n/ J4 Q
- using System.IO;+ j8 [- @3 C! E& K4 I% F' x
% Y* c7 O y% Q2 ]1 B# T2 a- class Program
6 c0 X& v) |1 O4 C - {4 ^$ D( L# A \, I# h% K k
- static void Main(string[] args)
( I0 i- J' s2 I; ^2 E( O - {0 J& |3 @ O7 x8 t' X: c
- Console.WriteLine("cvtWave");
8 R2 S8 X3 }# k8 B V G, M - Console.WriteLine("请输入源文件夹路径 (folder1):");
& @" O; s! }% j - string sourceFolder = Console.ReadLine();% G) }" d* _6 i" m
- Console.WriteLine("请输入目标文件夹路径 (folder2):");
+ N. ]" j% f2 b& v u6 c2 b - string destinationFolder = Console.ReadLine();
! c1 f( ]7 }9 S( ~/ v - : \4 m5 G* B8 J% L: M/ K5 V
- ConvertFilesToWAV(sourceFolder, destinationFolder);
& ^0 T$ r4 c7 f& R1 [' E" s - ?1 P; C% ^. B% d
- Console.WriteLine("转换完成.");
' m: M3 H t" @, R* [ B" a/ J8 a - }
; x. s, I: [! m5 r2 f4 x6 R
) v& u9 ^8 o% T8 ~ g1 Z! J- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
2 i4 j0 r! v7 L7 x- H$ `( F - {; b: `- n4 U6 R
- foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
9 x$ f1 A9 `- q4 I4 `0 S( v - {2 q* Y- N# B) |: @! f9 r
- string extension = Path.GetExtension(sourceFilePath).ToLower();
- ?% N! f. s& y# ?& z - . B. @( t/ [! c2 b# j- L3 E8 x
- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")# x, j( W. D C; W; m. Z1 a
- {
$ Z7 s! u/ K1 w0 T9 Q - string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);7 p5 D' |4 ~; E" Y( R
- string destinationFilePath = Path.Combine(destinationFolder, relativePath);
2 v: _( D4 B! \4 B: w* |, r - destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");
) J1 _& ~9 h6 M8 [+ v" p1 b - 9 [0 L# ^4 t# a' o# o: o, j6 V* f
- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
5 m. x9 I, B/ z8 X: x - ( J) f# q3 W, a, ~
- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";3 c: }& x8 `% D5 {/ C- Q% ^
- % K. i$ A f1 a9 ~. g
- Process ffmpegProcess = new Process1 `* S6 H0 r# T3 _
- {
5 m. Z0 w; X5 V: k& T! M2 { - StartInfo = new ProcessStartInfo
& |4 B! |( K# C# ?/ z* }; M - {, V& O q$ I2 _* K1 k" Q0 V, Z
- FileName = "ffmpeg64.exe",
- F* a/ b" K: `, t, k6 } - Arguments = ffmpegArgs,, m( [" e8 Z6 `
- UseShellExecute = false,
2 [3 s: ?/ A; q/ x0 I/ \- _$ `$ _ - RedirectStandardError = true," `$ x q1 j- B( w) H
- CreateNoWindow = true
, v+ F1 P0 s3 m# T - }6 ^6 s$ p. Z6 l4 e5 b
- };! H8 d0 G" {* E( _( m3 [: _
: V/ [5 [3 o8 ^5 ^/ o* ]" G- ffmpegProcess.Start();
5 u8 E. k% s4 R
0 R6 }& g% o! {9 R+ R- while (!ffmpegProcess.StandardError.EndOfStream). [" k- j7 w' [
- {
: c' P4 l8 n' Q3 W% H& Y2 J - string line = ffmpegProcess.StandardError.ReadLine();4 r& I; u* P0 @% ~2 t6 m
- Console.WriteLine(line); L V1 h1 t& V. ?$ l
- }- o+ x% @) C$ P! x0 z3 {( G# {( O
% e J: T |$ s1 K S- ffmpegProcess.WaitForExit();1 O; h5 m( {% S
- }
- g5 ~" [2 f5 x3 v+ a, E& Z+ e* L! t - }
( h1 ~" a/ W* J - }
4 c: g& r) ?8 y0 I. j' S - }
5 e7 b9 |7 N8 M% Y) y$ J- I1 r) r& m
复制代码 - H. R- F0 G2 {' @- f
/ h: u! t& g- |) S+ C
0 A, V% g: M0 L
+ }' r1 n) A& ]1 N
|