微信下載錄音檔案(音軌分離 ffmpeg視訊合成)
/// <SUMMARY>
/// 下載儲存多媒體檔案,返回多媒體儲存路徑
/// </SUMMARY>
/// <PARAM name="ACCESS_TOKEN"></PARAM>
/// <PARAM name="MEDIA_ID"></PARAM>
/// <RETURNS></RETURNS>
public string GetMultimedia(HttpContext context)
{
//下面都是為了獲取微信token,我是存在xml中的。
string filepath = HttpContext.Current.Server.MapPath("/AK.xml");
StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
XmlDocument xml = new XmlDocument();
xml.Load(str);
str.Close();
str.Dispose();
var Token = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
string file = string.Empty;
string content = string.Empty;
string strpath = string.Empty;
string savepath = string.Empty;
string MEDIA_ID = context.Request["MEDIA_ID"];
//上面都是為了獲取token MEDIA_ID是微信的檔案ID,需要在前臺用wx.uploadVoice上傳
string stUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + Token + "&media_id=" + MEDIA_ID;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
strpath = myResponse.ResponseUri.ToString();
//WebClient mywebclient = new WebClient();
string basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "file", "VoiceCard");
HttpCookie mycookie = context.Request.Cookies[CommManage.hd_cookie_name];
string openId = mycookie.Value;
var fileName = openId + new Random().Next(1000, 9999);
savepath = Path.Combine(basePath , fileName + ".amr");
try
{
//mywebclient.DownloadFile(strpath, savepath);
DownLoad(strpath, savepath, "audio/amr");
///amr轉換成MP3格式
//mywebclient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted) ;
//Thread.Sleep(5000);
//把從微信下載的檔案轉成mp3格式,如果要在h5頁面裡聽需要轉換,如果只是簡單下載則不需要轉碼
ConvertToAmr(Path.Combine(basePath, fileName + ".amr"),Path.Combine(basePath, fileName + ".mp3"));
//轉換成功後儲存
hd_join_user my_model = main_db.hd_join_user.FirstOrDefault(p => p.open_id == openId && p.hd_code == VoiceCardManage.hd_code);
var voice = new hd_articlevote_images();
voice.article_code = my_model.id.ToString();
voice.images = fileName + ".mp3";
voice.hd_code = VoiceCardManage.hd_code;
main_db.SaveChanges();
return fileName + ".mp3";
}
catch (Exception ex)
{
savepath = ex.ToString();
Log.Error("bug" , ex.ToString().Substring(0,100));
return "";
}
}
}
上面是從微信端下載音訊檔案,其他檔案也是類似
//下載檔案
public static bool DownLoad(string url, string path, string contentType)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ServicePoint.Expect100Continue = false;
req.Method = "GET";
req.KeepAlive = true;
req.ContentType = contentType;// "image/png";
using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
{
using (Stream reader = rsp.GetResponseStream())
{
using (FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buff = new byte[512];
int c = 0; //實際讀取的位元組數
while ((c = reader.Read(buff, 0, buff.Length)) > 0)
{
writer.Write(buff, 0, c);
}
}
}
}
return true;
}
catch (Exception e)
{
return false;
}
}
amr格式轉成MP3
/// <summary>
/// 將amr音訊轉成mp3手機音訊
/// </summary>
/// <param name="applicationPath">ffmeg.exe檔案路徑</param>
/// <param name="fileName">amr檔案的路徑(帶檔名)</param>
/// <param name="targetFilName">生成目前mp3檔案路徑(帶檔名)</param>
/// string c 裡面的ffmpeg.exe需要下載這個檔案,路徑是ffmpeg.exe的檔案路勁
public void ConvertToAmr( string fileName, string targetFilName)
{
string c = @"G:\toolsZ\ffmpeg64\bin" + @"\ffmpeg.exe -i " + fileName + " " + targetFilName;
c = c.Replace("//", "\\").Replace("/", "\\");
Log.Info("ff", c);
Cmd(c);
}
開啟ffmpeg.exe檔案需要開啟cmd.exe
/// <summary>
/// 執行Cmd命令
/// </summary>
private void Cmd(string c)
{
try
{
process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.StandardInput.WriteLine(c);
process.StandardInput.AutoFlush = true;
process.StandardInput.WriteLine("exit");
StreamReader reader = process.StandardOutput;//擷取輸出流
//process.StandardInput.Flush();
//process.StandardInput.Close();
process.WaitForExit();
}
catch
{ }
}
相關文章
- 尋找(下載)微信音訊檔案音訊
- 如何下載微信公眾號中的音訊、視訊檔案?音訊
- ffmpeg實戰-音視訊合成案例
- 微信開發之錄音檔案
- ffmpeg命令錄製windows音視訊Windows
- FFmpeg音視訊同步
- mkvtoolnix 分離影片音軌 把英語的音軌分離出來 - 軟體推薦
- ffmpeg 匯出影片檔案中的音訊音訊
- 使用FFmpeg處理音視訊
- 抖音視訊批量下載,手把手教你下載抖音無水印視訊
- Android WebView 實現檔案選擇、拍照、錄製視訊、錄音AndroidWebView
- [FFmpeg + OpenGL + OpenSL ES]音視訊同步- 8
- C#程式呼叫FFmpeg操作音視訊C#
- ffmpeg實戰-音視訊基礎概念
- ffmpeg 去除音訊中的靜音音訊
- iOS開發系列--音訊播放、錄音、視訊播放、拍照、視訊錄製(轉)iOS音訊
- FFmpeg音訊解碼音訊
- javascript手機移動端微信拍照錄音錄視訊並阿里oss上傳JavaScript阿里
- Vue框架怎麼使用mediainfo.js來檢測視訊檔案是否有音軌?Vue框架AIJS
- 微信audio音訊不能播放音訊
- QQ音樂MP3檔案下載
- 根據小姐姐的抖音號批量下載抖音視訊
- 音訊開發之錄製播放pcm檔案音訊
- 微信開發之網頁中加入視訊和音訊網頁音訊
- 【秒懂音視訊開發】08_音訊錄製音訊
- axios前後端分離下載檔案iOS後端
- 風雲音訊處理大師提取視訊中的音訊檔案的方法音訊
- iOS: Audio File 音訊檔案錄製(支援VBR,CBR)iOS音訊
- RecorderManager安卓仿微信自定義音視訊錄製第三方庫安卓
- 下載網頁音原始檔網頁
- FFmpeg開發筆記(八):ffmpeg解碼音訊並使用SDL同步音訊播放筆記音訊
- 視訊提取圖片/圖片合成視訊ffmpeg(二十)
- Android 播放raw資料夾下音訊檔案Android音訊
- 音視訊--音訊入門音訊
- 音視訊–音訊入門音訊
- 東方證券:快手、抖音、微信視訊號春節亮點梳理及探討(附下載)
- wavesurfer fluent-ffmpeg提取音訊音訊
- 如何在微信小程式中實現音視訊通話微信小程式