[chronojump] Webcam ffmpeg should list devices on windows... and hopefully capture



commit 49979fcc159256967d0853aa18edbd625bd40ffb
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Sep 17 17:57:29 2018 +0200

    Webcam ffmpeg should list devices on windows... and hopefully capture

 src/utilMultimedia.cs | 40 ++++++++++++++++---------
 src/webcam.cs         | 13 ++++++++-
 src/webcamFfmpeg.cs   | 81 +++++++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 114 insertions(+), 20 deletions(-)
---
diff --git a/src/utilMultimedia.cs b/src/utilMultimedia.cs
index 0b858473..c3ef5d37 100644
--- a/src/utilMultimedia.cs
+++ b/src/utilMultimedia.cs
@@ -50,27 +50,39 @@ public class UtilMultimedia
                return devicesStr;
                */
 
-               List<string> list = new List<string>();
 
                //on Linux search for video0, video1, ...
                if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX)
-               {
-                       string prefix = "/dev/";
-                       var dir = new DirectoryInfo(prefix);
-                       foreach(var file in dir.EnumerateFiles("video*"))
-                               /*
-                                * return 0, 1, ...
-                               if( file.Name.Length > 5 &&                             //there's something 
more than "video", like "video0" or "video1", ...
-                                               char.IsNumber(file.Name, 5) )           //and it's a number
-                                       list.Add(Convert.ToInt32(file.Name[5]));                        //0 
or 1, or ...
-                                       */
-                               //return "/dev/video0", "/dev/video1", ...
-                               list.Add(prefix + file.Name);
-               }
+                       return GetVideoDevicesLinux();
+               else if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.WINDOWS)
+                       return GetVideoDevicesWindows();
+               else
+                       return new List<string>();
+       }
+
+       public static List<string> GetVideoDevicesLinux ()
+       {
+               List<string> list = new List<string>();
+               string prefix = "/dev/";
+               var dir = new DirectoryInfo(prefix);
+               foreach(var file in dir.EnumerateFiles("video*"))
+                       /*
+                        * return 0, 1, ...
+                        if( file.Name.Length > 5 &&                            //there's something more than 
"video", like "video0" or "video1", ...
+                        char.IsNumber(file.Name, 5) )          //and it's a number
+                        list.Add(Convert.ToInt32(file.Name[5]));                       //0 or 1, or ...
+                        */
+                       //return "/dev/video0", "/dev/video1", ...
+                       list.Add(prefix + file.Name);
 
                return list;
        }
 
+       public static List<string> GetVideoDevicesWindows ()
+       {
+               return WebcamFfmpegGetDevicesWindows.GetDevices();
+       }
+
        /*
         * IMAGES
         */
diff --git a/src/webcam.cs b/src/webcam.cs
index 0d105fbc..28feec38 100644
--- a/src/webcam.cs
+++ b/src/webcam.cs
@@ -1,5 +1,14 @@
 /*
 
+17 setembre 2018
+
+ffmpeg builds on windows only work on Windows 7 and above
+info en directshow per windows:
+https://trac.ffmpeg.org/wiki/DirectShow
+amb el del xavi padu el ffplay funciona pero el ffmpeg capture sembla que no va pq cal tocar el rtbufsize
+sí que va, tot i que va millor amv un -rtbufsize 702000k
+el problema es que el winwows media player no ho reprodueix, pero el ffplay sí (i el vlc també)
+
 *****
 superbo capture i play amb el play amb una mica de lag:
 https://bbs.archlinux.org/viewtopic.php?id=225376
@@ -148,10 +157,12 @@ public class WebcamManage
 {
        Webcam webcam;
        Webcam webcam2;
+       private UtilAll.OperatingSystems os;
        //TODO: implement an List<T> of objects containing webcam and video device
 
        public WebcamManage()
        {
+               os = UtilAll.GetOSEnum();
        }
 
        // 1 camera
@@ -183,7 +194,7 @@ public class WebcamManage
                LogB.Information("wRS at gui chronojump.cs 0, videoDevice: " + videoDevice);
 
                //w = new WebcamMplayer (videoDevice);
-               w = new WebcamFfmpeg (videoDevice);
+               w = new WebcamFfmpeg (os, videoDevice);
                Webcam.Result result = w.CapturePrepare (Webcam.CaptureTypes.VIDEO);
 
                LogB.Information("wRS at gui chronojump.cs 1, videoDevice: " + videoDevice);
diff --git a/src/webcamFfmpeg.cs b/src/webcamFfmpeg.cs
index fdc0ae14..6c09aad7 100644
--- a/src/webcamFfmpeg.cs
+++ b/src/webcamFfmpeg.cs
@@ -22,11 +22,15 @@ using System.Collections.Generic; //List
 using System.Diagnostics;
 using System;
 using System.IO;
+using System.Text.RegularExpressions; //Regex
 
 public class WebcamFfmpeg : Webcam
 {
-       public WebcamFfmpeg (string videoDevice)
+       private UtilAll.OperatingSystems os;
+
+       public WebcamFfmpeg (UtilAll.OperatingSystems os, string videoDevice)
        {
+               this.os = os;
                this.videoDevice = videoDevice;
                captureExecutable = "ffmpeg";
                Running = false;
@@ -85,14 +89,23 @@ public class WebcamFfmpeg : Webcam
 
                int i = 0;
                parameters.Insert (i ++, "-y"); //overwrite
+
                parameters.Insert (i ++, "-f");
-               parameters.Insert (i ++, "v4l2");
+               if(os == UtilAll.OperatingSystems.LINUX)
+                       parameters.Insert (i ++, "v4l2");
+               else //windows
+                       parameters.Insert (i ++, "dshow");
+
                parameters.Insert (i ++, "-framerate");
                parameters.Insert (i ++, "30");
                parameters.Insert (i ++, "-video_size");
                parameters.Insert (i ++, "640x480");
-               parameters.Insert (i ++, "-input_format");
-               parameters.Insert (i ++, "mjpeg");
+
+               if(os == UtilAll.OperatingSystems.LINUX) {
+                       parameters.Insert (i ++, "-input_format");
+                       parameters.Insert (i ++, "mjpeg");
+               }
+
                parameters.Insert (i ++, "-i");
                parameters.Insert (i ++, videoDevice);
                parameters.Insert (i ++, Util.GetVideoTempFileName());
@@ -109,8 +122,13 @@ public class WebcamFfmpeg : Webcam
 
                int i = 0;
                parameters.Insert (i ++, "-y"); //overwrite
+
                parameters.Insert (i ++, "-f");
-               parameters.Insert (i ++, "v4l2");
+               if(os == UtilAll.OperatingSystems.LINUX)
+                       parameters.Insert (i ++, "v4l2");
+               else //windows
+                       parameters.Insert (i ++, "dshow");
+
                parameters.Insert (i ++, "-i");
                parameters.Insert (i ++, videoDevice);
                parameters.Insert (i ++, "-map");
@@ -196,3 +214,56 @@ public class WebcamFfmpeg : Webcam
        }
 
 }
+
+
+public static class WebcamFfmpegGetDevicesWindows
+{
+       public static List<string> GetDevices()
+       {
+               string executable = "ffmpeg";
+               List<string> parameters = createParameters();
+
+               ExecuteProcess.Result execute_result = ExecuteProcess.run (executable, parameters);
+               if(! execute_result.success)
+               {
+                       LogB.Information("WebcamFfmpegGetDevicesWindows error: " + execute_result.stderr);
+                       return new List<string>();
+               }
+               else
+                       return parse(execute_result.stdout);
+       }
+
+       private static List<string> createParameters()
+       {
+               //ffmpeg -list_devices true -f dshow -i dummy
+               List<string> parameters = new List<string>();
+
+               int i = 0;
+               parameters.Insert (i ++, "-list_devices");
+               parameters.Insert (i ++, "true");
+               parameters.Insert (i ++, "-f");
+               parameters.Insert (i ++, "dshow");
+               parameters.Insert (i ++, "-i");
+               parameters.Insert (i ++, "dummy");
+
+               return parameters;
+       }
+
+       private static List<string> parse(string devicesOutput)
+       {
+               //https://stackoverflow.com/a/1547483
+               string[] lines = devicesOutput.Split(
+                               new[] { Environment.NewLine },
+                               StringSplitOptions.None
+                               );
+
+               List<string> parsedList = new List<string>();
+               foreach(string l in lines)
+               {
+                       foreach(Match match in Regex.Matches(l, "\"([^\"]*)\""))
+                               parsedList.Add(match.ToString());
+               }
+
+               return parsedList;
+       }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]