1. 程式人生 > >獲取音、視訊時長(NAudio,Shell32,FFmpeg)

獲取音、視訊時長(NAudio,Shell32,FFmpeg)

參考網址:https://blog.csdn.net/u013810234/article/details/57471780

 

 

以下為本次測試用到的音、視訊格式:

audio :”.wav;.mp3;.wma;.ra;.mid;.ogg;.ape;.au;.aac;”;

vedio :”.mp4;.mpg;.mpeg;.avi;.rm;.rmvb;.wmv;.3gp;.flv;.mkv;.swf;.asf;”;

Note:
1. 測試音、視訊均為對應格式的有效檔案(下載自地址:包含了各種可供測試音視訊格式,且不斷更新中。。);
2. 若某音/視訊時長為0,表示對應庫、元件無法解碼檔案,即不支援該格式;
3. 類之間的關係:定義了Duration父類,三個測試方案均繼承自Duration,並重寫父類GetDuration方法。

獲取時長父類

public abstract class Duration
{
/// <summary>
/// Abstract method of getting duration(ms) of audio or vedio
/// </summary>
/// <param name="filePath">audio/vedio's path</param>
/// <returns>Duration in original format, duration in milliseconds</returns>
public abstract Tuple<string, long> GetDuration(string filePath);

/// <summary>
/// Convert format of "00:10:16" and "00:00:19.82" into milliseconds
/// </summary>
/// <param name="formatTime"></param>
/// <returns>Time in milliseconds</returns>
public long GetTimeInMillisecond(string formatTime)
{
double totalMilliSecends = 0;

if (!string.IsNullOrEmpty(formatTime))
{
string[] timeParts = formatTime.Split(':');
totalMilliSecends = Convert.ToInt16(timeParts[0]) * 60 * 60 * 1000
+ Convert.ToInt16(timeParts[1]) * 60 * 1000
+ Math.Round(double.Parse(timeParts[2]) * 1000);
}

return (long)totalMilliSecends;
}
}
使用NAudio.dll

下載、引用NAudio.dll;
由於NAudio本身主要用於處理音訊,用其獲取視訊時長並不合理(僅作統一測試),所以多數格式不支援,不足為奇;
public class ByNAudio : Duration
{
/// <summary>
/// Get duration(ms) of audio or vedio by NAudio.dll
/// </summary>
/// <param name="filePath">audio/vedio's path</param>
/// <returns>Duration in original format, duration in milliseconds</returns>
/// <remarks>return value from NAudio.dll is in format of: "00:00:19.820"</remarks>
public override Tuple<string, long> GetDuration(string filePath)
{
TimeSpan ts;
try
{
using (AudioFileReader audioFileReader = new AudioFileReader(filePath))
{
ts = audioFileReader.TotalTime;
}
}
catch (Exception)
{
/* As NAudio is mainly used for processing audio, so some formats may not surport,
* just use 00:00:00 instead for these cases.
*/
ts = new TimeSpan();
//throw ex;
}

return Tuple.Create(ts.ToString(), GetTimeInMillisecond(ts.ToString()));
}
}

NAudio結果:

 

使用Shell32.dll

引用Shell32.dll,在COM裡;
Windows自帶的元件,僅支援常見的音視訊格式;
public class ByShell32 : Duration
{
/// <summary>
/// Get duration(ms) of audio or vedio by Shell32.dll
/// </summary>
/// <param name="filePath">audio/vedio's path</param>
/// <returns>Duration in original format, duration in milliseconds</returns>
/// <remarks>return value from Shell32.dll is in format of: "00:10:16"</remarks>
public override Tuple<string, long> GetDuration(string filePath)
{
try
{
string dir = Path.GetDirectoryName(filePath);

// From Add Reference --> COM
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder folder = shell.NameSpace(dir);
Shell32.FolderItem folderitem = folder.ParseName(Path.GetFileName(filePath));

string duration = null;

// Deal with different versions of OS
if (Environment.OSVersion.Version.Major >= 6)
{
duration = folder.GetDetailsOf(folderitem, 27);
}
else
{
duration = folder.GetDetailsOf(folderitem, 21);
}

duration = string.IsNullOrEmpty(duration) ? "00:00:00" : duration;
return Tuple.Create(duration, GetTimeInMillisecond(duration));
}
catch (Exception ex)
{
throw ex;
}
}
}

Shell32結果:

 

使用FFmpeg.exe

下載FFmpeg.exe;
非同步呼叫“ffmpeg -i 檔案路徑”命令,獲取返回文字,並解析出Duration部分;
FFmpeg是對音視訊進行各種處理的一套完整解決方案,包含了非常先進的音訊/視訊編解碼庫,因此可處理多種格式(本次測試的音、視訊格式均可以進行有效解碼)。
public class ByFFmpeg : Duration
{
private StringBuilder result = new StringBuilder(); // Store output text of ffmpeg

/// <summary>
/// Get duration(ms) of audio or vedio by FFmpeg.exe
/// </summary>
/// <param name="filePath">audio/vedio's path</param>
/// <returns>Duration in original format, duration in milliseconds</returns>
/// <remarks>return value from FFmpeg.exe is in format of: "00:00:19.82"</remarks>
public override Tuple<string, long> GetDuration(string filePath)
{
GetMediaInfo(filePath);
string duration = MatchDuration(result.ToString());

return Tuple.Create(duration, GetTimeInMillisecond(duration));
}

// Call exe async
private void GetMediaInfo(string filePath)
{
result.Clear(); // Clear result to avoid previous value's interference

Process p = new Process();
p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = string.Concat("-i ", filePath);
p.ErrorDataReceived += new DataReceivedEventHandler(OutputCallback);

p.Start();
p.BeginErrorReadLine();

p.WaitForExit();
p.Close();
p.Dispose();
}

// Callback funciton of output stream
private void OutputCallback(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
result.Append(e.Data);
}
}

// Match the 'Duration' section in "ffmpeg -i filepath" output text
private string MatchDuration(string text)
{
string pattern = @"Duration:\s(\d{2}:\d{2}:\d{2}.\d+)";
Match m = Regex.Match(text, pattern);

return m.Groups.Count == 2 ? m.Groups[1].ToString() : string.Empty;
}
}


ffmpeg -i filePath
……[mp3 @ 0233ca60] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from ‘2012.mp3’:
Duration: 00:22:47.07, start: 0.000000, bitrate: 127 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
At least one output file must be specified
Note:以上為ffmpeg -i 命令的輸出值,需要匹配到Duration的時長部分。

FFmpeg結果:

Source Code(含測試音、視訊檔案): Github