Advertisement
Guest User

FFMPEG C# Scan for commercial breaks

a guest
Nov 6th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.08 KB | None | 0 0
  1.         static List<TimeSpan> scanForCommercialBreaks(string file, double threshhold, double wait)
  2.         {
  3.             Process proc = new Process();
  4.             proc.StartInfo.FileName = ffmpeg_location;
  5.  
  6.             string fname_noext = Path.GetFileNameWithoutExtension(file);
  7.             string fname_root = Path.GetDirectoryName(file);
  8.  
  9.             string filename = file;
  10.  
  11.             //get duration
  12.             proc.StartInfo.Arguments = "-i " + "\"" + filename + "\"" + " -vf blackframe -an -f null -";
  13.             AddLog("scanForCommercialBreaks:" + proc.StartInfo.Arguments);
  14.             //proc.StartInfo.Arguments = "-i " + "\"" + filename + "\"" + " -vf select='gte(scene,0)' -an -f null -";
  15.  
  16.             proc.StartInfo.RedirectStandardError = true;
  17.             proc.StartInfo.UseShellExecute = false;
  18.             if (!proc.Start())
  19.             {
  20.                 Console.WriteLine("Error starting");
  21.             }
  22.             StreamReader reader = proc.StandardError;
  23.             List<TimeSpan> nums = new List<TimeSpan>();
  24.             string line;
  25.             string last_num = "";
  26.             Console.WriteLine("Scanning for Commercial Breaks...");
  27.             DateTime now = DateTime.Now;
  28.             while ((line = reader.ReadLine()) != null)
  29.             {
  30.                 if((DateTime.Now - now) > TimeSpan.FromSeconds(.5))
  31.                 {
  32.                     Console.Write("_");
  33.                     now = DateTime.Now;
  34.                 }
  35.                 int a = line.IndexOf("Parsed_blackframe");
  36.                 //AddLog(line.ToString());
  37.                 if (a >= 0)
  38.                 {
  39.                     a = line.IndexOf(" t:");
  40.                     int b = line.IndexOf(" ", a + 1);
  41.                     string num = line.Substring(a + 3, b - a - 3);
  42.                     //Console.WriteLine("NUM:" + num);
  43.                     if (last_num != "")
  44.                     {
  45.                         //
  46.                         if (Double.Parse(num) - Double.Parse(last_num) > 2) nums.Add(TimeSpan.FromSeconds(0));
  47.                     }
  48.                     last_num = num;
  49.                     if (Double.Parse(num) > wait)
  50.                     {
  51.                         nums.Add(TimeSpan.FromSeconds(Double.Parse(num)));
  52.                         Console.Write(".");
  53.                     }
  54.                     else
  55.                     {
  56.                         Console.Write("<");
  57.                     }
  58.                    
  59.                     //Console.WriteLine("blah:" + TimeSpan.FromSeconds(Double.Parse(num)));
  60.  
  61.                 }
  62.                 else
  63.                 {
  64.                     if (nums.Count > 0)
  65.                     {
  66.                         if (nums[nums.Count - 1].TotalSeconds != 0)
  67.                         {
  68.                             nums.Add(TimeSpan.FromSeconds(0));
  69.                             Console.Write("^");
  70.                         }
  71.                     }
  72.                     //Console.WriteLine("sigal:" + line);
  73.                 }
  74.             }
  75.             Console.WriteLine("#");
  76.             Console.WriteLine("Confirming Breaks...");
  77.             for (int i = 1; i < nums.Count; i++)
  78.             {
  79.                 if (nums[i - 1].TotalSeconds == 0 && nums[i].TotalSeconds == 0)
  80.                 {
  81.                     nums.RemoveAt(i);
  82.                     Console.Write("-");
  83.                 }
  84.             }
  85.             List<TimeSpan> commerical_breaks = new List<TimeSpan>();
  86.             commerical_breaks.Add(TimeSpan.FromSeconds(0));
  87.             int last = 0;
  88.             for(int i = 0;i<nums.Count;i++)
  89.             {
  90.                 TimeSpan t = nums[i];
  91.                 //Console.WriteLine(i + " - " + t.ToString());
  92.                 if (t.TotalSeconds == 0) //start new count
  93.                 {
  94.                     if (last != 0)
  95.                     {
  96.  
  97.                         TimeSpan q = nums[last+1];
  98.                         TimeSpan qa = nums[i-1];
  99.                         //Console.WriteLine("Total: " + (qa - q).TotalSeconds + " - " + qa.ToString() + " - " + q.ToString());
  100.                         AddLog("Total: " + (qa - q).TotalSeconds + " - " + qa.ToString() + " - " + q.ToString() + " ticks:" + (qa - q).Ticks + " thresh hold:" + TimeSpan.FromSeconds(threshhold).Ticks);
  101.                         if ((qa - q).Ticks > TimeSpan.FromSeconds(threshhold).Ticks)
  102.                         {
  103.  
  104.                             //Console.WriteLine("Found commercial break at " + qa.ToString());
  105.                             AddLog("Found commercial break between (" + q.ToString() + "," + qa.ToString() + ")");
  106.                             TimeSpan tdiff = qa - q;
  107.                             AddLog("Using difference: " + (q + new TimeSpan(tdiff.Ticks / 2)).ToString());
  108.                             commerical_breaks.Add((q + new TimeSpan(tdiff.Ticks / 2)));
  109.                             Console.Write("*");
  110.                         }
  111.                         last = i;
  112.                     }
  113.                     else
  114.                     {
  115.                         last = i;
  116.                     }
  117.                 }
  118.                
  119.                 //Console.WriteLine(t.ToString());
  120.             }
  121.             Console.WriteLine("#");
  122.             Console.WriteLine("Commercial breaks found: " + commerical_breaks.Count);
  123.             AddLog("Commercial breaks found: " + commerical_breaks.Count);
  124.             //Console.ReadKey();
  125.             proc.Close();
  126.            
  127.             TimeSpan lt = new TimeSpan(0, 0, 0);
  128.             List<TimeSpan> new_com = new List<TimeSpan>();
  129.             foreach (TimeSpan t in commerical_breaks)
  130.             {
  131.                 if (t.TotalMinutes - lt.TotalMinutes > 5 || lt.TotalMinutes == 0 || threshhold == 0)
  132.                 {
  133.                     new_com.Add(t);
  134.                     AddLog("New Found commercial break at " + t.ToString());
  135.                     //Console.WriteLine("New Found commercial break at " + t.ToString());
  136.                     Console.Write("+");
  137.                 }
  138.  
  139.                 lt = t;
  140.             }
  141.  
  142.             return new_com;
  143.            
  144.             //return commerical_breaks;
  145.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement