2

I am struggling to the get the length/duration of a video using FFMPEG. Below is the code which I got from Google, but when I run the method it returns empty string. Did whatever tweaks I could but no success. Can anybody please guide me what is going wrong here?

 private static void GetVideoDuration()
    {
        string basePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
        string filePath = basePath + @"1.mp4";            
        string cmd = string.Format("-v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1  {0}", filePath);
        Process proc = new Process();
        proc.StartInfo.FileName = Path.Combine(basePath, @"ffprobe.exe");
        proc.StartInfo.Arguments = cmd;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.UseShellExecute = false;            

        if (!proc.Start())
        {
            Console.WriteLine("Error starting");
            return;
        }
        StreamReader reader = proc.StandardError;
        string line;
        while ((line = reader.ReadToEnd()) != null)
        {
            Console.WriteLine(line);
        }
        proc.Close();
    }
Jitender Kumar
  • 2,439
  • 4
  • 29
  • 43
  • You must RedirectStandardOutput and read all the output from FFMPEG. FFMPEG put 'lots' of text there. After starting FFMPEG, WaitForExit first, to allow FFMPEG to finish the job. –  Jul 20 '16 at 07:13
  • Does the `ffprobe` command work if you run it manually, unscripted via command-line interface? This should be tried before you start attempting to script it. – llogan Jul 20 '16 at 15:51
  • No I haven't tried ffprobe using command-line interface, but I tried few ffprobe commands by fitting it in above code, but everytime they returns empty string as output and the sad part is that they should return only video duration, which exactly I need. – Jitender Kumar Jul 21 '16 at 02:57
  • Hi @LordNeckbeard, I tried using command-line interface and above command it working fine. Can you please guide me then why its not working with above code? – Jitender Kumar Jul 21 '16 at 03:04

1 Answers1

6

Thanks everybody, I got the answer.

It seems that ffprobe does not return the output using

proc.StandardError;

but using

proc.StandardOutput;

Above statement was working fine with ffmpeg but not with ffprobe and below statement is working with ffprobe. Here is the working example in case if anybody need it.

   private static void GetVideoDuration()
    {
        string basePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
        string filePath = basePath + @"1.mp4";
        string cmd = string.Format("-v error -select_streams v:0 -show_entries stream=duration -sexagesimal -of default=noprint_wrappers=1:nokey=1  {0}", filePath);
        Process proc = new Process();
        proc.StartInfo.FileName = Path.Combine(basePath, @"ffprobe.exe");
        proc.StartInfo.Arguments = cmd;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.UseShellExecute = false;
        if (!proc.Start())
        {
            Console.WriteLine("Error starting");
            return;
        }
        string duration = proc.StandardOutput.ReadToEnd().Replace("\r\n", "");
        // Remove the milliseconds
        duration = duration.Substring(0, duration.LastIndexOf("."));
        proc.WaitForExit();
        proc.Close();
    }

Above method will return you duration in hh:mm:ss.fff tt format, means including milliseconds, but if you want duration in seconds then from command please remove the -sexagesimal.

Jitender Kumar
  • 2,439
  • 4
  • 29
  • 43