-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_videos.py
107 lines (87 loc) · 3.3 KB
/
process_videos.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
104
105
106
107
#!/usr/bin/env python3
import os
import sys
import itertools
import shutil
import subprocess
import re
def flattenDirectory(destination):
all_files = []
for root, _dirs, files in itertools.islice(os.walk(destination), 1, None):
for filename in files:
all_files.append(os.path.join(root, filename))
for filename in all_files:
newpath=os.path.join(destination, os.path.basename(filename))
if (os.path.exists(newpath)):
os.remove(newpath)
shutil.move(filename, destination)
def getAllFiles(directory):
all_files = os.listdir(directory)
return all_files
def getVideoFiles(directory):
all_files = getAllFiles(directory)
video_files = []
for name in all_files:
if (name.endswith((".mkv", ".avi", ".mpg", ".mpeg", ".mp4", ".wmv", ".ts"))):
video_files.append(name)
return video_files
def removeSmallFiles(directory, threshold):
all_files = getAllFiles(directory)
for name in all_files:
file=os.path.join(directory, name)
isFolder=os.path.isdir(file)
if (os.path.isdir(file)):
shutil.rmtree(file)
continue
size=os.path.getsize(file) >> 20
if (size < threshold):
print("Removing File (size too small): " + file)
os.remove(file)
def removeVideoMetaData(directory):
files = getVideoFiles(directory)
for f in files:
orig=os.path.join(directory, f)
withmeta=orig + ".withmeta"
os.rename(orig, withmeta)
print("Probing metadata in " + f)
process = subprocess.Popen(["ffprobe", "-v", "error", "-show_entries", "stream_tags=language", "-of", "default=noprint_wrappers=1", withmeta], stdout=subprocess.PIPE)
out, err = process.communicate()
print(out)
print("Removing metadata from " + f)
if re.search(b"language=eng", out, re.IGNORECASE):
print("Found english track, removing all other audio tracks")
subprocess.call(["ffmpeg", "-loglevel", "error", "-y", "-i", withmeta, "-map", "0:v", "-map", "0:m:language:eng", "-c", "copy", "-map_metadata", "-1", "-metadata", "title=", "-metadata", "comment=", orig])
else:
print("Did NOT find english track, retaining all audio tracks")
subprocess.call(["ffmpeg", "-loglevel", "error", "-y", "-i", withmeta, "-map", "0:v", "-map", "0:a", "-c", "copy", "-map_metadata", "-1", "-metadata", "title=", "-metadata", "comment=", orig])
os.remove(withmeta)
#ffmpeg -y -i "fwc.mp4" -c copy -map_metadata -1 -metadata title="" -metadata comments="" "fwc_test.mp4"
try:
(scriptname,directory,orgnzbname,jobname,reportnumber,category,group,postprocstatus,url) = sys.argv
except:
try:
directory = sys.argv[1]
jobname = sys.argv[3]
except:
print("No commandline parameters found")
sys.exit(1)
# flatten
print("Flattening contents of " + directory)
flattenDirectory(directory)
# remove files under 60MB
removeSmallFiles(directory, 60)
# handle obfuscated files
video_files = getVideoFiles(directory)
if len(video_files) == 1:
existing=video_files[0]
fp=directory + "/" + existing
ext=os.path.splitext(existing)[-1]
new=jobname + ext
if (new.lower() != existing.lower()):
print("Renaming " + existing + " to " + new)
shutil.move(fp, os.path.join(directory,new))
# remove metadata
removeVideoMetaData(directory)
# Success code
print("Completed")
sys.exit(0)