Last active
July 31, 2022 16:48
-
-
Save BillyDoesDev/05e53905c894ff302fbfc67f1d2d33ab to your computer and use it in GitHub Desktop.
change the resoultion of a video on the fly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# python vidres.py path/to/input/video [path/to/output/video] | |
import os | |
import re | |
import shutil | |
import subprocess | |
import sys | |
data = subprocess.run( | |
["ffprobe", "-i", sys.argv[1]], capture_output=True, check=None | |
).stderr.decode() | |
h, m, s = map(float, re.findall(r"\d\d:\d\d:\d\d\.\d\d", data)[0].split(":")) | |
width, height = map( | |
int, re.findall(r"\d+x\d+", re.findall(r"Stream.*\)", data)[0])[-1].split("x") | |
) | |
try: | |
os.mkdir("temp") | |
OUT_FILE = sys.argv[2] | |
except (FileExistsError, IndexError): | |
OUT_FILE = "output.webm" | |
# split clips | |
count = 0 | |
for hours in range(int(h + 1)): | |
for minuites in range(int(m + 1)): | |
for seconds in range(int(s + 1)): | |
for splitseconds in range(0, 100, 10): | |
subprocess.run( | |
[ | |
"ffmpeg", | |
"-y", | |
"-ss", | |
str(hours).rjust(2, "0") | |
+ ":" | |
+ str(minuites).rjust(2, "0") | |
+ ":" | |
+ str(seconds).rjust(2, "0") | |
+ "." | |
+ str(splitseconds).rjust(2, "0"), | |
"-t", | |
"00:00:00.10", | |
"-i", | |
sys.argv[1], | |
"-c:a", | |
"copy", | |
f"temp/.{count}.mp4", | |
], | |
check=None, | |
) | |
count += 1 | |
## `count` is the total number of clips that were split from the original video | |
# scale clips | |
for _ in range(count - 10): # skip the last second | |
# why (count - 10)? cuz we split the clip into secions of `10`/100 seconds each | |
subprocess.run( | |
[ | |
"ffmpeg", | |
"-y", | |
"-i", | |
f"temp/.{_}.mp4", | |
"-vf", | |
f"scale={width}:{height}", | |
f"temp/_{_}.webm", | |
], | |
check=None, | |
) | |
## Do all your frame manipulation here | |
if _ // max(1, count // 14) % 2 != 0: # (count//n), `n` is (roughly) how many times the effect alternates | |
height += 200 # use whatever that works best for you | |
else: | |
height -= 200 | |
# speed up clips | |
with open("src.txt", mode="w", encoding="utf-8") as f: | |
for index in range(count - 10): | |
subprocess.run( | |
[ | |
"ffmpeg", | |
"-y", | |
"-i", | |
f"temp/_{index}.webm", | |
"-filter:v", | |
f"setpts=PTS/{10e50}", | |
f"temp/{index}.webm", | |
], | |
check=None, | |
) | |
f.write(f"file 'temp/{index}.webm'\n") | |
# concat clips | |
subprocess.run( | |
[ | |
"ffmpeg", | |
"-y", | |
"-f", | |
"concat", | |
"-safe", | |
"0", | |
"-i", | |
"src.txt", | |
"-c", | |
"copy", | |
"-an", # mutes audio | |
OUT_FILE, | |
], | |
stderr=subprocess.DEVNULL, | |
check=None, | |
) | |
# clean stuff up | |
shutil.rmtree("temp") | |
os.remove("src.txt") | |
# Then you can use something like this to add in audio | |
# ffmpeg -i video.webm -i audio.wav -map 0:v -map 1:a -c:v copy -shortest out.webm | |
# The `-shortest` option will make the output the same duration as the shortest input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment