Commits

Justin Lloyd  committed 14cf802

Added watermark feature to VideoFile class
Added demo code to harness to show off the new watermarking code

  • Participants
  • Parent commits 948f0a1

Comments (0)

Files changed (2)

File FFMPEGHarness/Program.cs

                 AudioCodec = "libvo_aacenc",
                 Format = "flv",
                 BufferSize = 50000,
-                Size = new Size(320,240),
+                Size = new Size(320, 240),
                 MaximumRate = 400,
-                Overwrite=true,
+                Overwrite = true,
                 OutputOptions = "-ar 22050 -ab 128k -ac 1",
             };
 
 
         private static void WatermarkVideo()
         {
+            Tuple<WatermarkPosition, Point, string>[] testPositions = new Tuple<WatermarkPosition, Point, string>[]
+            {
+                new Tuple<WatermarkPosition, Point, string>(WatermarkPosition.BottomRight, new Point(15,15), "bottom_right.flv"),
+                new Tuple<WatermarkPosition, Point, string>(WatermarkPosition.BottomLeft, new Point(15,15), "bottom_left.flv"),
+                new Tuple<WatermarkPosition, Point, string>(WatermarkPosition.TopRight, new Point(15,15), "top_right.flv"),
+                new Tuple<WatermarkPosition, Point, string>(WatermarkPosition.TopLeft, new Point(15,15), "top_left.flv"),
+                
+                new Tuple<WatermarkPosition, Point, string>(WatermarkPosition.Center, new Point(0,0), "center.flv"),
+                
+                new Tuple<WatermarkPosition, Point, string>(WatermarkPosition.CenterTop, new Point(0,15), "center_top.flv"),
+                new Tuple<WatermarkPosition, Point, string>(WatermarkPosition.CenterBottom, new Point(0,15), "center_bottom.flv"),
+                new Tuple<WatermarkPosition, Point, string>(WatermarkPosition.MiddleLeft, new Point(15,0), "middle_left.flv"),
+                new Tuple<WatermarkPosition, Point, string>(WatermarkPosition.MiddleRight, new Point(15,0), "middle_right.flv"),
+            };
+
+
             string watermarkFilePath = Directory.GetFiles(".", "*.png").First();
-            FFMPEGParameters parameters = new FFMPEGParameters { InputFilePath = m_inputFilePath, OutputFilePath = m_outputFilePath, SameQ = true, Overwrite = true, VideoFilter = String.Format("\"movie=\\'{0}\\' [logo]; [in][logo] overlay=main_w-overlay_w-{1}:main_h-overlay_h-{2} [out]\"", watermarkFilePath.Replace("\\", "\\\\"), 32, 15) };
-            string output = FFMPEG.Execute(parameters);
+            VideoFile videoFile = new VideoFile(m_inputFilePath);
+            // test all permitted position combinations for a watermark
+            foreach (var testPosition in testPositions)
+            {
+                string newFilePath = videoFile.WatermarkVideo(watermarkFilePath, false, testPosition.Item1, testPosition.Item2);
+                File.Delete(Path.Combine(Path.GetDirectoryName(m_inputFilePath), testPosition.Item3));
+                File.Move(newFilePath, Path.Combine(Path.GetDirectoryName(m_inputFilePath), testPosition.Item3));
+            }
+
         }
 
         static void Main(string[] args)

File FFMPEGWrapper/VideoFile.cs

 
 namespace FFMPEGWrapper
 {
+    public enum WatermarkPosition
+    {
+        TopLeft,
+        TopRight,
+        BottomLeft,
+        BottomRight,
+        Center,
+        MiddleLeft,
+        MiddleRight,
+        CenterTop,
+        CenterBottom,
+    }
+
     public class VideoFile
     {
         public TimeSpan Duration
             Dimensions = InfoProcessor.GetVideoDimensions(output);
         }
 
+        public string WatermarkVideo(string watermarkImageFilePath, bool overwrite, WatermarkPosition position, Point offset)
+        {
+            string extension = Path.GetExtension(FilePath);
+            string tempOutputFile = Path.ChangeExtension(Path.GetTempFileName(), extension);
+
+            string overlayFormat;
+            switch (position)
+            {
+                case WatermarkPosition.TopLeft:
+                    overlayFormat = "{0}:{1}";
+                    break;
+                case WatermarkPosition.TopRight:
+                    overlayFormat = "main_w-overlay_w-{0}:{1}";
+                    break;
+                case WatermarkPosition.BottomLeft:
+                    overlayFormat = "{0}:main_h-overlay_h-{1}";
+                    break;
+                case WatermarkPosition.BottomRight:
+                    overlayFormat = "main_w-overlay_w-{0}:main_h-overlay_h-{1}";
+                    break;
+                case WatermarkPosition.Center:
+                    overlayFormat = "(main_w-overlay_w)/2-{0}:(main_h-overlay_h)/2-{1}";
+                    break;
+                case WatermarkPosition.MiddleLeft:
+                    overlayFormat = "{0}:(main_h-overlay_h)/2-{1}";
+                    break;
+                case WatermarkPosition.MiddleRight:
+                    overlayFormat = "main_w-overlay_w-{0}:(main_h-overlay_h)/2-{1}";
+                    break;
+                case WatermarkPosition.CenterTop:
+                    overlayFormat = "(main_w-overlay_w)/2-{0}:{1}";
+                    break;
+                case WatermarkPosition.CenterBottom:
+                    overlayFormat = "(main_w-overlay_w)/2-{0}:main_h-overlay_h-{1}";
+                    break;
+
+                default:
+                    throw new ArgumentException("Invalid position specified");
+
+            }
+
+            string overlayPostion = String.Format(overlayFormat, offset.X, offset.Y);
+
+            FFMPEGParameters parameters = new FFMPEGParameters
+            {
+                InputFilePath = FilePath,
+                OutputFilePath = tempOutputFile,
+                SameQ = true,
+                Overwrite = true,
+                VideoFilter = String.Format("\"movie=\\'{0}\\' [logo]; [in][logo] overlay={1} [out]\"", watermarkImageFilePath.Replace("\\", "\\\\"), overlayPostion)
+            };
+
+
+            string output = FFMPEG.Execute(parameters);
+            if (File.Exists(tempOutputFile) == false)
+            {
+                throw new ApplicationException(String.Format("Failed to watermark video {0}" + Environment.NewLine + "{1}", FilePath, output));
+            }
+
+            FileInfo watermarkedVideoFileInfo = new FileInfo(tempOutputFile);
+            if (watermarkedVideoFileInfo.Length == 0)
+            {
+                throw new ApplicationException(String.Format("Failed to watermark video {0}" + Environment.NewLine + "{1}", FilePath, output));
+            }
+
+            if (overwrite)
+            {
+                File.Delete(FilePath);
+                File.Move(tempOutputFile, FilePath);
+
+                return FilePath;
+            }
+
+            return tempOutputFile;
+        }
+
 
     }
 }