2011-02-10, 04:42
That doesn't sound too hard though I would use something simple like Microsoft.DirectX.AudioVideoPlayback to get the duration if that is all you're after. It's already included in any Windows installation and returns the duration in seconds automatically.
If you're after the codec, resolution and audio properties then I would use MediaInfo over ffmpeg. MediaInfo is what XBMC uses to extract the video information so it's safe to assume the same details would be returned. In fact, I already have a method coded in C# that does this already. I would just need to marry that with a call to SQLite.
If you're after the codec, resolution and audio properties then I would use MediaInfo over ffmpeg. MediaInfo is what XBMC uses to extract the video information so it's safe to assume the same details would be returned. In fact, I already have a method coded in C# that does this already. I would just need to marry that with a call to SQLite.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaInfoLib;
using System.IO;
namespace InfoGatherer
{
class MediaInfoBuilder
{
public static StreamInfo GetMediaInfo(string filePath)
{
try
{
MediaInfo mediaInfo = new MediaInfo();
mediaInfo.Open(filePath);
StreamInfo streamInfo = new StreamInfo();
VideoInfo videoInfo = new VideoInfo();
videoInfo.VideoAspectDisplayRatio = mediaInfo.Get(StreamKind.Video, 0, "DisplayAspectRatio");
videoInfo.VideoBitrate = mediaInfo.Get(StreamKind.Video, 0, "BitRate/String");
videoInfo.VideoCodec = mediaInfo.Get(StreamKind.Video, 0, "Codec");
videoInfo.VideoCodecID = mediaInfo.Get(StreamKind.Video, 0, "CodecID");
videoInfo.VideoContainer = Path.GetExtension(filePath);
videoInfo.VideoDuration = mediaInfo.Get(StreamKind.Video, 0, "PlayTime");
videoInfo.VideoFormat = mediaInfo.Get(StreamKind.Video, 0, "Format");
videoInfo.VideoHeight = mediaInfo.Get(StreamKind.Video, 0, "Height");
videoInfo.VideoScanType = mediaInfo.Get(StreamKind.Video, 0, "ScanType/String");
videoInfo.VideoWidth = mediaInfo.Get(StreamKind.Video, 0, "Width");
List<AudioInfo> audioStreamList = new List<AudioInfo>();
int audioStreamCount = Convert.ToInt32(mediaInfo.Get(StreamKind.Audio, 0, "Count"));
for (int i = 0; i < audioStreamCount; i++)
{
try
{
AudioInfo audioInfo = new AudioInfo();
audioInfo.Bitrate = mediaInfo.Get(StreamKind.Audio, i, "BitRate/String");
audioInfo.Channels = mediaInfo.Get(StreamKind.Audio, i, "Channel(s)");
audioInfo.Codec = mediaInfo.Get(StreamKind.Audio, i, "Codec/String");
audioInfo.Language = mediaInfo.Get(StreamKind.Audio, i, "Language");
if (audioInfo.Bitrate != string.Empty)
audioStreamList.Add(audioInfo);
}
catch
{
Console.WriteLine("Error while getting audio info!");
}
}
streamInfo.VideoInfo = videoInfo;
streamInfo.StreamAudioList = audioStreamList;
mediaInfo.Close();
return streamInfo;
}
catch (Exception ex)
{
Console.WriteLine("Error occured while getting MediaInfo: " + ex.Message);
return null;
}
}
}
public partial class StreamInfo
{
private VideoInfo videoInfo;
public VideoInfo VideoInfo { get { return videoInfo; } set { videoInfo = value; } }
private List<AudioInfo> streamAudioList;
public List<AudioInfo> StreamAudioList { get { return streamAudioList; } set { streamAudioList = value; } }
}
public partial class VideoInfo
{
private string videoWidth;
public string VideoWidth { get { return videoWidth; } set { videoWidth = value; } }
private string videoHeight;
public string VideoHeight { get { return videoHeight; } set { videoHeight = value; } }
private string videoCodec;
public string VideoCodec { get { return videoCodec; } set { videoCodec = value; } }
private string videoDuration;
public string VideoDuration { get { return videoDuration; } set { videoDuration = value; } }
private string videoFormat;
public string VideoFormat { get { return videoFormat; } set { videoFormat = value; } }
private string videoBitrate;
public string VideoBitrate { get { return videoBitrate; } set { videoBitrate = value; } }
private string videoContainer;
public string VideoContainer { get { return videoContainer; } set { videoContainer = value; } }
private string videoCodecID;
public string VideoCodecID { get { return videoCodecID; } set { videoCodecID = value; } }
private string videoScanType;
public string VideoScanType { get { return videoScanType; } set { videoScanType = value; } }
private string videoAspectDisplayRatio;
public string VideoAspectDisplayRatio { get { return videoAspectDisplayRatio; } set { videoAspectDisplayRatio = value; } }
}
public partial class AudioInfo
{
private string language;
public string Language { get { return language; } set { language = value; } }
private string codec;
public string Codec { get { return codec; } set { codec = value; } }
private string channels;
public string Channels { get { return channels; } set { channels = value; } }
private string bitrate;
public string Bitrate { get { return bitrate; } set { bitrate = value; } }
}
}