前言
上篇文章已經跟大家分享了asp.net利用七牛轉換法將.amr轉.mp3的方法,當時也說了還有另外一種方法是利用ffmpeg轉換法,下面這篇文章就給大家詳細介紹這種方法。這種方法相對第一種來說,要簡單的多!
FFmpeg的名稱來自MPEG視頻編碼標準,前面的“FF”代表“Fast Forward”,FFmpeg是一套可以用來記錄、轉換數字音頻、視頻,并能將其轉化為流的開源計算機程序。可以輕易地實現多種視頻格式之間的相互轉換。
ffmpeg轉換法
首先,你得下載個“ffmpeg.exe” 插件,然后把它放到你的項目中,如下圖:
程序中會調用該文件,以助于轉換音頻格式!
上代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
using System; using System.Threading; using System.IO; using System.Diagnostics; using System.Security; public partial class cowala_201512Chritmas_amrtest : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { changedPlay.Visible = false ; } } protected void Ffmpeg_Click( object sender, EventArgs e) { if (AmrFileUp.HasFile) { string key = AmrFileUp.FileName; string savepath = Server.MapPath( "~/upload/amr/" ) + key; AmrFileUp.SaveAs(savepath); string mp3SavePth = Server.MapPath( "~/upload/mp3/" ) + key.Split( '.' )[0].ToString() + ".mp3" ; if (! string .IsNullOrEmpty(ConvertToMp3(savepath, mp3SavePth))) { changedPlay.Visible = true ; changedPlay.Attributes.Add( "src" , "upload/mp3/" + key.Split( '.' )[0].ToString() + ".mp3" ); Response.Write( "<script>alert('轉換成功!');</script>" ); } } } public string ConvertToMp3( string pathBefore, string pathLater) { string c = Server.MapPath( "/ffmpeg/" ) + @"ffmpeg.exe -i " + pathBefore + " " + pathLater; string str = RunCmd(c); return str; } /// <summary> /// 執行Cmd命令 /// </summary> private string RunCmd( string c) { try { ProcessStartInfo info = new ProcessStartInfo( "cmd.exe" ); info.RedirectStandardOutput = false ; info.UseShellExecute = false ; Process p = Process.Start(info); p.StartInfo.UseShellExecute = false ; p.StartInfo.RedirectStandardInput = true ; p.StartInfo.RedirectStandardOutput = true ; p.StartInfo.RedirectStandardError = true ; p.Start(); p.StandardInput.WriteLine(c); p.StandardInput.AutoFlush = true ; Thread.Sleep(1000); p.StandardInput.WriteLine( "exit" ); p.WaitForExit(); string outStr = p.StandardOutput.ReadToEnd(); p.Close(); return outStr; } catch (Exception ex) { return "error" + ex.Message; } } } |
接著來張效果圖:
總結
好了,就這么簡單,不要不敢不相信你的眼睛,其實就是這么簡單!以上就是asp.net音頻轉換之.amr轉.mp3(利用ffmpeg轉換法)的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
原文鏈接:http://www.cnblogs.com/LittleBai/p/5924416.html