6 S9 V8 h, p# f$ E为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的: A6 L2 h+ k5 J+ B7 K) w2 @
sampleRate为16000的格式。* [" \/ U) d0 m6 E M) r1 ]. X
本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。! x/ Y* H" l" {, s) L
执行后,会在目标目录生成和源文件相同目录结构的wave文件,
@( ]4 G! Y' B9 K# o1 V7 W) L供语音识别之用。
( {: |0 p- p2 X
& R! I5 Y) N# i) U. ~本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,
! n; f1 I. k W: A/ A可以从以下软件中获得。
3 u8 g; Q3 f$ \1 l5 c z9 Ihttps://softaro.net/ffmpeggui/
* _% `1 R A# e4 A5 }0 M' X8 A! z: r! x) n" B1 [7 C
工具代码如下,vs2022编译即可/ B: A9 ~9 m& Z+ C' q! n- c
- using System;2 y+ A- R, A, x2 e7 M
- using System.Diagnostics;5 W8 o% z( ]' q7 J3 _ t) R
- using System.IO;/ a8 s) b" R2 N" S
- 2 C& y) t+ K* e+ V0 m
- class Program
6 {) f# E3 h0 ~. n7 t - {" k# }4 @: f+ V% b7 n( g
- static void Main(string[] args)
. o; t1 \! M- K/ @ - {
1 {& `# Y8 p7 l; h# N - Console.WriteLine("cvtWave");/ r( Z4 N$ C5 j
- Console.WriteLine("请输入源文件夹路径 (folder1):");7 Z2 g4 v. k) O, O
- string sourceFolder = Console.ReadLine();
" h/ A5 \3 l5 e - Console.WriteLine("请输入目标文件夹路径 (folder2):");' i( r; {* ]! t7 z7 ~ e4 A- H, ]3 n
- string destinationFolder = Console.ReadLine();
+ k3 |$ k1 c- i2 P - + I* L) o2 D( _0 {
- ConvertFilesToWAV(sourceFolder, destinationFolder);% q/ p4 J5 Q/ \- b
% x/ i7 E# K- l' g- Console.WriteLine("转换完成.");5 S0 A y- ?, i$ Y+ U& {6 w
- }9 ^2 i2 H& ^/ E
- ( V% F; k7 V3 J! o A$ G" ]
- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)+ P9 z" _- E h& E0 n+ a j3 ~, k) _
- {! E7 [' x/ i$ R
- foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))- h4 r% R5 |: A1 ^$ r
- {
0 l$ E' b2 L' `! A0 o. c' D) F - string extension = Path.GetExtension(sourceFilePath).ToLower();
8 p& `& D7 _* Z$ i
q+ `" V7 I9 _% g. J- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3"); v8 {( k% o4 w4 T( R
- {
4 }! ]! f4 g) G7 i6 m0 \5 b - string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);
* H" m+ x9 ^! \- _$ b - string destinationFilePath = Path.Combine(destinationFolder, relativePath);* r; w" A" T) ?* n. K* T5 ], t5 o
- destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");
# u, b4 x& @& j5 L, f" l1 ^ - " D* G8 q8 W3 Y$ J2 Q
- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
6 Y l9 M: b1 h6 D3 e5 x
- } j$ ~2 e$ I% z- t1 ]3 u- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\""; L! j }$ K! c* z
, x" h/ v4 w* u, v, U7 h- G' G- Process ffmpegProcess = new Process
7 P& w4 ]! |! y1 l - {) E) E( d/ J* i3 J6 ^" I# q
- StartInfo = new ProcessStartInfo- q2 R4 y3 p- t8 w* d1 u) ^- ]
- {& l5 r3 m. e( T& p4 ` Q& v- g* [
- FileName = "ffmpeg64.exe",# e% _$ d. p i8 _7 L5 |0 C
- Arguments = ffmpegArgs,
# ^0 M: X7 s8 m7 l z+ P - UseShellExecute = false," I* ] f" r: {4 e6 C
- RedirectStandardError = true,8 @$ D- D, l0 P5 Q+ l
- CreateNoWindow = true
7 d% O' J/ L5 \1 Y% }, m - }
9 h/ x. \! t% E" w' b - };: ?: G( {* |" g0 a
- m. [! b, P, |: |% {& F Q
- ffmpegProcess.Start();" u- N' @0 ]# q0 o& c. p' V1 d" ]
# g, [& j/ N0 p" D( |( [: m9 k" l- while (!ffmpegProcess.StandardError.EndOfStream)
; U2 ?& e: z$ w6 X - {6 |$ M6 h& b4 n: R7 P
- string line = ffmpegProcess.StandardError.ReadLine();
$ x/ \+ v, Z/ m9 P! t1 `: T - Console.WriteLine(line);# k) u! m# J$ u9 c
- }
1 {) k2 v, ], h
- P7 \/ a( P* k% X& A2 M8 b a- F- ffmpegProcess.WaitForExit();3 V) ~! B4 V+ a9 u& Y0 z
- }
, ~7 n0 Z% `, F& ?. y - }
& v1 ^$ V/ v5 \" K' _ - }
; R( x% O+ o! t1 X& K/ n6 k3 s - }7 q0 s4 L5 l, ?3 j) k
复制代码 - z- Y( Z/ F* d: R
. V* J" p# z6 Q; U) `9 l- X# I$ E1 ~
9 `8 n! I- T6 ?; x. T' B
0 P [7 H+ z% t) `/ I! T/ V- K |