Time extensions for FFmpeg

FFmpeg has it's own internal time format. E.g. if user wants to manually set seek parameter ("-ss") he has to convert it to FFmpeg specific format - "{hours:D}:{minutes:D2}:{seconds:D2}.{milliseconds:D3}".

There are 2 methods which can help with that problem: TimeSpan.ToFFmpeg() and string.ParseFFmpegTime()

public static string ToFFmpeg(this TimeSpan ts)
    int milliseconds = ts.Milliseconds;
    int seconds = ts.Seconds;
    int minutes = ts.Minutes;
    var hours = (int)ts.TotalHours;

    return $"{hours:D}:{minutes:D2}:{seconds:D2}.{milliseconds:D3}";
public static TimeSpan ParseFFmpegTime(this string text)
    List parts = text.Split(':')

    var milliseconds = 0;
    var seconds = 0;

    if (parts[0].Contains('.'))
        string[] secondsSplit = parts[0].Split('.');
        seconds = int.Parse(secondsSplit[0]);
        milliseconds = int.Parse(secondsSplit[1]);
        seconds = int.Parse(parts[0]);

    int minutes = int.Parse(parts[1]);
    int hours = int.Parse(parts[2]);

    return new TimeSpan(0, hours, minutes, seconds, milliseconds);

But honestly, you do not have to know what underneath those methods. That is sample use:

public IConversion SetSeek(TimeSpan? seek)
    if (seek.HasValue)
        _seek = $"-ss {seek.Value.ToFFmpeg()} ";
    return this;