7 ?$ t" p' j$ p9 Z" E0 {8 w
为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的) s; Q' p" A' Z2 [" C" U
sampleRate为16000的格式。
# V( P {$ y7 ?- ?: S本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。
/ y' I: C4 L% X; i执行后,会在目标目录生成和源文件相同目录结构的wave文件,
+ ^% ]! J( `8 [3 B5 x供语音识别之用。" z: G3 e3 y I$ P% t
: B" T/ M3 p3 Q" W; K# N+ F; O3 V
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,
0 A* @8 E' B# h可以从以下软件中获得。1 _2 g# A- J& i: c
https://softaro.net/ffmpeggui/
2 A% V1 ]7 Q' z9 r% W: I+ e$ a' z/ m9 ~7 i) u1 b# ]
工具代码如下,vs2022编译即可% C0 q+ [( c5 f7 p. K; W
- using System;2 V2 s+ t9 m+ V) k' o
- using System.Diagnostics;
/ c) s) o7 _2 d+ E - using System.IO;
7 E) u8 K9 R7 L9 W
; H7 u6 [0 b. p3 |& H1 [ s' Z' @- class Program
; z- U: v, ~" ^, |2 o0 i5 \ P - {' J: L* A: W9 z9 E# p9 r
- static void Main(string[] args)
: I- A; \' d) x8 D: Q - {, U/ B: k: l4 j# l, G* a
- Console.WriteLine("cvtWave");% B$ g! `; J" B; ` W! T
- Console.WriteLine("请输入源文件夹路径 (folder1):"); _9 l( v0 Y6 Q/ P
- string sourceFolder = Console.ReadLine();" ?1 _* [; M( ]' Y/ g
- Console.WriteLine("请输入目标文件夹路径 (folder2):");
, \+ Y5 k, ]( y6 ]' z$ a - string destinationFolder = Console.ReadLine();$ {8 L0 p% X# x# v% Y( S6 b
- ! n1 e; V F8 B
- ConvertFilesToWAV(sourceFolder, destinationFolder);0 h$ _5 i, g7 W/ p5 m
- + u+ \$ Q! e; s! _: a+ A, w
- Console.WriteLine("转换完成.");
6 `7 s+ G$ A0 q( t+ u' v* E5 Z5 w - }* S3 z j. N( o2 U5 M& Z
: N6 H0 j& ~5 k9 C( R6 C# V0 R- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
" L( M+ T" |: q0 w" j7 k - {3 [0 W$ r M- b2 @ U7 W+ L
- foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))# I0 f% i0 X0 z2 L* G
- {
0 ]2 D: B& j5 v- M$ u - string extension = Path.GetExtension(sourceFilePath).ToLower();
h6 b; k. `0 M3 i7 O3 x - % c# K& n' U; e
- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")
( ~; V9 J) F" U6 _) k - {
6 N! a/ u6 I) N - string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);8 Y5 @9 ^ ~2 @" E7 l4 v
- string destinationFilePath = Path.Combine(destinationFolder, relativePath);
8 J9 C0 e, S# M9 m' h - destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");
* u+ u9 C9 l2 I1 J9 S7 Z - ( V- ?2 Z- d, K! z# o8 X6 k* A
- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));* D' G# b c9 U: b" V, q2 m
: W) T& P" G1 d( s8 H# T& i2 S/ A$ |5 c- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\""; s$ e' y6 z/ e& J0 M7 M% v
+ i+ x# ~2 { J! W! W# F% t- R6 g- Process ffmpegProcess = new Process) l' t- f9 J6 F5 U) H( M$ f
- {3 [5 Y! w, `6 t) x& ~$ O
- StartInfo = new ProcessStartInfo
& C+ J9 ?+ i& k1 |% j/ W0 n4 W9 T - {7 ?; A, N: P0 I* Z- `/ Q5 q# k! R
- FileName = "ffmpeg64.exe",) I) q5 B; q6 a
- Arguments = ffmpegArgs,
: m$ Q j* j* ]0 q3 ~7 i/ c - UseShellExecute = false,8 ]$ ?5 }( h. y) K2 e
- RedirectStandardError = true,$ b+ P! F1 c! u/ T8 a
- CreateNoWindow = true/ J `' \2 \" y
- }
& y- B2 `) _& U) O - };
7 H S% O; S8 y- s9 q6 h - 5 N2 p2 S* K3 ]( L! G
- ffmpegProcess.Start();
* W/ s) a: q: y# i1 k/ R
w8 c! D: @6 A/ e+ s- while (!ffmpegProcess.StandardError.EndOfStream)
! u2 ^: f5 S# Y0 G" D - {" m2 v6 Q0 a; [3 X! ]8 }
- string line = ffmpegProcess.StandardError.ReadLine();
. F' c& C; d$ F - Console.WriteLine(line);) n: n; A6 C+ [5 P7 a
- }
4 k) n3 p- d/ r0 {' z- |$ l - - g; c( M0 E, b, t+ I' F1 z; x
- ffmpegProcess.WaitForExit();5 [# r5 u7 Y5 H5 }# J
- }
1 T* J% L! B6 ^- e# e - }! [! r; J+ k! d+ ]6 D; O# n
- }
# F m, I$ x8 M& K2 I/ h1 w7 k - }% N& k: e! ]3 f9 K- Y: o
复制代码 ) t3 O! N! ?/ E6 f2 B# X9 k
) i% ~. U( o `3 ?1 G+ R* S7 r1 n) y' H! D8 \1 X
/ }6 F* S: g! j8 l( P- y' Y6 U
|