Created
May 29, 2021 10:58
-
-
Save tobchen/d26060be691b2c8bbb91a64a56e4c0c0 to your computer and use it in GitHub Desktop.
Python script to convert and speed up any video to a 14.9s video for use in Instagram stories
This file contains hidden or 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
import sys | |
import subprocess | |
import re | |
if len(sys.argv) < 3: | |
print("Needs input & output arguments!") | |
exit() | |
in_path: str = sys.argv[1] | |
out_path: str = sys.argv[2] | |
process = subprocess.run(["ffmpeg", "-i", in_path], stderr=subprocess.PIPE) | |
lines = process.stderr.decode("utf-8").split("\n") | |
for line in lines: | |
match = re.search(r'Duration: (\d\d):(\d\d):(\d\d.\d\d)', line) | |
if match: | |
duration_s: float = float(match.group(1)) * 60 * 60 + float(match.group(2)) * 60 + float(match.group(3)) | |
break | |
else: | |
print("Couldn't get video duration!") | |
exit() | |
speed_factor: float = 14.9 / duration_s | |
# From: https://ubuntuforums.org/showthread.php?t=2339470 | |
subprocess.run([ | |
"ffmpeg", | |
"-i", in_path, | |
"-c:v", "libx264", | |
"-filter:v", "setpts=PTS*{:.3f}".format(speed_factor), | |
"-r", "30", | |
"-an", | |
out_path]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment