Run black on all Python scripts!
This commit is contained in:
parent
fb6cfce656
commit
cd9cbcaa28
30 changed files with 1027 additions and 704 deletions
|
@ -14,7 +14,7 @@ import json
|
|||
import statistics
|
||||
import datetime
|
||||
|
||||
coloredlogs.install(level='DEBUG', fmt='%(levelname)s %(message)s')
|
||||
coloredlogs.install(level="DEBUG", fmt="%(levelname)s %(message)s")
|
||||
log = logging.getLogger()
|
||||
|
||||
# Constants
|
||||
|
@ -34,15 +34,15 @@ def videoMetadata(filename):
|
|||
p.check_returncode()
|
||||
metadataRaw = p.stdout
|
||||
data = dict()
|
||||
for metadataLine in metadataRaw.split(b'\n'):
|
||||
for metadataLine in metadataRaw.split(b"\n"):
|
||||
# Skip empty lines
|
||||
if not len(metadataLine):
|
||||
continue
|
||||
# Skip comments
|
||||
if metadataLine.startswith(b';'):
|
||||
if metadataLine.startswith(b";"):
|
||||
continue
|
||||
# Parse key-value
|
||||
metadataLineSplit = metadataLine.split(b'=')
|
||||
metadataLineSplit = metadataLine.split(b"=")
|
||||
if len(metadataLineSplit) != 2:
|
||||
log.warning("Unparsed metadata line: `{}`".format(metadataLine))
|
||||
continue
|
||||
|
@ -52,6 +52,7 @@ def videoMetadata(filename):
|
|||
data[key] = val
|
||||
return data
|
||||
|
||||
|
||||
def videoInfos(filename):
|
||||
assert os.path.isfile(filename)
|
||||
cmd = ["ffprobe", filename, "-print_format", "json", "-show_streams"]
|
||||
|
@ -61,7 +62,10 @@ def videoInfos(filename):
|
|||
infos = json.loads(infosRaw)
|
||||
return infos
|
||||
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
|
||||
def streamDuration(stream):
|
||||
if "duration" in stream:
|
||||
return float(stream["duration"])
|
||||
|
@ -77,13 +81,14 @@ def streamDuration(stream):
|
|||
else:
|
||||
raise KeyError("Can't find duration information in stream")
|
||||
|
||||
|
||||
def videoDuration(filename):
|
||||
# TODO Doesn't work with VP8 / webm
|
||||
infos = videoInfos(filename)
|
||||
durations = [streamDuration(stream) for stream in infos["streams"]]
|
||||
dev = statistics.stdev(durations)
|
||||
assert dev <= DURATION_MAX_DEV, "Too much deviation ({} s)".format(dev)
|
||||
return sum(durations)/len(durations)
|
||||
return sum(durations) / len(durations)
|
||||
|
||||
|
||||
todos = set()
|
||||
|
@ -130,13 +135,12 @@ for root, inputName in progressbar.progressbar(allVideos):
|
|||
meta = videoMetadata(inputFull)
|
||||
|
||||
# If it has the field with the original file
|
||||
if 'original' in meta:
|
||||
if "original" in meta:
|
||||
# Skip file
|
||||
continue
|
||||
else:
|
||||
assert not os.path.isfile(outputFull), outputFull + " exists"
|
||||
|
||||
|
||||
size = os.stat(inputFull).st_size
|
||||
try:
|
||||
duration = videoDuration(inputFull)
|
||||
|
@ -151,7 +155,11 @@ for root, inputName in progressbar.progressbar(allVideos):
|
|||
totalSize += size
|
||||
todos.add(todo)
|
||||
|
||||
log.info("Converting {} videos ({})".format(len(todos), datetime.timedelta(seconds=totalDuration)))
|
||||
log.info(
|
||||
"Converting {} videos ({})".format(
|
||||
len(todos), datetime.timedelta(seconds=totalDuration)
|
||||
)
|
||||
)
|
||||
|
||||
# From https://stackoverflow.com/a/3431838
|
||||
def sha256(fname):
|
||||
|
@ -161,17 +169,30 @@ def sha256(fname):
|
|||
hash_sha256.update(chunk)
|
||||
return hash_sha256.hexdigest()
|
||||
|
||||
|
||||
# Progress bar things
|
||||
totalDataSize = progressbar.widgets.DataSize()
|
||||
totalDataSize.variable = 'max_value'
|
||||
barWidgets = [progressbar.widgets.DataSize(), ' of ', totalDataSize, ' ', progressbar.widgets.Bar(), ' ', progressbar.widgets.FileTransferSpeed(), ' ', progressbar.widgets.AdaptiveETA()]
|
||||
totalDataSize.variable = "max_value"
|
||||
barWidgets = [
|
||||
progressbar.widgets.DataSize(),
|
||||
" of ",
|
||||
totalDataSize,
|
||||
" ",
|
||||
progressbar.widgets.Bar(),
|
||||
" ",
|
||||
progressbar.widgets.FileTransferSpeed(),
|
||||
" ",
|
||||
progressbar.widgets.AdaptiveETA(),
|
||||
]
|
||||
bar = progressbar.DataTransferBar(max_value=totalSize, widgets=barWidgets)
|
||||
bar.start()
|
||||
processedSize = 0
|
||||
|
||||
|
||||
for inputFull, originalFull, outputFull, size, duration in todos:
|
||||
tmpfile = tempfile.mkstemp(prefix="compressPictureMovies", suffix="."+OUTPUT_EXTENSION)[1]
|
||||
tmpfile = tempfile.mkstemp(
|
||||
prefix="compressPictureMovies", suffix="." + OUTPUT_EXTENSION
|
||||
)[1]
|
||||
try:
|
||||
# Calculate the sum of the original file
|
||||
checksum = sha256(inputFull)
|
||||
|
@ -180,7 +201,12 @@ for inputFull, originalFull, outputFull, size, duration in todos:
|
|||
originalRel = os.path.relpath(originalFull, ORIGINAL_FOLDER)
|
||||
originalContent = "{} {}".format(originalRel, checksum)
|
||||
metadataCmd = ["-metadata", 'original="{}"'.format(originalContent)]
|
||||
cmd = ["ffmpeg", "-hide_banner", "-y", "-i", inputFull] + OUTPUT_FFMPEG_PARAMETERS + metadataCmd + [tmpfile]
|
||||
cmd = (
|
||||
["ffmpeg", "-hide_banner", "-y", "-i", inputFull]
|
||||
+ OUTPUT_FFMPEG_PARAMETERS
|
||||
+ metadataCmd
|
||||
+ [tmpfile]
|
||||
)
|
||||
p = subprocess.run(cmd)
|
||||
p.check_returncode()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue