def get_duration(ffinfo):
    """
    ffinfo: json of ffprobe output.
    return the duration of the stream in seconds.
    mkv doesn't have duration, but does tags:DURATION.
    """
    duration = ffinfo.get("duration")
    if duration is not None:
        try:
            duration = float(duration)
        except Exception as e:
            return 0
        else:
            return duration
    # e.g. 00:03:20.720000000
    d = "00:00:00.000000000"
    str_dur = ffinfo.get("tags",{"DURATION":d}).get("DURATION",d)
    return sum([p*q for p,q in
                zip([float(i) for i in str_dur.split(":")],
                    [3600,60,1])])