//转换电影
#region //运行FFMpeg的视频解码,(这里是绝对路径)
/// <summary>
/// 转换文件并保存在指定文件夹下面(这里是绝对路径)
/// </summary>
/// <param name="fileName">上传视频文件的路径(原文件)</param>
/// <param name="playFile">转换后的文件的路径(网络播放文件)</param>
/// <param name="imgFile">从视频文件中抓取的图片路径</param>
/// <returns>成功:返回图片虚拟地址; 失败:返回空字符串</returns>
public string ChangeFilePhy(string fileName, string playFile, string imgFile)
{
//取得ffmpeg.exe的路径,路径配置在Web.Config中,如:<add key="ffmpeg" value="E:\51aspx\ffmpeg.exe" />
start = DateTime.Now;
string ffmpeg = ffmpegtool;
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
{
return "";
}
//获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/Web/User1/00001.jpg
string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
//截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="240x180" />
string FlvImgSize = VideoToFlv.sizeOfImg;
System.Diagnostics.Process FilesProcess = new System.Diagnostics.Process();
FilesProcess.StartInfo.FileName = ffmpeg;
FilesProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
FilesProcess.StartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 30 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
try
{
FilesProcess.Start();
while (!FilesProcess.HasExited)
{
}
//截图
CatchImg(fileName, imgFile);
WriteRunTime();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return "";
}
#endregion
//截图程序
public string CatchImg(string fileName, string imgFile)
{
//
string ffmpeg = ffmpegtool;
//
string flv_img = imgFile + ".jpg";
//
string FlvImgSize = VideoToFlv.sizeOfImg;
//
System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//
ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img;
try
{
System.Diagnostics.Process.Start(ImgstartInfo);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
//
if (System.IO.File.Exists(flv_img))
{
return flv_img;
}
return "";
}
#region //运行FFMpeg的视频解码,(这里是绝对路径)
/// <summary>
/// 转换文件并保存在指定文件夹下面(这里是绝对路径)
/// </summary>
/// <param name="fileName">上传视频文件的路径(原文件)</param>
/// <param name="playFile">转换后的文件的路径(网络播放文件)</param>
/// <param name="imgFile">从视频文件中抓取的图片路径</param>
/// <returns>成功:返回图片虚拟地址; 失败:返回空字符串</returns>
public string ChangeFilePhy(string fileName, string playFile, string imgFile)
{
//取得ffmpeg.exe的路径,路径配置在Web.Config中,如:<add key="ffmpeg" value="E:\51aspx\ffmpeg.exe" />
start = DateTime.Now;
string ffmpeg = ffmpegtool;
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
{
return "";
}
//获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/Web/User1/00001.jpg
string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
//截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="240x180" />
string FlvImgSize = VideoToFlv.sizeOfImg;
System.Diagnostics.Process FilesProcess = new System.Diagnostics.Process();
FilesProcess.StartInfo.FileName = ffmpeg;
FilesProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
FilesProcess.StartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 30 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
try
{
FilesProcess.Start();
while (!FilesProcess.HasExited)
{
}
//截图
CatchImg(fileName, imgFile);
WriteRunTime();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return "";
}
#endregion
//截图程序
public string CatchImg(string fileName, string imgFile)
{
//
string ffmpeg = ffmpegtool;
//
string flv_img = imgFile + ".jpg";
//
string FlvImgSize = VideoToFlv.sizeOfImg;
//
System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//
ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img;
try
{
System.Diagnostics.Process.Start(ImgstartInfo);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
//
if (System.IO.File.Exists(flv_img))
{
return flv_img;
}
return "";
}
本文介绍使用FFmpeg进行视频格式转换及从视频中截取图片的方法。通过C#调用FFmpeg,实现视频文件(.flv)的转换,并设置视频参数如分辨率等。此外,还介绍了如何自动从视频中捕捉一张预览图片。
3853

被折叠的 条评论
为什么被折叠?



