-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate_video.py
103 lines (91 loc) · 3.13 KB
/
rotate_video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Rotate the video files in a folder
This requires `ffmpeg` available in the system path.
"""
import subprocess
import pathlib
import argparse
def rotate(
files: list[pathlib.Path], out_folder: pathlib.Path, speed: str, transpose: int
) -> None:
"""rotate the video files and generate them into out_folder
Args:
files (list[pathlib.Path]): The video files to be rotated.
out_folder (pathlib.Path): Must be an empty folder.
It will be created if not exists yet.
speed (str): Usually be one of ultrafast and fast.
transpose (int): 1 is 90 clock-wise; 2 is 90 counter clock-wise.
See more in https://ffmpeg.org/ffmpeg-filters.html#toc-transpose-1
Raises:
FileExistsError: May throw when `out_folder` is not empty or can't
be made.
FileNotFoundError: May throw when `files` doesn't exist.
"""
out_folder = out_folder.expanduser()
out_folder.mkdir(exist_ok=True)
if not out_folder.exists():
raise FileExistsError(f"Fail to create folder {out_folder}")
if len(list(out_folder.iterdir())) > 0:
raise FileExistsError(f"out_folder is not empty ({out_folder})")
for file in files:
if not file.exists():
raise FileNotFoundError(f"{file}")
n = len(files)
print(f"There're {n} files in total.")
for i, file in enumerate(files):
print(f"Rotating {i+1} of {n}...")
out_file = out_folder / file.name
cmd: list[str] = [
"ffmpeg",
"-i",
str(file),
"-loglevel",
"fatal",
"-nostats", # to be less verbose
"-c:v",
"libx264",
"-preset",
f"{speed}",
"-vf",
f"transpose={transpose}",
str(out_file),
]
subprocess.run(cmd, check=True)
print(f"All {n} files are done.")
def get_files(x: str) -> list[pathlib.Path]:
folder = pathlib.Path(x).expanduser()
if not folder.exists():
raise FileExistsError(folder)
def isfile_notdot(f: pathlib.Path) -> bool:
return f.is_file() and f.name[0] != "."
out = list(filter(isfile_notdot, folder.iterdir()))
out.sort()
return out
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"-t",
"--transpose",
type=int,
default=2,
help="1 is 90 clock-wise; 2 (default) is 90 counter clock-wise. "
"See more in https://ffmpeg.org/ffmpeg-filters.html#toc-transpose-1",
)
parser.add_argument(
"-s",
"--speed",
type=str,
default="ultrafast",
help="Should be one of fast and ultrafast (default)",
)
parser.add_argument(
"ffolder", help="The folder contains the video files to be rotated"
)
parser.add_argument(
"tfolder", help="The folder where the rotated files to be generated into"
)
options = parser.parse_args()
files = [pathlib.Path(f) for f in get_files(options.ffolder)]
out_folder = pathlib.Path(options.tfolder)
rotate(files, out_folder, options.speed, options.transpose)
if __name__ == "__main__":
main()