0 g% P1 n( s6 s. S为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的1 P- B! A' x i& ]
sampleRate为16000的格式。, y; G# C0 V# Y! C, ~2 X8 B7 M
本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。
# X) a0 `6 p7 K6 u Z2 k执行后,会在目标目录生成和源文件相同目录结构的wave文件,+ |( s% D$ j+ x) j# {8 }
供语音识别之用。
9 i# F! c8 F6 U K) B( r) d& G5 ^) h9 f/ U5 m5 {1 O G
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,! ^% B7 D2 ^) _- s2 u
可以从以下软件中获得。
9 A+ m b" H0 z( z' Ghttps://softaro.net/ffmpeggui/- ~* S4 W3 \- S/ ^5 h
: I& J- ^1 b1 d5 \( e0 E+ g' }
工具代码如下,vs2022编译即可3 g2 G. o% S; ?7 R8 f# L/ |. Y) Q
- using System;
" p* k/ p9 |: i# w' S - using System.Diagnostics;
8 a. C. t. a# t% [ - using System.IO;
, C! m' X3 K) S! x - 8 b. S, e2 z! f( A8 N6 G
- class Program- E" p2 C, t) [. Y% c) U& p6 m
- {
& i( C [3 m# {5 T" l! ` - static void Main(string[] args)
) d' R( h: e `1 s& S - {
( x' b( f% [" M# Q5 a" D1 z' A" q - Console.WriteLine("cvtWave");
3 a% `. j/ }7 `( W$ M - Console.WriteLine("请输入源文件夹路径 (folder1):");9 G3 Z4 K$ Y9 y) f, B9 E' L5 j2 h
- string sourceFolder = Console.ReadLine();
! A& T8 n3 [" Q+ Y' b - Console.WriteLine("请输入目标文件夹路径 (folder2):");8 u- R. I8 @ _% l
- string destinationFolder = Console.ReadLine();1 ?( {% ~) _, N6 p5 V' I* T
5 [! i$ e! O. Y$ b7 J& h- ConvertFilesToWAV(sourceFolder, destinationFolder);1 [3 s, u5 n/ `$ w
8 D4 k3 ^4 ]. i- t- Console.WriteLine("转换完成.");
' Q5 H e! b/ o3 M; \ - }
3 |- ~' T* J% H3 E, P' h: F - 5 j8 h1 l G: `4 @5 l" v0 s
- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
8 Z6 }% C- R& A7 `# u3 } - {" H) }! Q# F1 @) W
- foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))2 E' G# a* E/ q2 c3 e
- {! w6 J- C k8 N" Z+ ]/ b# j+ g
- string extension = Path.GetExtension(sourceFilePath).ToLower();" G4 w' M5 u' c& Y9 q/ U+ ?) i
. `* d# I$ |7 M- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")
0 a+ ?1 B+ Q3 y+ u - {
, w" h: l* O6 a - string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);/ e7 \$ U9 N1 G$ U- [
- string destinationFilePath = Path.Combine(destinationFolder, relativePath); |9 G* Q) P: q. `/ j' `
- destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");3 k- o+ E% m; [' M) W
7 Q( }/ k: Q% i3 ?0 ]9 M- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
- ?. V) y% I" ^ - # O/ G. s- c: m7 c) h
- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";+ j+ z4 @# {' v. ^& O
- - }5 o2 B7 ?4 H- r7 n$ b/ {
- Process ffmpegProcess = new Process; D5 K$ S1 T/ ]' q q
- {
* ]* v" }9 \" U) \3 g* ~* o - StartInfo = new ProcessStartInfo) o# h, o) X+ C
- {& f, L6 V7 ?$ K+ c( j
- FileName = "ffmpeg64.exe",: g% U! v5 o* Q) u) x/ `
- Arguments = ffmpegArgs,
6 } C7 V4 F" `" G( D - UseShellExecute = false,
/ Q5 B' D8 i/ E2 i$ X$ y% z8 @* G - RedirectStandardError = true,
# p. T& l/ j |7 o) H4 P" u/ w - CreateNoWindow = true
$ U$ O) g8 u9 }3 u5 x - }
/ O6 y' A. H: w, M - };( v' ?) o& D* K# ^8 V
2 U+ {) J+ ]. Z# _1 l6 d- ffmpegProcess.Start();4 Y% ]$ N, F2 i H0 l2 m6 ^
- % H3 {/ A7 W( I u
- while (!ffmpegProcess.StandardError.EndOfStream)+ I. ]) ?# u6 e, l. b
- {
# u. M u" j) M: Y" U: o' B. I. s - string line = ffmpegProcess.StandardError.ReadLine();
, {" \# j+ T, _4 l$ l$ b - Console.WriteLine(line);
6 M* M( X1 [) Y. d) g6 S - }! c% l! I8 T! n. o
3 e# R/ M4 `& w& ?- ffmpegProcess.WaitForExit();
1 D# G: l0 t7 a M0 Q - }3 S5 U; U) k/ H) p* u- S* G9 d
- }
) S* Y8 k, O/ ~: h1 B& d: X - }
5 g( f) B: n/ o& b - }
% r* m; l/ G7 L. B! }5 K1 n8 _! d
复制代码
u& F, L+ c- u; t
9 }* F. ~) t/ g& X
% B1 ?! |$ S" E9 V, p0 @" L$ u. R3 h! f
|