-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscode.py
More file actions
executable file
·74 lines (64 loc) · 3.05 KB
/
transcode.py
File metadata and controls
executable file
·74 lines (64 loc) · 3.05 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
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""Batch transcode MKV files into mp4's"""
import argparse
import glob
import os
import shutil
import subprocess
import yaml
def main():
parser = argparse.ArgumentParser(description="""\
Batch transcode MKV files into mp4's suitable for import to Plex
Search all directories in input-dir for directories of MKV files, and then
transcode the contents of those directories, giving the largest file the
same name as the directory and storing the remaining files in a subdirectory
called "Behind The Scenes". The resulting directory is copied to output-dir,
and the original input directory is copied to archive-dir.
Wthe main title, and the remaining tracks will appear as "Behind the
Scenes" extras.hen the directories in input-dir are in the form "Title
(Year)" and output-dir is a directory that Plex searches for movies, the
largest video will appear in Plex as the main title, and the remaining
tracks will appear as "Behind the Scenes" extras.
""")
parser.add_argument('--input-dir', default='incoming',
help='Directory containing mkv files to be
transcoded')
parser.add_argument('--output-dir', default='Movies',
help='Directory in which to write transcoded
files')
parser.add_argument('--archive-dir', default='loaded',
help='Directory in which to place source of '
'sucessfully transcoded files')
args = parser.parse_args()
transcode_tmp = '/tmp/transcode'
for movie_title in os.listdir(args.input_dir):
print(movie_title)
titles = glob.glob(os.path.join(args.input_dir, movie_title, '*.mkv'))
if len(titles) == 0:
continue
titles = sorted(titles, key=os.path.getsize, reverse=True)
# ASSUME: biggest .mkv file is the main feature, and others are extras
main_mkv = titles.pop(0)
dest_dir = os.path.join(transcode_tmp, movie_title)
dest_file = os.path.join(dest_dir, movie_title + '.mp4')
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
transcode(main_mkv, dest_file)
# Put all the extras into "Behind The Scenes" and sort them out later
dest_dir = os.path.join(dest_dir, "Behind The Scenes")
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
for mkv in titles:
_, mkv_file = os.path.split(mkv)
dest_file = os.path.join(dest_dir, mkv_file.replace('.mkv','.mp4'))
transcode(mkv, dest_file)
# Move completed movies to the done area and archive input
shutil.move(os.path.join(transcode_tmp, movie_title), args.output_dir)
shutil.move(os.path.join(args.input_dir, movie_title), args.archive_dir)
def transcode(in_path, out_path):
"""Transcode the movie"""
#print(['HandBrakeCLI', '-Z', 'Super HQ 1080p30 Surround', '-O',
subprocess.run(['HandBrakeCLI', '-Z', 'Super HQ 1080p30 Surround', '-O',
'-i', in_path, '-o', out_path])
if __name__ == "__main__":
main()