2023-11-26 21:04:58 +01:00
|
|
|
#!/usr/bin/env nix-shell
|
|
|
|
#! nix-shell -i python3 --pure
|
|
|
|
#! nix-shell -p python3 ffmpeg
|
2020-12-27 14:20:44 +01:00
|
|
|
|
2023-03-18 14:55:54 +01:00
|
|
|
# Compress a video to make it fit under a certain size.
|
|
|
|
# Usage: videoQuota SIZE SRC DST
|
|
|
|
# SIZE: destination video size in bytes
|
|
|
|
# SRC: source video file
|
|
|
|
# DST: destination video file
|
|
|
|
# Example: videoQuota 20971520 source.mov dest.mp4
|
|
|
|
# To make a ~20 MiB MP4 of a MOV video
|
|
|
|
|
2020-12-27 14:20:44 +01:00
|
|
|
import subprocess
|
2023-11-23 22:59:09 +01:00
|
|
|
import sys
|
2020-12-27 14:20:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
def duration_file(path: str) -> float:
|
|
|
|
cmd = [
|
|
|
|
"ffprobe",
|
|
|
|
"-v",
|
|
|
|
"error",
|
|
|
|
"-show_entries",
|
|
|
|
"format=duration",
|
|
|
|
"-of",
|
|
|
|
"default=noprint_wrappers=1:nokey=1",
|
|
|
|
path,
|
|
|
|
]
|
|
|
|
run = subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
|
|
|
|
ret = run.stdout.decode().strip()
|
|
|
|
return float(ret)
|
|
|
|
|
|
|
|
|
|
|
|
# Constants
|
|
|
|
audio_br_bi = 128000
|
|
|
|
|
|
|
|
# TODO Arguments if you feel like it
|
|
|
|
quota_by = int(sys.argv[1])
|
|
|
|
in_file = sys.argv[2]
|
|
|
|
out_file = sys.argv[3]
|
2021-12-17 22:13:27 +01:00
|
|
|
filters = sys.argv[4:]
|
2020-12-27 14:20:44 +01:00
|
|
|
|
|
|
|
quota_bi = quota_by * 8
|
|
|
|
duration = duration_file(in_file)
|
|
|
|
tot_br_bi = quota_bi / duration
|
|
|
|
video_br_bi = int(tot_br_bi - audio_br_bi)
|
|
|
|
assert video_br_bi > 0, "Not even enough space for audio"
|
|
|
|
|
2021-12-17 22:41:47 +01:00
|
|
|
cmd = (
|
|
|
|
[
|
|
|
|
"ffmpeg",
|
|
|
|
"-i",
|
|
|
|
in_file,
|
|
|
|
]
|
|
|
|
+ filters
|
|
|
|
+ [
|
|
|
|
"-b:v",
|
|
|
|
str(video_br_bi),
|
|
|
|
"-b:a",
|
|
|
|
str(audio_br_bi),
|
|
|
|
out_file,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
print(" ".join(cmd))
|
2020-12-27 14:20:44 +01:00
|
|
|
subprocess.run(cmd, check=True)
|