. H; U+ e( v3 f* l @; m为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的1 e+ t6 M a: p6 ?/ I
sampleRate为16000的格式。
8 C. G& m- c7 f n) d本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。2 q" ]$ w6 k- _
执行后,会在目标目录生成和源文件相同目录结构的wave文件,
5 c5 }$ _$ \1 L$ Y- o& p供语音识别之用。$ V# }3 p8 m6 a: H+ n
8 z! t, O% ~# N3 }- T$ w% {& U本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,
" ^/ J! N! q7 X3 G' k R- C可以从以下软件中获得。' V/ `4 q2 V+ C% {8 w9 l
https://softaro.net/ffmpeggui/
! [" _3 I" x9 i* z/ k0 F( f
% Y- m! W/ N( R4 K$ M工具代码如下,vs2022编译即可6 M5 Q4 u$ U) Z( k0 K4 `
- using System;$ I% F4 E0 J2 w( {
- using System.Diagnostics;! \0 n+ y$ ]1 v: A
- using System.IO;
7 Z+ U3 `- ?2 E3 o
0 U6 g4 H4 s$ I8 N" ?- class Program, F7 @ b8 J. S: |+ v
- {! F9 m1 Y# e: c
- static void Main(string[] args)
& Y, _" k( ^6 @: q; S - {
$ l5 T; m4 E3 p) v# S - Console.WriteLine("cvtWave");
: c" r6 U' K! o* ?/ }, d - Console.WriteLine("请输入源文件夹路径 (folder1):");
Q {1 F! M7 F( E - string sourceFolder = Console.ReadLine();; d4 o0 v' P, ^
- Console.WriteLine("请输入目标文件夹路径 (folder2):");
6 Y, [4 u+ G! }# ^& w - string destinationFolder = Console.ReadLine();! G6 O) ?2 Q% B$ K% g6 g
/ P4 X% L1 q7 R0 l# a# t- ConvertFilesToWAV(sourceFolder, destinationFolder);& P1 U. ?$ O' D W, }( x' F
- : w! }( E% v1 E+ o% y' p6 _! Q
- Console.WriteLine("转换完成.");
. L: N! y: A% k" g1 J$ P - }/ c5 w3 `' a8 s
- # z0 N6 q) S. H2 b/ Y" x: g ~
- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)7 {6 \. m9 c' B* D# |
- {0 b; v! R# n9 I' \" \. S
- foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
3 K7 Y) H% B: S) E8 g. b% v t - {
8 z7 c, I7 J. p7 }7 E! S, z - string extension = Path.GetExtension(sourceFilePath).ToLower();% A+ Q/ @) ^6 [& Y U( [
2 I( F8 T& X5 l+ ]- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")
/ M7 F% e/ [4 |7 u - {
/ d# ^) N( v; b+ ^0 H& d2 w - string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);& a! V5 z! d, ? X( W) c
- string destinationFilePath = Path.Combine(destinationFolder, relativePath);
" V8 m q1 |7 j7 O3 r: W& o0 U3 T - destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");, d* t8 Q* f4 D1 d# E
* |' m& Y R; s2 l. u- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath)); U9 P y' _4 F! h
: _6 P8 T0 U1 Y' X6 A# @7 p: ?- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";
: h: ~( L6 r0 ? - ]; [8 C/ ^' h! X* G
- Process ffmpegProcess = new Process
9 H9 R/ S! x3 d - { k$ O0 y/ T B5 {
- StartInfo = new ProcessStartInfo
. y _9 J% _3 W - {
( a: n8 P0 G! @4 h$ V! W - FileName = "ffmpeg64.exe",0 G% r8 O: }6 `. R4 f
- Arguments = ffmpegArgs,
* P* I# X! h7 s) w! {, `! O - UseShellExecute = false,, `) k0 X; F/ K6 \' ~6 ^
- RedirectStandardError = true,# G8 W7 A! r1 b' A
- CreateNoWindow = true G0 m% w) E# T. O5 [7 z
- }
5 ^1 A F. C0 R/ B: {& D - };) t. K3 M5 L4 I2 ~+ t) W
" I+ Z8 y, d0 h9 h2 I- ffmpegProcess.Start();- c( H* e; m6 z. ?8 B7 @$ G4 a
- ! ]: v& S& M! x# o
- while (!ffmpegProcess.StandardError.EndOfStream)
4 \& w4 |: H3 C* m( X, G' q6 m7 b2 K - {' H6 F. L+ M. U4 E2 P, W: e! x$ m: Z) s
- string line = ffmpegProcess.StandardError.ReadLine();8 ]4 |! Z8 t( P
- Console.WriteLine(line);7 X8 m; b" Y+ }! `# B- h
- }
" S; h' s6 e- d7 e
8 N7 l: M8 H$ h5 [' H: b$ W8 \! A- ffmpegProcess.WaitForExit();
0 C, ?7 z2 e! `2 i" R6 b - }9 t, X( u/ b( t2 e. y* \; p4 n) J
- }; H$ M) h) _! @! |: x
- }
1 N! C4 o( i" w, Y( f$ D3 w - }
: k5 E/ y) u, u: ]0 n
复制代码
' z! ]! m% t4 j2 }0 g, Y# B% D3 ~
& g( i3 ~2 ?6 t4 [7 M
* u/ m1 h: f+ d5 S* o: q2 `) [$ G: b0 ~- h0 R# g$ s5 @. T8 O
|