Last active
February 13, 2021 16:49
-
-
Save pauliusbaulius/fcff60bfe0cb4c288994d509750009eb to your computer and use it in GitHub Desktop.
webm2mp4 script'let
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
#!/usr/bin/env python3 | |
import os | |
from argparse import ArgumentParser, RawDescriptionHelpFormatter | |
from typing import List | |
from sys import exit | |
description = """ | |
webm2mp4 | |
tiny cli tool to convert all webm files to mp4 for macos brainlets. | |
want to show your friends a webm meme? imessage won't show it. | |
also for anti-google enthusiasts. fuck google, therefore fuck webm. | |
0. mv webm2mp4.py webm2mp4 | |
1. chmod +x web2mp4 | |
2. sudo mv web2mp4 /usr/local/bin/ | |
3. use from your shell :^) | |
2021-01-13 | |
""" | |
def create_output_path(input_file: os.path, output_path: os.path) -> os.path: | |
filename = os.path.splitext(input_file)[0] + ".mp4" | |
return os.path.join(output_path, filename) | |
def delete_webm(p: os.path): | |
os.remove(p) | |
def find_webms(p: os.path) -> List: | |
return [f for f in os.listdir(p) if f.endswith('.webm')] | |
def ffmpeg_convert(input_file: os.path, output_path: os.path, delete: bool): | |
# check if output_path is a file, then use it as new name, otherwise use old name. | |
is_dir_output_path = os.path.isdir(output_path) | |
output_path = output_path if not is_dir_output_path else create_output_path(input_file, output_path) | |
# convert with ffmpeg | |
# todo should add -y to overwrite and have no out to console? | |
os.system(f"ffmpeg -i {input_file} {output_path}") | |
if delete: | |
delete_webm(input_file) | |
parser = ArgumentParser(description=description, formatter_class=RawDescriptionHelpFormatter) | |
parser.add_argument("-i", "--input", type=str, help="input dir/file", required=True) | |
parser.add_argument("-o", "--output", type=str, help="output dir/file", required=True) | |
parser.add_argument( "--no-delete", action="store_false", help="do not delete webm(s) after conversion, default is delete") | |
args = parser.parse_args() | |
is_dir_input = os.path.isdir(args.input) | |
is_dir_output = os.path.isdir(args.output) | |
if is_dir_input and is_dir_output: | |
# many to many, keep name | |
webms = find_webms(args.input) | |
for webm in webms: | |
ffmpeg_convert(input_file=webm, output_path=args.output, delete=args.no_delete) | |
elif not is_dir_input: | |
# single file to new name | |
ffmpeg_convert(input_file=args.input, output_path=args.output, delete=args.no_delete) | |
else: | |
# when input is a dir and output is a single file | |
exit("Can't convert multiple webms to a single mp4!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment