56 lines
1 KiB
Python
56 lines
1 KiB
Python
#! /usr/bin/env python3
|
|
# encoding: utf-8
|
|
|
|
import os
|
|
import pathlib
|
|
import over
|
|
import re
|
|
|
|
# --------------------------------------------------
|
|
|
|
def parse_fps(raw):
|
|
if "/" in raw:
|
|
num, den = (int(x) for x in raw.split("/"))
|
|
|
|
return float(num) / float(den)
|
|
|
|
return float(raw)
|
|
|
|
# --------------------------------------------------
|
|
|
|
def parse_time(raw):
|
|
"""
|
|
Parses ffmpeg's state sentence containing the time= token.
|
|
|
|
E.g. b"... time=00:01:11.85 ..."
|
|
|
|
Returns the time in seconds, e.g. 71.85.
|
|
"""
|
|
|
|
s = re.findall(b"time=([^ ]+) ", raw)[0]
|
|
|
|
hh, mm, ss = (float(t) for t in s.split(b":"))
|
|
|
|
return hh * 3600 + mm * 60 + ss
|
|
|
|
# --------------------------------------------------
|
|
|
|
def to_Path(raw_path):
|
|
"""
|
|
Returns pathlib.Path pointing to raw_path, handling "~/" correctly.
|
|
|
|
To be removed after python:3.5 move.
|
|
"""
|
|
|
|
if raw_path.startswith("~"):
|
|
raw_path = os.path.expanduser(raw_path)
|
|
|
|
return pathlib.Path(raw_path)
|
|
|
|
# --------------------------------------------------
|
|
|
|
def float_or_string(a):
|
|
try:
|
|
return float(a)
|
|
except:
|
|
return a
|