/ T& {6 o; x x& Y' z b
为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的, @. T3 S5 ~8 K5 u: r
sampleRate为16000的格式。& X" u# u2 B6 J/ G+ F% G
本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。( ]- e$ X. b3 y3 q$ m5 s# i
执行后,会在目标目录生成和源文件相同目录结构的wave文件,
, a5 X) ~9 H8 [) k" ]7 u4 N供语音识别之用。
' D J$ s ^1 G9 b; H! w2 \9 v; O) t8 a( s
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,3 R0 ?0 l1 y. @: K3 o5 N) ?- f
可以从以下软件中获得。5 Y7 d8 d+ [$ r
https://softaro.net/ffmpeggui/
' n" K2 E0 l% @% H2 L2 r4 |% ^ g) B! ?& K
工具代码如下,vs2022编译即可- V. |) z' ]; I! O" }8 n( |$ ~
- using System;
8 V6 l1 l$ y, f+ H0 h8 U6 v4 Y - using System.Diagnostics;
* d6 R( ]7 P/ \: j: ? - using System.IO;
6 Y4 D# g. k: @1 z& n# y
" B+ X6 R& K$ O0 K0 ~" q- class Program
8 B2 y0 c/ j3 T8 n) _ - {) R+ A6 J1 C$ _" N5 y8 p6 [+ S
- static void Main(string[] args)
6 M& i5 J# M. g9 f. c - {5 j# Y# O) d2 x6 q+ o
- Console.WriteLine("cvtWave");
% p8 ~+ w7 F0 m* {' m- } {4 r$ e8 t - Console.WriteLine("请输入源文件夹路径 (folder1):");! z- ]. x: }9 h# s0 [( M; @
- string sourceFolder = Console.ReadLine();
k, l# W& d2 J& h: L+ d2 V; G - Console.WriteLine("请输入目标文件夹路径 (folder2):");
, O4 X% t- S5 i. O - string destinationFolder = Console.ReadLine();) J) D9 F- U8 L- h8 }: e2 D
1 _- Q( N1 B6 {/ x" r- ConvertFilesToWAV(sourceFolder, destinationFolder);2 J( T4 H: K. v
* I6 w: M/ |6 m# U- Console.WriteLine("转换完成.");
* e8 U4 j; ^; e* x/ y- Y) y) F - }% s) ^. t% w( M$ E7 E& T
- 1 `1 O9 S/ u% z9 v" p3 I$ t
- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
: ?* z. d& R, H' v7 H$ P# I" | - {
. o" M& n" l% P; q: i" V8 F9 ^ - foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
" ]- ^: Y2 g+ ]9 r# q - {& B$ h5 n! X& S) r- n
- string extension = Path.GetExtension(sourceFilePath).ToLower();, b0 ] e/ R) B$ A' p
" V/ ]' c1 e$ j- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")4 V0 @- Y/ x) g# u
- {6 W0 \" g) S, \) L# g
- string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);
+ }* }, |, f" o* B* w, ]- e8 M - string destinationFilePath = Path.Combine(destinationFolder, relativePath);4 u" s2 n; G/ b! ]$ N
- destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");) m& s; t+ z0 \* t
3 \! ]' F& k/ c1 o0 i- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));2 n. W* U6 F% `* O" E
% a9 t8 Q% X0 G4 G3 m- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";
^; C- Z5 B1 y, I; o! ~
2 h; Y0 \; X6 f7 @- Process ffmpegProcess = new Process
! l; p- k. _1 ^ - {
6 [' u! L1 C1 N" h6 g - StartInfo = new ProcessStartInfo6 f- v! v8 }* h2 L8 G# ~5 o
- {5 I. D# X- G8 O0 Y
- FileName = "ffmpeg64.exe",
9 {) Q! e& A# D6 k& o- ] - Arguments = ffmpegArgs,) a8 L7 |" p9 n' i
- UseShellExecute = false,. L1 O% L2 v, H* H3 m6 L% D- S
- RedirectStandardError = true,! q9 w: K$ e$ ?: o |' Q% l( D
- CreateNoWindow = true$ m4 [2 U! U; b
- }3 s b( K* I. c& \( O1 `
- };! U8 {. j& ] `! C
- - |1 l" q2 L6 Q. [! J
- ffmpegProcess.Start();
3 t) u+ `% ~# E! S
: j2 c3 K* f$ \9 {! n- while (!ffmpegProcess.StandardError.EndOfStream)$ N0 F! ^; B- t4 V7 W0 Q/ ]; Z4 c
- {
" ^* O& F/ f* {5 m) V0 I& H - string line = ffmpegProcess.StandardError.ReadLine();1 H( ? g% {4 e0 I$ A; R
- Console.WriteLine(line);) {; Y0 Q, s7 G. y
- }
: S2 [) f0 y4 T1 r
: [4 E5 G# w7 p- ffmpegProcess.WaitForExit();
1 X+ v6 y" V1 F' B - }6 x/ `3 V3 U! ~3 m
- }2 X" |1 y1 p; b8 _
- }" ^$ v2 B4 i. h
- }) O) e" s! z0 R# b I5 o6 R
复制代码
/ ]; a2 I- h: H8 m! g
# O6 A" v5 \7 ]: h( K3 L3 b9 ~! A8 N! a7 f$ {# s7 H' O+ q
' E) N; X+ b. F, ?
|