public VideoConversion(VideoConversionSettings settings) { this.Settings = settings; this.Settings.ID = Guid.NewGuid(); //check if the resolution is provided otherwise we will use the original WxH if (settings.Width == 0 && settings.Height == 0) { if (File.Exists(settings.SourcePath)) { var output = Helper.GetVideoInfo(settings.SourcePath); settings.Height = output.Height; settings.Width = output.Width; } else { //The file do not exists //To do: log this error in to the database. } } }
public static VideoConversionSettings GetVideoInfo(string sourcePath) { if (string.IsNullOrEmpty(sourcePath)) { return(null); } var video = new VideoConversionSettings(); //set up the parameters for video info string output = ProcessVideo(string.Format("-i {0}", sourcePath)); //get duration Regex re = new Regex("[D|d]uration:.((\\d|:|\\.)*)"); Match m = re.Match(output); if (m.Success) { string duration = m.Groups[1].Value; string[] timepieces = duration.Split(new char[] { ':', '.' }); if (timepieces.Length == 4) { video.Duration = new TimeSpan(0, System.Convert.ToInt16(timepieces[0]), System.Convert.ToInt16(timepieces[1]), System.Convert.ToInt16(timepieces[2]), System.Convert.ToInt16(timepieces[3])); } } //get audio bit rate re = new Regex("[B|b]itrate:.((\\d|:)*)"); m = re.Match(output); decimal kb = 0; if (m.Success) { decimal.TryParse(m.Groups[1].Value, out kb); } video.AudioBitrate = kb; //get the audio format re = new Regex("[A|a]udio:.*"); m = re.Match(output); if (m.Success) { video.AudioFormat = m.Value; } //get the video format re = new Regex("[V|v]ideo:.*"); m = re.Match(output); if (m.Success) { video.VideoFormat = m.Value; } //get the video format re = new Regex("(\\d{2,3})x(\\d{2,3})"); m = re.Match(output); if (m.Success) { int width = 0; int height = 0; int.TryParse(m.Groups[1].Value, out width); int.TryParse(m.Groups[2].Value, out height); video.Height = height; video.Width = width; } return(video); }