.net 使用ffmpeg.exe进行音频转码
#region 音频转换
private int AudioIntervalTime = 100, iAudio = 0;
private string strPath = "D:\\web\\XXXX\\voice\\";
/// <summary>
/// 线程委托
/// </summary>
private delegate void DelegateAudio();
/// <summary>
/// 定时器
/// </summary>
System.Threading.Timer timeAudio;
/// <summary>
/// 定义线程
/// </summary>
private void TimeThreadAudio()
{
Thread thread = new Thread(ThreadAudio);
thread.Name = "Audio";
thread.IsBackground = true;
thread.Start();
}
/// <summary>
/// 线程委托
/// </summary>
private void ThreadAudio()
{
DelegateAudio dp = new DelegateAudio(ThreadAudioFunction);
dp.BeginInvoke(null, null);
}
/// <summary>
/// 线程定时调整
/// </summary>
private void ThreadAudioFunction()
{
timeAudio = new System.Threading.Timer(new System.Threading.TimerCallback(AudioFunction), null, 0, 0);
}
/// <summary>
/// 线程工作方法体
/// </summary>
/// <param name="sender"></param>
private void AudioFunction(object sender)
{
timeAudio.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
try
{
Dictionary<string, string> list = new RedisTool().GetAllEntriesFromHash(SysConst.str_H_Rd_AudioConvert);
foreach (var item in list)
{
string fileName = strPath + item.Key;
string targetFileName = strPath + item.Key.Replace("amr","mp3"); ;
WavConvertToAmrHelp toamr = new WavConvertToAmrHelp();
toamr.ConvertToAmr(System.Windows.Forms.Application.StartupPath, fileName, targetFileName);
ShowContent(string.Format("当前时间:{0};音频文件:{1};转换成功;\r\n", DateTime.Now.ToString(), item.Key), rtbAudioContent, iAudio++);
new RedisTool().RemoveEntryFromHash(SysConst.str_H_Rd_AudioConvert, item.Key);
}
}
catch (Exception ex)
{
LogHelper.Save(ex);
ShowContent(DateTime.Now.ToString() + "\r\n" + ex.ToString() + "\r\n", rtbAudioContent, iAudio);
}
timeAudio.Change(AudioIntervalTime, AudioIntervalTime);
}
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text; namespace SZ.Company.Common
{
/// <summary>
/// 音频转换工具类
/// </summary>
public class WavConvertToAmrHelp
{
/// <summary>
/// 将Wav音频转成Amr手机音频
/// </summary>
/// <param name="applicationPath">ffmeg.exe文件路径</param>
/// <param name="fileName">WAV文件的路径(带文件名)</param>
/// <param name="targetFilName">生成目前amr文件路径(带文件名)</param>
public void ConvertToAmr(string applicationPath, string fileName, string targetFilName)
{
string c = applicationPath + @"\ffmpeg.exe -y -i " + fileName + " -ar 8000 -ab 12.2k -ac 1 " + targetFilName;
Cmd(c);
} /// <summary>
/// 执行Cmd命令
/// </summary>
private void Cmd(string c)
{
try
{
System.Diagnostics.Process 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.WaitForExit();
}
catch
{ }
} /// <summary>
/// 获取文件的byte[]
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public byte[] GetFileByte(string fileName)
{
FileStream pFileStream = null;
byte[] pReadByte = new byte[];
try
{
pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(pFileStream);
r.BaseStream.Seek(, SeekOrigin.Begin); //将文件指针设置到文件开
pReadByte = r.ReadBytes((int)r.BaseStream.Length);
return pReadByte;
}
catch
{
return pReadByte;
}
finally
{
if (pFileStream != null)
pFileStream.Close();
}
} /// <summary>
/// 将文件的byte[]生成文件
/// </summary>
/// <param name="pReadByte"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public bool writeFile(byte[] pReadByte, string fileName)
{
FileStream pFileStream = null;
try
{
pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
pFileStream.Write(pReadByte, , pReadByte.Length);
}
catch
{
return false;
}
finally
{
if (pFileStream != null)
pFileStream.Close();
}
return true; }
}
}
.net 使用ffmpeg.exe进行音频转码的更多相关文章
- C# 使用ffmpeg.exe进行音频转换完整demo-asp.net转换代码
C# 使用ffmpeg.exe进行音频转换完整demo-asp.net转换代码 上一篇说了在winform下进行调用cmd.exe执行ffmpeg.exe进行音频转换完整demo.后来我又需要移植这个 ...
- C# 使用ffmpeg.exe进行音频转换完整demo
今天在处理微信的开发接口时候,发现微信多媒体上传接口中返回的音频格式是amr.坑人的是现在大部分的web 播放器,不支持amr的格式播放.试了很多方法都不行. 没办法,只要找一个妥协的解决方案:将am ...
- C# 使用 ffmpeg 进行音频转码
先放一下 ffmpeg 的官方文档以及下载地址: 官方文档:http://ffmpeg.org/ffmpeg.html 下载地址:http://ffmpeg.org/download.html 用 f ...
- Ffmpeg音频转码 卡顿(MP2转AAC)
最好经手一个小的功能将mp2实时流转成AAC并发布成rtmp音频流,本身不是很难的一个需求, 一个晚上就能将功能开发好.功能开发完毕后,找来一音频文件利用Ffmpeg命令将音视频文件推成 实时udp格 ...
- 使用ffmpeg.exe进行转码参数说明
使用ffmpeg.exe进行转码参数说明 摘自:https://blog.csdn.net/coloriy/article/details/47337641 2015年08月07日 13:04:32 ...
- Azure Functions + Azure Batch实现MP3音频转码方案
客户需求 客户的环境是一个网络音乐播放系统,根据网络情况提供给手机用户收听各种码率的MP3歌曲,在客户没购买歌曲的情况下提供一个三十秒内的试听版本.这样一个系统非常明确地一个需求就是会定期需要将一批从 ...
- C++调用ffmpeg.exe提取视频帧
有时候,我们获得一段视频,需要将其中的每一帧都提取出来,来进行一些相关的处理,这时候我们就可以需要用到ffmpeg.exe来进行视频帧的提取. ffmpeg简介:FFmpeg是一套可以用来记录.转换数 ...
- 最简单的基于FFMPEG+SDL的音频播放器 ver2 (採用SDL2.0)
===================================================== 最简单的基于FFmpeg的音频播放器系列文章列表: <最简单的基于FFMPEG+SDL ...
- 最简单的基于FFMPEG+SDL的音频播放器 ver2 (采用SDL2.0)
===================================================== 最简单的基于FFmpeg的音频播放器系列文章列表: <最简单的基于FFMPEG+SDL ...
随机推荐
- Xib与View关联方法
1,在需要实例的地方 //加载一个uiview的作法 [LotteryInvestigationView *lotteryInvestigationView=[[[NSBundle mainBundl ...
- CAN基础知识
CAN:Controller Area Network,是ISO国际标准化的串行通信协议. CAN控制器根据两根线上的电位来判断总线电平.总线电平分为显性电平和隐性电平,二者必居其一.发送方通过使总线 ...
- Codeforces 451E Devu and Flowers(组合计数)
题目地址 在WFU(不是大学简称)第二次比赛中做到了这道题.高中阶段参加过数竞的同学手算这样的题简直不能更轻松,只是套一个容斥原理公式就可以.而其实这个过程放到编程语言中来实现也没有那么的复杂,不过为 ...
- realestate.cei.gov.cn
using AnfleCrawler.Common; using System; using System.Collections.Concurrent; using System.Collectio ...
- This tag and its children can be replaced by one <TextView/> and a compound drawable
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- GridView控件中加自动排列序号
GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField HeaderText="序号"> < ...
- cocos2dx 3.0 之 lua 创建类
利用3.0里面封装好的模板类来写,不罗嗦,直接上例子(消除星星小游戏中 星星类) require "extern" StarSprite = class("StarSpr ...
- Objective C运行时(runtime)技术的几个要点总结
前言: Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表.属性列表.变量列表,修改方法.属性,增加方法,属性等等,本文对相 ...
- Spring使用——环境部署和配置问题总结
众所周知,spring是Java中一个非常非常重要的框架,主要提供了依赖注入DI,和切面编程AOP.我多年前做过一段时间的Java,不过那时候项目中没有用Spring,所以一直也没有特别注意,最近看了 ...
- Angularjs中的promise
promise 是一种用异步方式处理值的方法,promise是对象,代表了一个函数最终可能的返回值或抛出的异常.在与远程对象打交道非常有用,可以把它们看成一个远程对象的代理. 要在Angular中创建 ...