-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_media.py
More file actions
executable file
·66 lines (56 loc) · 1.95 KB
/
check_media.py
File metadata and controls
executable file
·66 lines (56 loc) · 1.95 KB
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
#!/usr/bin/env python
# -*- coding utf-8 -*-
import argparse
from pathlib import Path
from os import chdir
import ffmpeg
args_parser = argparse.ArgumentParser(
prog="check-media", description="Directory of media files to process."
)
args_parser.add_argument(
"directory",
metavar="directory",
type=str,
help="Enter the directory to search for media files to process.",
)
def get_files(extension):
"""Retrieve all files with specified extension"""
all_files = []
for ext in extension:
all_files.extend(Path(args.directory).rglob(ext))
return all_files
def process_files(file_list):
"""Check files passed in to see if they are .avi or audio is not ac3 and recode as required"""
for file in file_list:
# Change to file dir and generate probe info and new_filename
chdir(file.parent)
info = ffmpeg.probe(file, select_streams="a:0")
new_filename = str(file.name).split(".", maxsplit=1)[0] + ".mp4"
if file.suffix == ".avi":
try:
(
ffmpeg.input(file.name)
.output(new_filename, acodec="ac3", vcodec="copy")
.run(capture_stdout=True, capture_stderr=True)
)
Path.unlink(file)
except ffmpeg.Error as error:
print(error.stderr)
continue
elif info["streams"][0]["codec_name"] != "ac3":
try:
(
ffmpeg.input(str(file.name))
.output(new_filename, acodec="ac3", vcodec="copy")
.run(capture_stdout=True, capture_stderr=True)
)
Path.unlink(file)
except ffmpeg.Error as error:
print(error.stderr)
continue
else:
continue
if __name__ == "__main__":
args = args_parser.parse_args()
files = get_files(("*.mkv", "*.mp4", "*.avi"))
process_files(files)