5 x: g+ c4 U1 j6 L% e
为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的
' r4 u" |, z) ?. _- \; dsampleRate为16000的格式。& l) C4 B. `! v) ^9 C8 V
本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。
* G- D1 Z+ Z- ]3 n6 T执行后,会在目标目录生成和源文件相同目录结构的wave文件,
, }3 P [3 e6 u+ t' \$ V供语音识别之用。
) O4 C/ H0 g2 F; R# K% o4 c
. M+ V( z1 T& P本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll, K5 @) Y8 p0 h& e
可以从以下软件中获得。6 ^ ^& R+ V, Y! W& `! t
https://softaro.net/ffmpeggui/
) u/ }& Q! M) \
- ]& Z" D* z9 U工具代码如下,vs2022编译即可: `1 n$ s% ~* @9 r3 Z
- using System;
H% `5 Y$ i0 ] \ - using System.Diagnostics;& e( B9 U( x7 y b% O, w# F
- using System.IO;
' z4 X8 p3 r2 _& {* |
8 v7 ^2 z# `( l* w$ c- class Program
4 C3 W4 |, `- u3 x - {2 L) x; y5 n$ S, a9 |! ^
- static void Main(string[] args)$ }2 g" H+ t$ T; g
- {- p' I4 k ^- p! x' U0 l
- Console.WriteLine("cvtWave");
( p: t S8 z2 P/ v - Console.WriteLine("请输入源文件夹路径 (folder1):");
4 E' r* C3 Z& ?) J* }7 T - string sourceFolder = Console.ReadLine();# w( w3 @% P9 C, n; y* {) A; h
- Console.WriteLine("请输入目标文件夹路径 (folder2):");# R' |3 R" V, v! Z5 K
- string destinationFolder = Console.ReadLine();" H. n$ L: z$ P5 x/ c8 f
# n9 [( K7 f; t' |# `) C5 l- ConvertFilesToWAV(sourceFolder, destinationFolder);
2 e: E; j8 z' h# ^+ D$ c1 J3 U8 b - : W* q/ ^8 `- a! O+ W& _5 g
- Console.WriteLine("转换完成.");. ~! t9 ?4 B: ~. e1 K5 M6 F
- }
9 h/ u7 S6 C- S - ! Y0 m2 R: ?: Z. }( D: D! W3 R
- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)5 ^. P3 B* t! U9 z* u2 m% s
- {
/ J$ Q$ u" l( Q7 _ u" } - foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
. I# n( Z4 E* N( r/ ~& V' s" @# P+ @ - {* b: z# r/ f8 o% O# i
- string extension = Path.GetExtension(sourceFilePath).ToLower();5 t6 J7 n5 T: k5 O" M
" }1 J+ {& W* Y8 T- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")
7 }% T3 ?3 E5 h - {$ w$ n) I, a F+ o, G m* J
- string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);; y! z% C* z4 q+ ^' k* e" ?
- string destinationFilePath = Path.Combine(destinationFolder, relativePath);7 [7 Z3 F/ K8 V2 u
- destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");
3 h; I# r6 M* z
. M8 [$ c: `+ u' c- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));( w+ ? E- P+ i$ R, M8 J
- 1 y' h7 t+ b x" C% d. G
- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";8 J) F* t9 x2 W
! w6 T- s! S, p- Process ffmpegProcess = new Process
2 _: y0 `9 [3 E9 k/ ^4 u - {
+ F* ]9 p+ J H. ] - StartInfo = new ProcessStartInfo
0 x) h4 r+ ~: q - {
. |# G' J- x8 j5 H2 Z8 F+ d6 n - FileName = "ffmpeg64.exe",
' C g! z$ k3 z+ A! } s5 w - Arguments = ffmpegArgs,, I5 e4 b: J1 z9 ^$ t* t
- UseShellExecute = false,
; _4 U1 x4 c- r. B - RedirectStandardError = true,9 |4 j8 i x1 t; W
- CreateNoWindow = true; h H1 {8 h" ?3 ?
- }
5 d/ S' M9 v; {1 p - };
9 V+ D- ~- Y4 n3 W
4 C$ ~7 ?" G* |) b$ B# R2 Z- ffmpegProcess.Start();+ z5 v9 F; W; u' c5 _7 q& M
9 s2 {( {: k) D- while (!ffmpegProcess.StandardError.EndOfStream)# E( L, t. X0 ~6 p
- {) Z1 R( S$ s4 |* Y" G
- string line = ffmpegProcess.StandardError.ReadLine();
: s+ j( v: I; b+ }' k0 n6 f" ? - Console.WriteLine(line);
' w* B0 N1 ]3 s4 C$ E# s; K - }
0 A9 E6 V: F9 |2 I: I% x3 M2 c - $ h; x* ]4 L8 q; O- b4 U0 C
- ffmpegProcess.WaitForExit();6 C, Z8 `2 ^5 j" {& o
- }. O: ~8 ?2 o4 i5 C1 y
- }7 G3 C4 y# t, R( o6 S
- }5 N: k3 q) v8 p9 i- i
- }3 T' X9 u" x, @
复制代码
5 y- |* ^! r6 O& Z8 X- E( i0 k+ j( E, V* g/ m. f; z
, E O- ^/ f- Q
" b( C7 m6 ~3 _7 m3 b8 p |