C#如何获取mp4文件的时长? 我用了各种办法,在win7下都是可以获得的,但是在xp下就是获取不到,求大虾指点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 方法一:IWMPMedia mediaInfo = wmp.newMedia(fi.FullName);mediaInfo.duration; 用这个可以获得方法二:MediaPlayer.MediaPlayer media = new MediaPlayer.MediaPlayer();media.Open(fi.FullName);media.Duration;用这个获得方法三: private string getsc(string SongPath) { string dirName = Path.GetDirectoryName(SongPath); string SongName = Path.GetFileName(SongPath);//获得歌曲名称 FileInfo fInfo = new FileInfo(SongPath); ShellClass sh = new ShellClass(); Folder dir = sh.NameSpace(dirName); FolderItem item = dir.ParseName(SongName); return Regex.Match(dir.GetDetailsOf(item, -1), "\\d:\\d{2}:\\d{2}").Value;//获取歌曲时间 }用此函数获得但是这些方法在win7下有用,在xp系统下均无效,肿么办,求大神们帮忙解决啊。 MediaPlayer.MediaPlayer media = new MediaPlayer.MediaPlayer();media.Open(fi.FullName);media.Duration;用这个获得 wmp.currentMedia.getItemInfo("Duration") 如果不行用string a=wmp.currentMedia.durationString; 试一试 这是我项目中用的一个ffmepg类using System;using System.Collections.Generic;using System.Text;using System.ComponentModel;using System.Runtime.CompilerServices;using System.IO;using System.Web;namespace AUV5.Common{ public class FormatConverter { //FFmpeg配置信息 private string ffmpegpath = "/FFmpeg/ffmpeg.exe";//FFmpeg的服务器路径 private string imgsize = "400*300"; //视频截图大小 private string videosize = "480*360"; //视频大小 #region 也可将信息添加到配置文件中 //public static string ffmpegpath = ConfigurationManager.AppSettings["ffmpeg"]; //public static string imgsize = ConfigurationManager.AppSettings["imgsize"]; //public static string videosize = ConfigurationManager.AppSettings["videoize"]; #endregion private string destVideo = ""; /// <summary> /// 视频路径 /// </summary> public string DestVideo { get { return destVideo; } set { destVideo = value; } } private string destImage = ""; /// <summary> /// 图片路径 /// </summary> public string DestImage { get { return destImage; } set { destImage = value; } } /// <summary> /// 视频长度 /// </summary> public string VideoLength { get; set; } //文件类型 public enum VideoType { [Description(".avi")] AVI, [Description(".mov")] MOV, [Description(".mpg")] MPG, [Description(".mp4")] MP4, [Description(".flv")] FLV } /// <summary> /// 返回枚举类型的描述信息 /// </summary> /// <param name="myEnum"></param> /// <returns></returns> private string GetDiscription(System.Enum myEnum) { System.Reflection.FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString()); object[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true); if (attrs != null && attrs.Length > 0) { DescriptionAttribute desc = attrs[0] as DescriptionAttribute; if (desc != null) { return desc.Description.ToLower(); } } return myEnum.ToString(); } //将GetDesCription定义为扩展方法,需.net3.5 //public static string Description(this Enum myEnum) //{ // return GetDiscription(myEnum); //} //构造函数 //创建目录 public FormatConverter() { } #region 使用FFmpeg进行格式转换 /// <summary> /// 运行格式转换 /// </summary> /// <param name="sourceFile">要转换文件绝对路径</param> /// <param name="destPath">转换结果存储的相对路径</param> /// <param name="videotype">要转换成的文件类型</param> /// <param name="createImage">是否生成截图</param> /// <returns> /// 执行成功返回空,否则返回错误信息 /// </returns> public string Convert(string sourceFile, string destPath,string uniquename, VideoType videotype, bool createImage,bool getDuration) { //取得ffmpeg.exe的物理路径 string ffmpeg = System.Web.HttpContext.Current.Server.MapPath(ffmpegpath); if (!File.Exists(ffmpeg)) { return "找不到格式转换程序!"; } if (!File.Exists(sourceFile)) { return "找不到源文件!"; } //string uniquename = FileHelper.GetUniqueFileName(); string filename = uniquename + GetDiscription(videotype); string destFile = HttpContext.Current.Server.MapPath(destPath + filename); //if (Path.GetExtension(sourceFile).ToLower() != GetDiscription(videotype).ToLower()) //{ System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; /*ffmpeg参数说明 * -i 1.avi 输入文件 * -ab/-ac <比特率> 设定声音比特率,前面-ac设为立体声时要以一半比特率来设置,比如192kbps的就设成96,转换 均默认比特率都较小,要听到较高品质声音的话建议设到160kbps(80)以上 * -ar <采样率> 设定声音采样率,PSP只认24000 * -b <比特率> 指定压缩比特率,似乎ffmpeg是自动VBR的,指定了就大概是平均比特率,比如768,1500这样的 --加了以后转换不正常 * -r 29.97 桢速率(可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97) * s 320x240 指定分辨率 * 最后的路径为目标文件 */ FilestartInfo.Arguments = " -i " + sourceFile + " -ab 80 -ar 22050 -r 29.97 -s " + videosize + " " + destFile; //FilestartInfo.Arguments = "-y -i " + sourceFile + " -s 320x240 -vcodec h264 -qscale 4 -ar 24000 -f psp -muxvb 768 " + destFile; try { //转换 System.Diagnostics.Process.Start(FilestartInfo); destVideo = destPath + filename; } catch { return "格式转换失败!"; } //} //格式不需要转换则直接复制文件到目录 //else //{ // File.Copy(sourceFile, destFile,true); // destVideo = destPath + filename; //} //提取视频长度 if (getDuration) { VideoLength = GetVideoDuration(ffmpeg, sourceFile); } //提取图片 if (createImage) { //定义进程 System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); //截大图 string imgpath = destPath + uniquename + ".jpg";// FileHelper.GetUniqueFileName(".jpg"); ConvertImage(sourceFile, imgpath, imgsize, ImgstartInfo); //截小图 imgpath = destPath + uniquename + "_thumb.jpg"; DestImage = ConvertImage(sourceFile, imgpath, "80*80", ImgstartInfo); } return ""; } private string ConvertImage(string sourceFile, string imgpath, string imgsize, System.Diagnostics.ProcessStartInfo ImgstartInfo) { ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; /*参数设置 * -y(覆盖输出文件,即如果生成的文件(flv_img)已经存在的话,不经提示就覆盖掉了) * -i 1.avi 输入文件 * -f image2 指定输出格式 * -ss 8 后跟的单位为秒,从指定时间点开始转换任务 * -vframes * -s 指定分辨率 */ //duration: 00:00:00.00 string[] time = VideoLength.Split(':'); int seconds = int.Parse(time[0]) * 60 * 60 + int.Parse(time[1]) * 60 + int.Parse(time[2]); int ss = seconds > 5 ? 5 : seconds - 1; ImgstartInfo.Arguments = " -i " + sourceFile + " -y -f image2 -ss " + ss.ToString() + " -vframes 1 -s " + imgsize + " " + HttpContext.Current.Server.MapPath(imgpath); try { System.Diagnostics.Process.Start(ImgstartInfo); return imgpath; } catch { return ""; } } private string GetVideoDuration(string ffmpegfile, string sourceFile) { using (System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process()) { String duration; // soon will hold our video's duration in the form "HH:MM:SS.UU" String result; // temp variable holding a string representation of our video's duration StreamReader errorreader; // StringWriter to hold output from ffmpeg // we want to execute the process without opening a shell ffmpeg.StartInfo.UseShellExecute = false; //ffmpeg.StartInfo.ErrorDialog = false; ffmpeg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // redirect StandardError so we can parse it // for some reason the output comes through over StandardError ffmpeg.StartInfo.RedirectStandardError = true; // set the file name of our process, including the full path // (as well as quotes, as if you were calling it from the command-line) ffmpeg.StartInfo.FileName = ffmpegfile; // set the command-line arguments of our process, including full paths of any files // (as well as quotes, as if you were passing these arguments on the command-line) ffmpeg.StartInfo.Arguments = "-i " + sourceFile; // start the process ffmpeg.Start(); // now that the process is started, we can redirect output to the StreamReader we defined errorreader = ffmpeg.StandardError; // wait until ffmpeg comes back ffmpeg.WaitForExit(); // read the output from ffmpeg, which for some reason is found in Process.StandardError result = errorreader.ReadToEnd(); // a little convoluded, this string manipulation... // working from the inside out, it: // takes a substring of result, starting from the end of the "Duration: " label contained within, // (execute "ffmpeg.exe -i somevideofile" on the command-line to verify for yourself that it is there) // and going the full length of the timestamp duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length); return duration; } } #endregion }} Process process = new Process(); process.StartInfo = new ProcessStartInfo("D:\\myFile\\ffmpeg.exe"); process.StartInfo.Arguments = "-i D:\\myFile\\1.mp4"; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.UseShellExecute = false; process.Start(); string strRst1 = process.StandardOutput.ReadToEnd();大神们在么?继续求助啊,为什么我的strRst1获得不到返回结果的数据呢? c#线程池,如何传递多个参数? Vs2008在本机编译通过 放到IIS出现错误 求一条复杂的sql语句(也可以通过其它方式实现这个功能、比如用N条sql语句进行查询得到结果最后整合) 紧急求救:关于域控制器的问题 求一个简单的购物车: C#中如何给状态栏组件上添加一个动态的时间? 可执行程序的集成问题 C#.net 学习方法和好书征集! mm在线求助!在水晶报表中,如何实现隐藏或抑制显示整个子报表! 请问,C#如何调用windows api? 带标签的字符串怎么截取 [玻璃鱼V]C#的winform如何进行逐步执行?
方法一:
IWMPMedia mediaInfo = wmp.newMedia(fi.FullName);
mediaInfo.duration; 用这个可以获得方法二:
MediaPlayer.MediaPlayer media = new MediaPlayer.MediaPlayer();
media.Open(fi.FullName);
media.Duration;用这个获得方法三: private string getsc(string SongPath)
{
string dirName = Path.GetDirectoryName(SongPath);
string SongName = Path.GetFileName(SongPath);//获得歌曲名称
FileInfo fInfo = new FileInfo(SongPath);
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(dirName);
FolderItem item = dir.ParseName(SongName);
return Regex.Match(dir.GetDetailsOf(item, -1), "\\d:\\d{2}:\\d{2}").Value;//获取歌曲时间
}用此函数获得但是这些方法在win7下有用,在xp系统下均无效,肿么办,求大神们帮忙解决啊。
media.Open(fi.FullName);
media.Duration;用这个获得
string a=wmp.currentMedia.durationString; 试一试
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.IO;
using System.Web;
namespace AUV5.Common
{
public class FormatConverter
{
//FFmpeg配置信息
private string ffmpegpath = "/FFmpeg/ffmpeg.exe";//FFmpeg的服务器路径
private string imgsize = "400*300"; //视频截图大小
private string videosize = "480*360"; //视频大小
#region 也可将信息添加到配置文件中
//public static string ffmpegpath = ConfigurationManager.AppSettings["ffmpeg"];
//public static string imgsize = ConfigurationManager.AppSettings["imgsize"];
//public static string videosize = ConfigurationManager.AppSettings["videoize"];
#endregion private string destVideo = ""; /// <summary>
/// 视频路径
/// </summary>
public string DestVideo
{
get { return destVideo; }
set { destVideo = value; }
}
private string destImage = ""; /// <summary>
/// 图片路径
/// </summary>
public string DestImage
{
get { return destImage; }
set { destImage = value; }
} /// <summary>
/// 视频长度
/// </summary>
public string VideoLength { get; set; } //文件类型
public enum VideoType
{
[Description(".avi")]
AVI,
[Description(".mov")]
MOV,
[Description(".mpg")]
MPG,
[Description(".mp4")]
MP4,
[Description(".flv")]
FLV
}
/// <summary>
/// 返回枚举类型的描述信息
/// </summary>
/// <param name="myEnum"></param>
/// <returns></returns>
private string GetDiscription(System.Enum myEnum)
{ System.Reflection.FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());
object[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
if (attrs != null && attrs.Length > 0)
{
DescriptionAttribute desc = attrs[0] as DescriptionAttribute;
if (desc != null)
{
return desc.Description.ToLower();
}
}
return myEnum.ToString();
}
//将GetDesCription定义为扩展方法,需.net3.5
//public static string Description(this Enum myEnum)
//{
// return GetDiscription(myEnum);
//} //构造函数
//创建目录
public FormatConverter()
{
} #region 使用FFmpeg进行格式转换 /// <summary>
/// 运行格式转换
/// </summary>
/// <param name="sourceFile">要转换文件绝对路径</param>
/// <param name="destPath">转换结果存储的相对路径</param>
/// <param name="videotype">要转换成的文件类型</param>
/// <param name="createImage">是否生成截图</param>
/// <returns>
/// 执行成功返回空,否则返回错误信息
/// </returns>
public string Convert(string sourceFile, string destPath,string uniquename, VideoType videotype, bool createImage,bool getDuration)
{
//取得ffmpeg.exe的物理路径
string ffmpeg = System.Web.HttpContext.Current.Server.MapPath(ffmpegpath);
if (!File.Exists(ffmpeg))
{
return "找不到格式转换程序!";
}
if (!File.Exists(sourceFile))
{
return "找不到源文件!";
}
//string uniquename = FileHelper.GetUniqueFileName();
string filename = uniquename + GetDiscription(videotype);
string destFile = HttpContext.Current.Server.MapPath(destPath + filename);
//if (Path.GetExtension(sourceFile).ToLower() != GetDiscription(videotype).ToLower())
//{
System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
/*ffmpeg参数说明
* -i 1.avi 输入文件
* -ab/-ac <比特率> 设定声音比特率,前面-ac设为立体声时要以一半比特率来设置,比如192kbps的就设成96,转换
均默认比特率都较小,要听到较高品质声音的话建议设到160kbps(80)以上
* -ar <采样率> 设定声音采样率,PSP只认24000
* -b <比特率> 指定压缩比特率,似乎ffmpeg是自动VBR的,指定了就大概是平均比特率,比如768,1500这样的 --加了以后转换不正常
* -r 29.97 桢速率(可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97)
* s 320x240 指定分辨率
* 最后的路径为目标文件
*/
FilestartInfo.Arguments = " -i " + sourceFile + " -ab 80 -ar 22050 -r 29.97 -s " + videosize + " " + destFile;
//FilestartInfo.Arguments = "-y -i " + sourceFile + " -s 320x240 -vcodec h264 -qscale 4 -ar 24000 -f psp -muxvb 768 " + destFile;
try
{
//转换
System.Diagnostics.Process.Start(FilestartInfo);
destVideo = destPath + filename;
}
catch
{
return "格式转换失败!";
}
//}
//格式不需要转换则直接复制文件到目录
//else
//{
// File.Copy(sourceFile, destFile,true);
// destVideo = destPath + filename;
//}
//提取视频长度
if (getDuration)
{
VideoLength = GetVideoDuration(ffmpeg, sourceFile);
}
//提取图片
if (createImage)
{
//定义进程
System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); //截大图
string imgpath = destPath + uniquename + ".jpg";// FileHelper.GetUniqueFileName(".jpg");
ConvertImage(sourceFile, imgpath, imgsize, ImgstartInfo); //截小图
imgpath = destPath + uniquename + "_thumb.jpg";
DestImage = ConvertImage(sourceFile, imgpath, "80*80", ImgstartInfo); }
return "";
} private string ConvertImage(string sourceFile, string imgpath, string imgsize, System.Diagnostics.ProcessStartInfo ImgstartInfo)
{
ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
/*参数设置
* -y(覆盖输出文件,即如果生成的文件(flv_img)已经存在的话,不经提示就覆盖掉了)
* -i 1.avi 输入文件
* -f image2 指定输出格式
* -ss 8 后跟的单位为秒,从指定时间点开始转换任务
* -vframes
* -s 指定分辨率
*/
//duration: 00:00:00.00
string[] time = VideoLength.Split(':');
int seconds = int.Parse(time[0]) * 60 * 60 + int.Parse(time[1]) * 60 + int.Parse(time[2]);
int ss = seconds > 5 ? 5 : seconds - 1;
ImgstartInfo.Arguments = " -i " + sourceFile + " -y -f image2 -ss " + ss.ToString() + " -vframes 1 -s " + imgsize + " " + HttpContext.Current.Server.MapPath(imgpath);
try
{
System.Diagnostics.Process.Start(ImgstartInfo);
return imgpath;
}
catch
{
return "";
}
}
private string GetVideoDuration(string ffmpegfile, string sourceFile)
{
using (System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process())
{
String duration; // soon will hold our video's duration in the form "HH:MM:SS.UU"
String result; // temp variable holding a string representation of our video's duration
StreamReader errorreader; // StringWriter to hold output from ffmpeg // we want to execute the process without opening a shell
ffmpeg.StartInfo.UseShellExecute = false;
//ffmpeg.StartInfo.ErrorDialog = false;
ffmpeg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// redirect StandardError so we can parse it
// for some reason the output comes through over StandardError
ffmpeg.StartInfo.RedirectStandardError = true; // set the file name of our process, including the full path
// (as well as quotes, as if you were calling it from the command-line)
ffmpeg.StartInfo.FileName = ffmpegfile; // set the command-line arguments of our process, including full paths of any files
// (as well as quotes, as if you were passing these arguments on the command-line)
ffmpeg.StartInfo.Arguments = "-i " + sourceFile; // start the process
ffmpeg.Start(); // now that the process is started, we can redirect output to the StreamReader we defined
errorreader = ffmpeg.StandardError; // wait until ffmpeg comes back
ffmpeg.WaitForExit(); // read the output from ffmpeg, which for some reason is found in Process.StandardError
result = errorreader.ReadToEnd(); // a little convoluded, this string manipulation...
// working from the inside out, it:
// takes a substring of result, starting from the end of the "Duration: " label contained within,
// (execute "ffmpeg.exe -i somevideofile" on the command-line to verify for yourself that it is there)
// and going the full length of the timestamp duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length);
return duration;
}
} #endregion }
}
Process process = new Process();
process.StartInfo = new ProcessStartInfo("D:\\myFile\\ffmpeg.exe");
process.StartInfo.Arguments = "-i D:\\myFile\\1.mp4";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.Start();
string strRst1 = process.StandardOutput.ReadToEnd();大神们在么?
继续求助啊,为什么我的strRst1获得不到返回结果的数据呢?