" I4 X6 K0 S% k5 b
为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的) P6 P( m; q: c6 q$ C
sampleRate为16000的格式。) z) N* @$ I D: ?- j
本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。$ D) e+ t, \- T( }/ U
执行后,会在目标目录生成和源文件相同目录结构的wave文件,
' _( G# J( Y9 c, t. ?供语音识别之用。3 v R" g- A- ^8 N% i5 r
3 E- D2 u; S* ~! G! [7 A9 R本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,2 s- i% v: u5 [% N7 ~5 s6 A
可以从以下软件中获得。& C5 @( T6 I4 l0 Z" p) r
https://softaro.net/ffmpeggui/0 `2 A$ d5 s7 R9 t2 z4 D- g: Z
# v7 U, |0 K Q8 T+ B) Z" ]
工具代码如下,vs2022编译即可
# @! F, o& y# n% w+ }- T* N- using System;( o# |! ~; w9 l+ x! W* z
- using System.Diagnostics;5 t; s k0 [( Q2 ]4 ?9 p2 M, N
- using System.IO;0 Y2 s) }# M& K) P
- 9 y. a5 r% P8 ?- ?# O
- class Program, ` ?/ v" p0 w& h- H' M: d
- {
+ O' j; b; N( v7 ~. K' @ - static void Main(string[] args)
`9 V4 |' B! V: ]# h: ? }& f- b - {, f; @, C$ }' u/ c( \! j" s' Z
- Console.WriteLine("cvtWave");9 W% f& E6 b: f& @
- Console.WriteLine("请输入源文件夹路径 (folder1):");
; i8 j# h& z" R. ^: \: _, } - string sourceFolder = Console.ReadLine();7 W2 w U1 Z- U3 z
- Console.WriteLine("请输入目标文件夹路径 (folder2):");
: e3 r% j1 J4 c& g8 ]3 V7 C - string destinationFolder = Console.ReadLine();3 b k3 e. k+ o1 y# l
5 K; p" L% l' }; d t6 ?- ConvertFilesToWAV(sourceFolder, destinationFolder);
! ]2 r" [1 I3 h% Y: Y/ W2 d - % Q7 y8 f- F# D
- Console.WriteLine("转换完成.");' {. e$ r% V6 V! m/ |
- } F6 j, K4 @- B- F9 Y5 G
( O6 C \0 k: B2 @0 o5 `6 h- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
" ~1 n5 h. c0 n& b- Y+ n$ H - {7 w( `/ x8 G* m' s
- foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))( d" R9 }1 I7 z
- {- ~$ C# l0 q8 [. q4 K F X! e6 f/ q/ f
- string extension = Path.GetExtension(sourceFilePath).ToLower();
# d E- i8 z+ h% e - 8 W S2 y4 @3 T$ q8 n( C2 b
- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")
4 `9 j2 A3 A/ q/ M8 a \5 x - {/ @) p' k; }! [9 ?% q: R
- string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);
4 c6 I1 K, ~6 s, Z5 b' Q- [ - string destinationFilePath = Path.Combine(destinationFolder, relativePath);7 p* W7 h6 w0 ?6 a
- destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");
5 v/ `/ m; B$ I# W$ q/ p
1 c5 Q: F! T# d; }. {- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
# M, p! I$ ^9 _! q) G- d
) K: Q; Q' X' i. f7 ~. W1 e" j- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";5 L/ W& M% ]% y, S- Z" M5 l6 O
- 6 b: [5 {0 m. D( B! Y! E. d$ |! i# f
- Process ffmpegProcess = new Process
4 r: m8 K% {4 r7 ^( O5 Y; ? - {4 F c: L9 E7 A7 i+ n+ d1 \
- StartInfo = new ProcessStartInfo
! W8 X7 a; O9 p4 Q - {' e9 R y1 A5 H
- FileName = "ffmpeg64.exe",* k9 A1 `8 n6 g% T2 M* I% {
- Arguments = ffmpegArgs,
8 n7 u; I) d+ L! l9 } - UseShellExecute = false,
' m W. p+ }" K; w7 l2 x @ - RedirectStandardError = true, z# k; k: t" f1 X/ K' G
- CreateNoWindow = true
# z2 Y S1 s" P - }
" x, L7 q4 b+ |" u2 q - };, b( }; F! u u( V9 P! G/ i
/ A/ K& ]. e" }8 I6 q6 @9 J7 P- ffmpegProcess.Start();) {0 J! ?! R( e
- . v: o7 j& P7 d1 P6 _
- while (!ffmpegProcess.StandardError.EndOfStream)
& d! C7 q# Q6 k i' Q - {
+ i+ I c' t. F$ z; N8 u. x - string line = ffmpegProcess.StandardError.ReadLine();2 h0 D. k! |( R" K" E, }
- Console.WriteLine(line);% t# X8 }7 u* E( Y" W# J
- }: T/ v1 Y- c1 K" \- \' b: [
- 5 E* I2 P8 W8 I* x. g9 y
- ffmpegProcess.WaitForExit();
$ k* l0 X# F, T( C7 J - }3 o6 T1 D/ V2 y( M
- }. [- D7 T4 z8 y$ L3 C% ~$ v
- }
* A; y- @" o: R6 i7 U6 Q F - }/ M: o0 q- v% Q7 w& j( J6 `: h4 z
复制代码 ' S, L. E- U6 W/ O' i( s
+ t: X/ |+ q. g* V
1 z- z' b3 }6 t* ^3 K# a
1 d9 q2 f4 O8 I1 F' Z2 Z- Q |