, y% r- A6 v( b为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的
6 R6 q2 X! @( g( `4 c1 `sampleRate为16000的格式。
8 N9 X, N- }7 Y# D6 z' z9 [" y! K本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。
' {3 F' |) ?0 S9 E0 |: [3 K! u执行后,会在目标目录生成和源文件相同目录结构的wave文件,
) D t3 B' w* c) k* A6 p供语音识别之用。8 R2 r6 R0 l5 |# n+ T0 L' R
) u( }# Y4 g# P, m8 {% M
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,5 O& Z1 g! ?# M2 d5 c
可以从以下软件中获得。! Q" h3 z. j6 H Y6 z
https://softaro.net/ffmpeggui/
6 {% {/ Z/ n" d& ^: c; z3 S
' T& S( j3 ?3 H7 K [4 E# ^工具代码如下,vs2022编译即可+ k+ q% d% U/ X% x9 Y9 K
- using System;9 N- b% y/ D" {- R5 e9 L4 N
- using System.Diagnostics;6 ]6 W9 S( F) E: J! R1 M6 s; d
- using System.IO;% r& L: c: ~+ Y5 \% E- ]# S
# n: H' p% A a1 C G9 E6 t- class Program
3 }; `6 D' L% H4 p0 e0 w3 r. _ T' U! t - {
8 \& r" s6 i* W9 d( i# i - static void Main(string[] args)+ D% Q9 @: a9 C) T
- {7 u7 l7 M4 m2 L' |
- Console.WriteLine("cvtWave");
- t! _2 L! ]. a4 \1 {1 H9 D' p! u - Console.WriteLine("请输入源文件夹路径 (folder1):");* z! n' l2 {, F, c- v+ O, ]& z' @
- string sourceFolder = Console.ReadLine();
7 O4 p: V* f0 z0 }$ O# {+ J) Z - Console.WriteLine("请输入目标文件夹路径 (folder2):");
3 O% c' s2 d. Z8 b- ?! ]) A - string destinationFolder = Console.ReadLine();
; y7 @6 ]$ X, C7 r
! y% G6 C* C6 i6 g' Q) z- ConvertFilesToWAV(sourceFolder, destinationFolder);1 O6 d1 u: o( J! H' a$ q. [5 U/ l; L1 l, p
- & J7 @) D( U, [8 q( q6 f- a
- Console.WriteLine("转换完成.");
) J9 M: k# {- J. K; J+ ~* l - }
* l- \9 o4 Z" j/ x( x: n/ H% K - 6 M( |* F1 I3 t
- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)& Q# x3 d; L+ @7 b1 Q
- {
9 m( l: w. i& _* L! r - foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
6 c( D9 Y7 F% V1 j! d; ~( V - {
! `) p* ~/ u$ U0 X2 W$ c$ [ - string extension = Path.GetExtension(sourceFilePath).ToLower();
1 |$ I0 w( ?4 p8 q - 9 M" ]1 {6 Q% [& T
- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3"), x9 e# k/ h! n: Q- Z
- {
* K3 j$ k! Y4 h8 t - string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);
8 ?2 f# Y7 \" D2 ~. ?8 e: i - string destinationFilePath = Path.Combine(destinationFolder, relativePath);
0 ?# v; R, y4 A" U% \ L- d - destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");
* w; B- Q/ X4 d' `9 o
( r9 T; e( A: b( U- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
: A' n4 O4 _( { - ' P G- a0 x5 ?' C7 }% q
- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";' `0 ^! M; Y& I2 R- i
- 6 H* L( Y; P$ a _
- Process ffmpegProcess = new Process( [% b" `0 a$ [0 r
- {: z" M- M! ^' Q* a
- StartInfo = new ProcessStartInfo
1 r! b' F4 `3 k, ] - {
7 R0 e! M4 W+ w) k - FileName = "ffmpeg64.exe",# P2 V% a6 h( k! n: c% {
- Arguments = ffmpegArgs,
/ W0 F, f5 s n( v& ^- x' L- P - UseShellExecute = false,3 @/ W& r; N2 }. y0 ^/ `1 S ]
- RedirectStandardError = true,
$ ~7 A [$ T# i) T$ _% x - CreateNoWindow = true9 m" C1 _) F* V% R
- }
, b* o6 ]+ T$ r& R' M - };/ b& c/ n; X( y
- n0 D: ~6 H7 ]% t6 i- ffmpegProcess.Start();6 R$ I* l% b4 B
. L2 }1 d$ `% R: q7 O- while (!ffmpegProcess.StandardError.EndOfStream)" {1 i/ b5 M: Q& J$ w
- {
1 `" e: L6 V3 x - string line = ffmpegProcess.StandardError.ReadLine();$ v% _, h, [* ]8 D) p. t% U
- Console.WriteLine(line);
8 K& i( t7 d/ F _8 V6 U( z - }/ n w2 a, N$ m2 L1 d
- " F% E7 l6 s) v- _; }
- ffmpegProcess.WaitForExit();3 t _4 G; e9 S" C, w
- }# c# M1 p9 m+ f3 D: I% S% [
- }
$ A) d- w& u6 Y8 u" z W9 X - }
2 a2 b8 w8 u j; Y3 O! c - }# e/ x1 |2 Q0 t `& {+ T: F
复制代码 9 T1 A4 y( H4 J u, I
! y* B4 y, W9 m' w) C
- b |8 J: _2 Z- j. x# x+ a+ F% Q" \/ |) p4 z7 x& |* _! b% s
|