/// <summary>
        /// Gets basic information about file using specified library instance.
        /// </summary>
        /// <param name="mediaInfoLibrary"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static BasicVideoInformation GetBasicVideoInfo(MediaInfoLibrary mediaInfoLibrary, string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("File not found.", fileName);
            }
            //
            BasicVideoInformation res;

            //
            mediaInfoLibrary.OpenFile(fileName);
            //
            res.FileName = fileName;
            //
            string fileSizeString = mediaInfoLibrary.Get(StreamKind.General, 0, "FileSize");

            res.FileSize = String.IsNullOrEmpty(fileSizeString) ? 0 : Convert.ToInt32(fileSizeString);
            //
            string durationString = mediaInfoLibrary.Get(StreamKind.General, 0, "Duration");

            res.DurationMilliseconds = String.IsNullOrEmpty(durationString) ? 0 : Convert.ToInt32(durationString);
            //
            res.VideoFormat = mediaInfoLibrary.Get(StreamKind.General, 0, "Format") ?? String.Empty;
            res.VideoCodec  = mediaInfoLibrary.Get(StreamKind.Video, 0, "Codec") ?? String.Empty;
            res.AudioCodec  = mediaInfoLibrary.Get(StreamKind.Audio, 0, "Codec") ?? String.Empty;
            //
            string widthStr = mediaInfoLibrary.Get(StreamKind.Video, 0, "Width");

            res.Width = string.IsNullOrEmpty(widthStr) ? 0 : Convert.ToInt32(widthStr);
            //
            string heightStr = mediaInfoLibrary.Get(StreamKind.Video, 0, "Height");

            res.Height = string.IsNullOrEmpty(heightStr) ? 0 : Convert.ToInt32(heightStr);
            //
            string bitrateStr = mediaInfoLibrary.Get(StreamKind.Video, 0, "BitRate");

            res.Bitrate = string.IsNullOrEmpty(bitrateStr) ? 0 : Convert.ToInt32(bitrateStr);
            //
            string fpsStr = mediaInfoLibrary.Get(StreamKind.Video, 0, "FrameRate");

            res.Fps = string.IsNullOrEmpty(fpsStr) ? 0d : Convert.ToDouble(fpsStr.Replace('.', ','));
            //
            mediaInfoLibrary.CloseFile();
            //
            return(res);
        }
Beispiel #2
0
        /// <summary>
        /// Gets basic information about file using appropriate library instance.
        /// Library will be unloaded after execution.
        /// </summary>
        /// <param name="fileName">Full path to the file.</param>
        public static BasicVideoInformation GetBasicVideoInfo(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("File not found.", fileName);
            }
            //
            if (information.ContainsKey(fileName))
            {
                KeyValuePair <DateTime, BasicVideoInformation> pair = information[fileName];
                DateTime time = File.GetLastWriteTimeUtc(fileName);
                if (pair.Key == time)
                {
                    return(pair.Value);
                }
                else
                {
                    information.Remove(fileName);
                }
            }
            //
            BasicVideoInformation res;
            //
            DateTime utc = File.GetLastWriteTimeUtc(fileName);

            using (MediaInfoLibrary mediaInfoLibrary = new MediaInfoLibrary()) {
                mediaInfoLibrary.OpenFile(fileName);
                //
                res.FileName = fileName;
                //
                string fileSizeString = mediaInfoLibrary.Get(StreamKind.General, 0, "FileSize");
                res.FileSize = String.IsNullOrEmpty(fileSizeString) ? 0 : Convert.ToInt32(fileSizeString);
                //
                string durationString = mediaInfoLibrary.Get(StreamKind.General, 0, "Duration");
                res.DurationMilliseconds = String.IsNullOrEmpty(durationString) ? 0 : Convert.ToInt32(durationString);
                //
                res.VideoFormat = mediaInfoLibrary.Get(StreamKind.General, 0, "Format") ?? String.Empty;
                res.VideoCodec  = mediaInfoLibrary.Get(StreamKind.Video, 0, "Codec") ?? String.Empty;
                res.AudioCodec  = mediaInfoLibrary.Get(StreamKind.Audio, 0, "Codec") ?? String.Empty;
                //
                string widthStr = mediaInfoLibrary.Get(StreamKind.Video, 0, "Width");
                res.Width = string.IsNullOrEmpty(widthStr) ? 0 : Convert.ToInt32(widthStr);
                //
                string heightStr = mediaInfoLibrary.Get(StreamKind.Video, 0, "Height");
                res.Height = string.IsNullOrEmpty(heightStr) ? 0 : Convert.ToInt32(heightStr);
                //
                string bitrateStr = mediaInfoLibrary.Get(StreamKind.Video, 0, "BitRate");
                res.Bitrate = string.IsNullOrEmpty(bitrateStr) ? 0 : Convert.ToInt32(bitrateStr);
                //
                string fpsStr = mediaInfoLibrary.Get(StreamKind.Video, 0, "FrameRate");
                res.Fps = string.IsNullOrEmpty(fpsStr) ? 0d : Convert.ToDouble(fpsStr.Replace('.', ','));
                //
                mediaInfoLibrary.CloseFile();
            }
            //
            information.Add(fileName, new KeyValuePair <DateTime, BasicVideoInformation>(utc, res));
            return(res);
        }