Skip to content

Instantly share code, notes, and snippets.

@williamcanin
Last active November 25, 2021 09:18
Show Gist options
  • Save williamcanin/f798b10923fe8f479133dd47bd066bb0 to your computer and use it in GitHub Desktop.
Save williamcanin/f798b10923fe8f479133dd47bd066bb0 to your computer and use it in GitHub Desktop.
Converting video formats to MOV. Compatible with Davinci Resolve.
#!/usr/bin/env python3
import argparse
from os import listdir
from os.path import isdir, join, splitext
from pathlib import Path
from subprocess import call
from typing import Union
# File with extensions to be converted
extensions = (".mp4", ".mkv", ".avi", ".wmv", ".flv")
# Codecs dict
codecs = {"video": ["libxvid", "mpeg4"], "audio": ["pcm_s24le", "pcm_s16le"]}
def menu() -> Union[str, bool]:
"""
Function that creates CLI commands
Returns:
Union[str, bool]: If it is a directory it returns a str,
the path otherwise returns False.
"""
parser = argparse.ArgumentParser(description="Converting Video to Davinci Resolve")
parser.add_argument("path", help="Adding path to videos")
args = parser.parse_args()
if isdir(args.path):
return args.path
return False
def convert_dv(path: str) -> str:
"""
Converting Video to Davinci Resolve
Args:
path (str): Specify the path where the videos to be converted are.
"""
if path is False:
return ">>> The parameter informed is not a directory. Aborted!"
all_files = listdir(path)
filter = [name for name in all_files for ext in extensions if name.endswith(ext)]
# Finish program if not found extensions
if not filter:
return f">>> Did not find any files in the list: {extensions}"
# Create folder output
output = Path(join(path, "output"))
output.mkdir(parents=True, exist_ok=True)
# Conversion process
for item in filter:
name, ext = splitext(item)
# model = ffmpeg -y -i <input_file> -codec:v mpeg4 -q:v 0 -codec:a pcm_s16le <output_file>
command = f'ffmpeg -y -i "{join(path, item)}" -c:v {codecs["video"][0]} -q:v 0 -c:a {codecs["audio"][0]} "{join(output, name)}.mov"'
call(command, shell=True, universal_newlines=True)
return "Done"
if __name__ == "__main__":
# Using: python3 convert_dv.py "<path/of/videos>"
print(convert_dv(menu()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment