FFMpeg下載地址:
http://ffdshow.faireal.net/mirror/ffmpeg/
FFMpeg支持轉flv的格式:
asx,asf,mpg,wmv,3gp,mp4,mov,avi
例子:
1. .wmv轉flv
- ffmpeg -i F:\01.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 寬x高 f:\test.flv
2. 從.wmv中取得第一張截圖
- ffmpeg -i F:\01.wmv -y -f image2 -t 0.001 -s 寬x高 f:\test.jpg;
FFMpeg參數說明:
1. 常用通用選項
- -i fileName : 輸入文件
- -y : 覆蓋輸出文件
- -t 持續時間(秒) : 錄制多久
- -ss 指定時間(秒) : 開始錄制點
- -f 格式 : 強迫采用格式(iamge2:jpg;gif:gif)
2. 常用視頻選項
- -b 比特率 : 默認為200(kb/s)
- -r 幀頻 : 默認為25
- -s 幀大小 : 寬x高(默認160x128)
- -aspect 設置橫縱比 : 4:3
- -vn : 不做視頻記錄
3. 常用音頻選項
- -ab 音頻碼率
- -ar 音頻采樣率
- -an 不使用音頻記錄
一個簡單的轉換類:
public class FFMpeg
{
#region 定義變量
// 存信變量
private Hashtable _variables = new Hashtable();
#endregion
#region 定義屬性
/// <summary>
/// ffmpeg.exe文件所在路徑
/// </summary>
public string Path
{
get { return (string)_variables["Path"]; }
set { _variables.Add("Path", value); }
}
/// <summary>
/// 要轉換成Flv的寬度(默認400)
/// </summary>
public int FlvWidth
{
get
{
if (_variables.ContainsKey("FlvWidth")) return (int)_variables["FlvWidth"];
else return 160;
}
set { _variables.Add("FlvWidth", value); }
}
/// <summary>
/// 要轉換成Flv的高度(默認350)
/// </summary>
public int FlvHeight
{
get
{
if (_variables.ContainsKey("FlvHeight")) return (int)_variables["FlvHeight"];
else return 128;
}
set { _variables.Add("FlvHeight", value); }
}
/// <summary>
/// 臷圖寬(默認240)
/// </summary>
public int ImageWidth
{
get
{
if (_variables.ContainsKey("ImageWidth")) return (int)_variables["ImageWidth"];
else return 160;
}
set { _variables.Add("ImageWidth", value); }
}
/// <summary>
/// 臷圖高(默認180)
/// </summary>
public int ImageHeight
{
get
{
if (_variables.ContainsKey("ImageHeight")) return (int)_variables["ImageHeight"];
else return 128;
}
set { _variables.Add("ImageHeight", value); }
}
#endregion
#region 構造函數
public FFMpeg(string path)
{
Path = path;
}
#endregion
#region 內部方法
/// <summary>
/// 轉換為Flv文件
/// </summary>
private void RunFlv(string fullName, string path)
{ //ffmpeg -i F:\01.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 寬x高 f:\test.flv
if (System.IO.File.Exists(fullName))
{
string flvName = System.IO.Path.ChangeExtension(fullName, ".flv");
if (!String.IsNullOrEmpty(path))
{
string lastChar = path.Substring(path.Length - 1);
if (lastChar == @"\" || lastChar == @"/") path = path.Substring(0, path.Length - 1);
flvName = path + @"\" + GetFileName(flvName);
}
string args = String.Format("-i {0} -ab 56 -ar 22050 -b 500 -r 15 -s {1}x{2} {3}", fullName, this.FlvWidth, this.FlvHeight, flvName);
RunCmd(args);
}
}
/// <summary>
/// 轉換為Jpg文件
/// </summary>
private void RunJpg(string fullName, string path)
{ //ffmpeg -i F:\01.wmv -y -f image2 -t 0.001 -s 寬x高 f:\test.jpg;
if (System.IO.File.Exists(fullName))
{
string jpgName = System.IO.Path.ChangeExtension(fullName, ".jpg");
if (!String.IsNullOrEmpty(path))
{
string lastChar = path.Substring(path.Length - 1);
if (lastChar == @"\" || lastChar == @"/") path = path.Substring(0, path.Length - 1);
jpgName = path + @"\" + GetFileName(jpgName);
}
string args = String.Format("-i {0} -y -f image2 -t 0.001 -s {1}x{2} {3}", fullName, this.ImageWidth, this.ImageHeight, jpgName);
RunCmd(args);
}
}
/// <summary>
/// 從文件路徑中取得文件名
/// </summary>
private string GetFileName(string fullName)
{
fullName = fullName.Replace(@"/", @"\");
return fullName.Substring(fullName.LastIndexOf(@"\"));
}
/// <summary>
/// 運行Dos命令
/// </summary>
private void RunCmd(string args)
{
string cmdFile = this.Path + @"\ffmpeg.exe";
System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo(cmdFile);
processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
processInfo.Arguments = args;
processInfo.UseShellExecute = false;
System.Diagnostics.Process.Start(processInfo);
}
#endregion
#region 對外方法
/// <summary>
/// 轉換為Flv文件
/// </summary>
public void ToFlv(string fullName)
{
RunFlv(fullName, String.Empty);
}
/// <summary>
/// 轉換為Flv文件到指定的目錄
/// </summary>
public void ToFlv(string fullName, string path)
{
RunFlv(fullName, path);
}
/// <summary>
/// 轉換為Jpg文件
/// </summary>
public void ToJpg(string fullName)
{
RunJpg(fullName, String.Empty);
}
/// <summary>
/// 轉換為Jpg文件到指定的目錄
/// </summary>
public void ToJpg(string fullName, string path)
{
RunJpg(fullName, path);
}
#endregion
}
使用方法:
string upfineName = Server.MapPath("~/Files/Media/") + WebUtility.GetDateTimeString() + this.upfile.FileName.Substring(this.upfile.FileName.LastIndexOf("."));
string flvPath = Server.MapPath("~/Files/Flv");
string jpgFile = Server.MapPath("~/Files/Jpg");
this.upfile.SaveAs(upfineName);
FFMpeg ffmpeg = new FFMpeg(SysArguments.FFMpegPath);
ffmpeg.ToFlv(upfineName,flvPath);
ffmpeg.ToJpg(upfineName,jpgFile);
SysArguments.FFMpegPath =ffmpeg.exe文件所在的文件夾路徑
參考文獻:
http://www.ffmpeg.com.cn/index.php