Как получить длительность видео с помощью FFMPEG в C # asp.net - PullRequest
6 голосов
/ 23 июня 2011

Я хочу получить длительность видеофайла в строке, используя C #.Я искал в интернете, и все, что я получаю, это:

ffmpeg -i inputfile.avi

И каждые1 говорят, что анализируют вывод на длительность.

Вот мой код, который

string filargs = "-y -i " + inputavi + " -ar 22050 " + outputflv;
    Process proc;
    proc = new Process();
    proc.StartInfo.FileName = spath;
    proc.StartInfo.Arguments = filargs;
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.CreateNoWindow = false;
    proc.StartInfo.RedirectStandardOutput = false;
    try
    {
        proc.Start();

    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }

    try
    {
        proc.WaitForExit(50 * 1000);
    }
    catch (Exception ex)
    { }
    finally
    {
        proc.Close();
    }

Теперь, пожалуйста, скажите мне, как я могу сохранить выходную строку и проанализировать ее для продолжительности видео.

Спасибо и привет,

Ответы [ 3 ]

4 голосов
/ 24 июня 2011

FFmpeg - это немного приключение для разбора.Но в любом случае вот что вам нужно знать.

Во-первых, FFmpeg не очень хорошо работает с опциями RedirectOutput

Вместо этого вам нужно будет сделать следующее:непосредственного запуска ffmpeg, запуска cmd.exe, передачи ffmpeg в качестве аргумента и перенаправления вывода в «файл монитора» через вывод командной строки, например так ... обратите внимание, что в цикле while (!proc.HasExited) вы можете прочитать этот файлдля статуса FFmpeg в реальном времени или просто прочитайте его в конце, если это быстрая операция.

        FileInfo monitorFile = new FileInfo(Path.Combine(ffMpegExe.Directory.FullName, "FFMpegMonitor_" + Guid.NewGuid().ToString() + ".txt"));

        string ffmpegpath = Environment.SystemDirectory + "\\cmd.exe"; 
        string ffmpegargs = "/C " + ffMpegExe.FullName + " " + encodeArgs + " 2>" + monitorFile.FullName;

        string fullTestCmd = ffmpegpath + " " + ffmpegargs;

        ProcessStartInfo psi = new ProcessStartInfo(ffmpegpath, ffmpegargs);
        psi.WorkingDirectory = ffMpegExe.Directory.FullName;
        psi.CreateNoWindow = true;
        psi.UseShellExecute = false;
        psi.Verb = "runas";

        var proc = Process.Start(psi);

        while (!proc.HasExited)
        {
            System.Threading.Thread.Sleep(1000);
        }

        string encodeLog = System.IO.File.ReadAllText(monitorFile.FullName);

Отлично, теперь у вас есть журнал того, что FFmpeg просто выплюнул.Теперь, чтобы получить продолжительность. Строка продолжительности будет выглядеть примерно так:

Duration: 00:10:53.79, start: 0.000000, bitrate: 9963 kb/s

Очистите результаты в List<string>:

var encodingLines = encodeLog.Split(System.Environment.NewLine[0]).Where(line => string.IsNullOrWhiteSpace(line) == false && string.IsNullOrEmpty(line.Trim()) == false).Select(s => s.Trim()).ToList();

... затем просматривайте их в поисках Duration.

        foreach (var line in encodingLines)
        {
            // Duration: 00:10:53.79, start: 0.000000, bitrate: 9963 kb/s
            if (line.StartsWith("Duration"))
            {
                var duration = ParseDurationLine(line);
            }
        }

Вот код, который может выполнить анализ для вас:

    private TimeSpan ParseDurationLine(string line)
    {
        var itemsOfData = line.Split(" "[0], "="[0]).Where(s => string.IsNullOrEmpty(s) == false).Select(s => s.Trim().Replace("=", string.Empty).Replace(",", string.Empty)).ToList();

        string duration = GetValueFromItemData(itemsOfData, "Duration:");

        return TimeSpan.Parse(duration);
    }

    private string GetValueFromItemData(List<string> items, string targetKey)
    {
        var key = items.FirstOrDefault(i => i.ToUpper() == targetKey.ToUpper());

        if (key == null) { return null; }
        var idx = items.IndexOf(key);

        var valueIdx = idx + 1;

        if (valueIdx >= items.Count)
        {
            return null;
        }

        return items[valueIdx];
    }
4 голосов
/ 23 июня 2011

Существует еще одна опция для получения длины видео, используя Media Info DLL

Использование Ffmpeg:

proc.StartInfo.RedirectErrorOutput = true;
string message = proc.ErrorOutput.ReadToEnd();

Фильтрация не должна быть проблемой, так что делайте это сами.

PS: используя ffmpeg, вы не должны читать StandardOutput, но ErrorOutput, я не знаю почему, но он работает только так.

1 голос
/ 23 августа 2013

Просто проверьте это:

    //Create varriables

    string ffMPEG = System.IO.Path.Combine(Application.StartupPath, "ffMPEG.exe");
    system.Diagnostics.Process mProcess = null;

    System.IO.StreamReader SROutput = null;
    string outPut = "";

    string filepath = "D:\\source.mp4";
    string param = string.Format("-i \"{0}\"", filepath);

    System.Diagnostics.ProcessStartInfo oInfo = null;

    System.Text.RegularExpressions.Regex re = null;
    System.Text.RegularExpressions.Match m = null;
    TimeSpan Duration =  null;

    //Get ready with ProcessStartInfo
    oInfo = new System.Diagnostics.ProcessStartInfo(ffMPEG, param);
    oInfo.CreateNoWindow = true;

    //ffMPEG uses StandardError for its output.
    oInfo.RedirectStandardError = true;
    oInfo.WindowStyle = ProcessWindowStyle.Hidden;
    oInfo.UseShellExecute = false;

    // Lets start the process

    mProcess = System.Diagnostics.Process.Start(oInfo);

    // Divert output
    SROutput = mProcess.StandardError;

    // Read all
    outPut = SROutput.ReadToEnd();

    // Please donot forget to call WaitForExit() after calling SROutput.ReadToEnd

    mProcess.WaitForExit();
    mProcess.Close();
    mProcess.Dispose();
    SROutput.Close();
    SROutput.Dispose();

    //get duration

    re = new System.Text.RegularExpressions.Regex("[D|d]uration:.((\\d|:|\\.)*)");
    m = re.Match(outPut);

    if (m.Success) {
        //Means the output has cantained the string "Duration"
        string temp = m.Groups(1).Value;
        string[] timepieces = temp.Split(new char[] {':', '.'});
        if (timepieces.Length == 4) {

            // Store duration
            Duration = new TimeSpan(0, Convert.ToInt16(timepieces[0]), Convert.ToInt16(timepieces[1]), Convert.ToInt16(timepieces[2]), Convert.ToInt16(timepieces[3]));
        }
    }

С благодарностью, Гауранга Дас.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...