/// <summary> /// Function to analyse a string line of a v1 timecodes file /// </summary> /// <param name="frame">string which contains a valid line</param> /// <param name="vfl">the video frame list to write to</param> private void AnalyseV1(String frame, VideoFrameList vfl) { //Get elements //elements[0] first frame //elememts[1] last frame //elements[2] framerate String[] elements = frame.Split(new String[] { "," }, StringSplitOptions.None); //check for valid elements if (elements.Length == 3) { //Calculate FrameInfo data int start = Convert.ToInt32(elements[0]); int end = Convert.ToInt32(elements[1]); for (int i = start; i <= end; i++) { VideoFrame tmp = new VideoFrame(); tmp.FrameNumber = i; tmp.FrameRate = AcHelper.DecimalParse(elements[2]); if (i == 0) { tmp.FrameStartTime = 0.0m; } else { tmp.FrameStartTime = vfl.FrameList[i - 1].FrameDuration + vfl.FrameList[i - 1].FrameStartTime; } //Add FrameInfo to FrameList vfl.Add(tmp); } } else { throw (new AcException("Invalid format v1 timecodes!")); } }
private void addFrameRangeToList() { //Get start frame Object startFrameObj = AcControls.AcInputBox.AcInputBox.Show("Enter the start frame of the frame range:", "Enter frame...", ""); if (startFrameObj == null) { return; } //Get end frame Object endFrameObj = AcControls.AcInputBox.AcInputBox.Show("Enter the end frame of the frame range:", "Enter frame...", ""); if (endFrameObj == null) { return; } //Get start time Object startTimeObj = AcControls.AcInputBox.AcInputBox.Show("Enter the start time of the first frame:", "Enter start time...", ""); if (startFrameObj == null) { return; } //Check for duration or frame rate Boolean useDuration = false; if (ShowQuestion("Do you want to enter duration in ms?\r\n(Answering No will prompt you to enter frame rate in fps))", "Select duration type...") == DialogResult.Yes) { useDuration = true; } Object durationObj = null; Object frameRateObj = null; if (useDuration) { //Get duration durationObj = AcControls.AcInputBox.AcInputBox.Show("Enter the duration for each frame in ms:", "Enter frame duration...", ""); if (durationObj == null) { return; } } else { //Get frame rate frameRateObj = AcControls.AcInputBox.AcInputBox.Show("Enter the frame rate for each frame in fps:", "Enter frame rate...", ""); if (frameRateObj == null) { return; } } Decimal currentStartTime = AcHelper.DecimalParse((String)startTimeObj); Decimal durationAmount = Decimal.MinValue; durationAmount = useDuration ? AcHelper.DecimalParse((String)durationObj) : AcHelper.DecimalParse((String)frameRateObj); //Add frame range for (Int32 i = Convert.ToInt32(startFrameObj); i <= Convert.ToInt32(endFrameObj); i++) { // check if frame number already exists and if it does, skip it if (vfl.FrameList.Any(x => x.FrameNumber == i)) { continue; } VideoFrame vf = new VideoFrame(); vf.FrameNumber = i; vf.FrameStartTime = currentStartTime; if (useDuration) { vf.FrameDuration = durationAmount; } else { vf.FrameRate = durationAmount; } //Update start time for next frame currentStartTime += vf.FrameDuration; //Add the frame to the list vfl.Add(vf); } }
/// <summary> /// Function to parse timecodes file /// </summary> /// <param name="timecodeFile">the path for the timecode file to parse</param> /// <param name="vfl">the video frame list to write to</param> /// <param name="writeDump">flag whether to write dump file</param> public void ParseTimecodes(String timecodeFile, VideoFrameList vfl, Boolean writeDump) { String curLine = ""; String prevLine = ""; bool isV1 = false; bool isV2 = false; Decimal assumedFps = 0.0m; //Open timecodes file using (StreamReader reader = new StreamReader(timecodeFile, Encoding.UTF8)) { try { //Start timing //Stopwatch timer = new Stopwatch(); //timer.Start(); //AcLogger.Log("Starting Parsing Timecodes file : " + timecodeFile, AcLogger.AcLogType.Form); //Read timecodes file while ((curLine = reader.ReadLine()) != null) { //Check for comment lines if (curLine.StartsWith("#")) { if (curLine.ToLower().Contains("v1")) { //AcLogger.Log("Version 1 Timecodes File Detected", AcLogger.AcLogType.Form); isV1 = true; timecodesFileVersion = 1; } if (curLine.ToLower().Contains("v2")) { //AcLogger.Log("Version 2 Timecodes File Detected", AcLogger.AcLogType.Form); isV2 = true; timecodesFileVersion = 2; } } //Check for assume line else if (curLine.ToLower().Contains("assume")) { curLine = curLine.ToLower().Replace("assume", "").Replace(" ", ""); assumedFps = AcHelper.DecimalParse(curLine); assumedFrameRate = assumedFps; //AcLogger.Log("Assumed FPS : " + assumedFps, AcLogger.AcLogType.Form); } else { //Check for empty line if (String.IsNullOrWhiteSpace(curLine)) { //Do nothing continue; } if (isV1) { AnalyseV1(curLine, vfl); } if (isV2) { //First Frame if (String.IsNullOrWhiteSpace(prevLine)) { prevLine = reader.ReadLine(); vfl.Add(AnalyseV2(curLine, prevLine, 0)); } //Other Frames else { vfl.Add(AnalyseV2(prevLine, curLine, vfl.Count)); prevLine = curLine; } } } } //Calculate last frame's FrameInfo data if v2 timecodes if (isV2) { vfl.Add(new VideoFrame() { FrameNumber = vfl.Count, FrameStartTime = AcHelper.DecimalParse(prevLine), FrameDuration = vfl.FrameList[vfl.Count - 1].FrameDuration }); } //timer.Stop(); } catch (Exception ex) { throw (new AcException("Error in reading timecodes file!", ex)); } } //AcLogger.Log("Parsing Timecodes finished!", AcLogger.AcLogType.Form); //AcLogger.Log("Parsing took : " + timer.Elapsed, AcLogger.AcLogType.Form); //AcLogger.Log("Total Frames : " + vfl.Count, AcLogger.AcLogType.Form); //Check timecode dump if (writeDump) { try { //Write Timecode Dump StringBuilder dumpBuilder = new StringBuilder(); using (StreamWriter writer = new StreamWriter(timecodeFile + ".dmp", false, Encoding.UTF8)) { for (int i = 0; i < vfl.Count; i++) { dumpBuilder.AppendFormat("Frame_Number:{0} Frame_Fps:{1} Frame_Duration:{2}\r\n", vfl.FrameList[i].FrameNumber, vfl.FrameList[i].FrameRate, vfl.FrameList[i].FrameDuration); } writer.Write(dumpBuilder.ToString()); } } catch (Exception ex) { throw (new AcException("Error writing timecode dump!", ex)); } } }
/// <summary> /// Function to parse duplicates file /// </summary> /// <param name="dupFile"> /// String with the path of the duplicates file /// </param> public static void ParseDup(String dupFile, VideoFrameList vfl) { using (StreamReader sr = new StreamReader(dupFile, Encoding.UTF8)) { //Start timing //Stopwatch timer = new Stopwatch(); //timer.Start(); //AcLogger.Log("Starting Parsing Duplicates file : " + dupFile, AcLogger.AcLogType.Form); //Check if FrameList exists bool createList = (vfl.Count == 0); //Read file String line = ""; Int32 frameNum = 0; while ((line = sr.ReadLine()) != null) { //Check for first line if (line.ToLowerInvariant().StartsWith("dedup")) { //Do nothing and advance to the next line continue; } //Check for frame difference line else if (line.ToLowerInvariant().StartsWith("frm")) { //Get frame difference String frameDiff = ""; int start = line.IndexOf("="); int len = line.IndexOf("%") - start; frameDiff = line.Substring(start + 1, len).Trim(); frameDiff = frameDiff.Substring(0, frameDiff.Length - 1); //Check if new frame list if (createList) { //Create new Video frame object VideoFrame tmpFrm = new VideoFrame(); tmpFrm.FrameNumber = frameNum; tmpFrm.FrameDifferenceFromPrevious = AcHelper.DecimalParse(frameDiff); //Add it to list vfl.Add(tmpFrm); } else { //Check if frame numbers don't match with the existing list if (frameNum > vfl.Count - 1) { //Invalid dup file throw (new AcException("Invalid Dup File! Frames are inconsistent with existing data!")); } else { //Set the difference vfl.FrameList[frameNum].FrameDifferenceFromPrevious = AcHelper.DecimalParse(frameDiff); } } //Advance frame number counter frameNum++; } } //timer.Stop(); //AcLogger.Log("Parsing Timecodes finished!", AcLogger.AcLogType.Form); //AcLogger.Log("Total frames : " + frameNum, AcLogger.AcLogType.Form); //AcLogger.Log("Parsing took : " + timer.Elapsed, AcLogger.AcLogType.Form); } }