0 e6 [* g' p- h% y, X ?为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的3 Z! R$ ?. I W, P
sampleRate为16000的格式。8 ^" d7 n0 t0 t$ Z, {! m% L
本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。. Q" F% A# r/ J, k* z a8 j- p5 q
执行后,会在目标目录生成和源文件相同目录结构的wave文件,6 Y( }* K3 m- N' [. \
供语音识别之用。 ~; q; [% {" ~# T% k6 C1 U
/ d# q9 c( A7 D. v# t1 f
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,
. e/ k4 K0 Y' Y) S S5 w: p& y# h+ `可以从以下软件中获得。
. H3 v: r7 u( Ghttps://softaro.net/ffmpeggui/
: N( r7 F: P! o
* u+ v0 {- o5 |! l, F4 V工具代码如下,vs2022编译即可
( i; u: X& ], |; t M+ X6 q+ m- using System;
' \, x y4 \ x- X* e - using System.Diagnostics;7 F) ^) U3 H7 K$ X" L
- using System.IO;
- n4 o$ @6 P8 s0 f, B
3 R& A# }: N9 e: B' }, Z* }& Y, z/ x- class Program3 Y4 K! u' a3 N: ^
- {+ d c4 S7 D4 w: x% ?$ z9 o: d
- static void Main(string[] args)
- M1 _) V2 E7 }1 i0 f/ ` - {2 @* e! H0 l6 a* Y R6 M/ j4 h0 V
- Console.WriteLine("cvtWave");4 e$ ~7 g/ h. }1 u& W1 Y" k* Y; q
- Console.WriteLine("请输入源文件夹路径 (folder1):");
W: W( E$ P7 L& w& Z - string sourceFolder = Console.ReadLine();0 |/ U, x+ V* l7 y% J4 K
- Console.WriteLine("请输入目标文件夹路径 (folder2):");
1 s l0 L$ `. p7 X - string destinationFolder = Console.ReadLine();
; d, ~+ Z% q& H, g - * j# `( j% ^" r
- ConvertFilesToWAV(sourceFolder, destinationFolder);
% }6 U V9 [+ `7 ?8 C6 X
2 Y$ {8 R3 p/ `- H1 i- Console.WriteLine("转换完成.");
& b0 A' y1 |$ e7 E - }
" Q7 s- K6 d6 X, d" L
/ s3 U: t* H# j: T- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
( i2 }, K' j9 H% W0 E# s1 j+ f - {! u2 Q$ q% v* z$ z( }0 ^! c: \& N
- foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
8 _3 u9 b9 W' r8 [0 l - {( p b3 t* `. H2 t4 l _6 [
- string extension = Path.GetExtension(sourceFilePath).ToLower();9 P# S& z: L" m, k) X; w7 v
t% v. j+ S. [* R# W# e- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")
0 R( B1 M: t/ }+ ^1 Y - {1 g) E/ t y, F9 @. y8 s& ]& n: T$ U# B
- string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);
L/ h" Y+ f4 Q+ K9 ~: n - string destinationFilePath = Path.Combine(destinationFolder, relativePath);
, y' t% o7 J0 @& l% o0 P9 k) y - destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");# r! L# J2 t* i+ @, m
- # o& N9 ~1 \ i6 i, b9 h% U. q
- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
7 Q+ B3 S$ a. B8 O. S+ D' x
. k, j4 I, a! u2 d- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";+ C( A+ x( c& h0 d
- # x2 ?0 N; ~% T$ d/ Y& R
- Process ffmpegProcess = new Process4 A' Y, @% p$ {
- {
) ?0 \; D- J* \! A - StartInfo = new ProcessStartInfo
' q1 D0 ~; U: X. x - {6 G" B7 G! n7 E- o# D
- FileName = "ffmpeg64.exe",
4 o! h+ v# W- _/ R+ n7 i! J5 } - Arguments = ffmpegArgs,
0 U6 p% e( s% J9 ^: y* R) f - UseShellExecute = false,
0 n5 U. z! Q8 O7 e- e2 _+ ~/ ~ - RedirectStandardError = true,
2 C% f3 s8 z+ _5 B - CreateNoWindow = true; |. p2 W. L# C
- }
$ l6 {1 l& G& @1 H4 s, R0 M - };8 d! j( E. Z9 u7 Q2 s
4 |8 ^* p( L. j* P$ W+ {$ M: g- ffmpegProcess.Start();
" i8 @8 d$ D1 ]" v3 t, _; c
; g; U; i5 k- t8 M8 }$ F- while (!ffmpegProcess.StandardError.EndOfStream)
N, }1 N y2 a }/ |# f9 M - {4 Y/ G6 \5 m b0 x8 h+ X: ^9 h
- string line = ffmpegProcess.StandardError.ReadLine();
- \& J" i) `2 p - Console.WriteLine(line);: [- L7 f+ X" ~2 F' Q- f
- }6 F/ V! i: z C1 b; q+ X# I$ [5 S4 h3 V
3 U, ~ I( Z( W7 U7 M- ffmpegProcess.WaitForExit();7 R8 B/ \+ t% T
- }
/ h9 [! N7 [$ G$ \ - }1 B' Y- C) e0 b- w
- }! m: F' m: O6 r) B/ M- W* G
- }
$ B( }: `- R4 w. H) d- i* f7 _% T
复制代码
0 L( V2 x3 H- h1 E# S
) V/ c, R: p7 [7 \; y, g/ O# S* p- G# T* p
, f( a3 |& \) a! |
|