Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static List<TimeSpan> scanForCommercialBreaks(string file, double threshhold, double wait)
- {
- Process proc = new Process();
- proc.StartInfo.FileName = ffmpeg_location;
- string fname_noext = Path.GetFileNameWithoutExtension(file);
- string fname_root = Path.GetDirectoryName(file);
- string filename = file;
- //get duration
- proc.StartInfo.Arguments = "-i " + "\"" + filename + "\"" + " -vf blackframe -an -f null -";
- AddLog("scanForCommercialBreaks:" + proc.StartInfo.Arguments);
- //proc.StartInfo.Arguments = "-i " + "\"" + filename + "\"" + " -vf select='gte(scene,0)' -an -f null -";
- proc.StartInfo.RedirectStandardError = true;
- proc.StartInfo.UseShellExecute = false;
- if (!proc.Start())
- {
- Console.WriteLine("Error starting");
- }
- StreamReader reader = proc.StandardError;
- List<TimeSpan> nums = new List<TimeSpan>();
- string line;
- string last_num = "";
- Console.WriteLine("Scanning for Commercial Breaks...");
- DateTime now = DateTime.Now;
- while ((line = reader.ReadLine()) != null)
- {
- if((DateTime.Now - now) > TimeSpan.FromSeconds(.5))
- {
- Console.Write("_");
- now = DateTime.Now;
- }
- int a = line.IndexOf("Parsed_blackframe");
- //AddLog(line.ToString());
- if (a >= 0)
- {
- a = line.IndexOf(" t:");
- int b = line.IndexOf(" ", a + 1);
- string num = line.Substring(a + 3, b - a - 3);
- //Console.WriteLine("NUM:" + num);
- if (last_num != "")
- {
- //
- if (Double.Parse(num) - Double.Parse(last_num) > 2) nums.Add(TimeSpan.FromSeconds(0));
- }
- last_num = num;
- if (Double.Parse(num) > wait)
- {
- nums.Add(TimeSpan.FromSeconds(Double.Parse(num)));
- Console.Write(".");
- }
- else
- {
- Console.Write("<");
- }
- //Console.WriteLine("blah:" + TimeSpan.FromSeconds(Double.Parse(num)));
- }
- else
- {
- if (nums.Count > 0)
- {
- if (nums[nums.Count - 1].TotalSeconds != 0)
- {
- nums.Add(TimeSpan.FromSeconds(0));
- Console.Write("^");
- }
- }
- //Console.WriteLine("sigal:" + line);
- }
- }
- Console.WriteLine("#");
- Console.WriteLine("Confirming Breaks...");
- for (int i = 1; i < nums.Count; i++)
- {
- if (nums[i - 1].TotalSeconds == 0 && nums[i].TotalSeconds == 0)
- {
- nums.RemoveAt(i);
- Console.Write("-");
- }
- }
- List<TimeSpan> commerical_breaks = new List<TimeSpan>();
- commerical_breaks.Add(TimeSpan.FromSeconds(0));
- int last = 0;
- for(int i = 0;i<nums.Count;i++)
- {
- TimeSpan t = nums[i];
- //Console.WriteLine(i + " - " + t.ToString());
- if (t.TotalSeconds == 0) //start new count
- {
- if (last != 0)
- {
- TimeSpan q = nums[last+1];
- TimeSpan qa = nums[i-1];
- //Console.WriteLine("Total: " + (qa - q).TotalSeconds + " - " + qa.ToString() + " - " + q.ToString());
- AddLog("Total: " + (qa - q).TotalSeconds + " - " + qa.ToString() + " - " + q.ToString() + " ticks:" + (qa - q).Ticks + " thresh hold:" + TimeSpan.FromSeconds(threshhold).Ticks);
- if ((qa - q).Ticks > TimeSpan.FromSeconds(threshhold).Ticks)
- {
- //Console.WriteLine("Found commercial break at " + qa.ToString());
- AddLog("Found commercial break between (" + q.ToString() + "," + qa.ToString() + ")");
- TimeSpan tdiff = qa - q;
- AddLog("Using difference: " + (q + new TimeSpan(tdiff.Ticks / 2)).ToString());
- commerical_breaks.Add((q + new TimeSpan(tdiff.Ticks / 2)));
- Console.Write("*");
- }
- last = i;
- }
- else
- {
- last = i;
- }
- }
- //Console.WriteLine(t.ToString());
- }
- Console.WriteLine("#");
- Console.WriteLine("Commercial breaks found: " + commerical_breaks.Count);
- AddLog("Commercial breaks found: " + commerical_breaks.Count);
- //Console.ReadKey();
- proc.Close();
- TimeSpan lt = new TimeSpan(0, 0, 0);
- List<TimeSpan> new_com = new List<TimeSpan>();
- foreach (TimeSpan t in commerical_breaks)
- {
- if (t.TotalMinutes - lt.TotalMinutes > 5 || lt.TotalMinutes == 0 || threshhold == 0)
- {
- new_com.Add(t);
- AddLog("New Found commercial break at " + t.ToString());
- //Console.WriteLine("New Found commercial break at " + t.ToString());
- Console.Write("+");
- }
- lt = t;
- }
- return new_com;
- //return commerical_breaks;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement