-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaPreserve-FFV1.py
216 lines (211 loc) · 7.82 KB
/
MediaPreserve-FFV1.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# MediaPreserve-FFV1.py
# Version 0.6.1
import argparse
import os
import shutil
import subprocess
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument(
"source", help="path to the directory containing the Archival Object directories"
)
parser.add_argument(
"destination",
help="path to the directory into which the Archival Object directories will be moved",
)
args = parser.parse_args()
FFMPEG_CMD = "/home/linuxbrew/.linuxbrew/bin/ffmpeg"
FFPROBE_CMD = "/home/linuxbrew/.linuxbrew/bin/ffprobe"
mov_paths = list(Path(args.source).glob("**/*.mov"))
for p in sorted(mov_paths, key=lambda x: x.stat().st_size):
video_stream_count = len(
subprocess.run(
[
FFPROBE_CMD,
"-v",
"error",
"-select_streams",
"v",
"-show_entries",
"stream=index",
"-of",
"csv=p=0",
p.as_posix(),
],
capture_output=True,
text=True,
).stdout.split()
)
if video_stream_count > 1:
continue
audio_stream_count = len(
subprocess.run(
[
FFPROBE_CMD,
"-v",
"error",
"-select_streams",
"a",
"-show_entries",
"stream=index",
"-of",
"csv=p=0",
p.as_posix(),
],
capture_output=True,
text=True,
).stdout.split()
)
if audio_stream_count > 1:
continue
print(f"\n📁 {p.parent.name}")
with open(f"{p.parent}/{p.parent.name}_prsv.mkv.md", "w") as f:
f.write(
"# TRANSCODING LOG\n\nThese were the steps used to convert the source MOV file to a lossless FFV1/MKV file.\n\n"
)
"""Start 3 of 4 long-running processes at the same time in the background."""
# calculate MD5 of *.mov file
print(f"⏳ calculating source *.mov file MD5 in the background")
calculating_md5_mov_file = subprocess.Popen(
["md5sum", p.as_posix()],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# calculate MD5 of *.mov audio/video streams
print(f"⏳ calculating source *.mov streamhash as MD5 in the background")
calculating_md5_mov_streams = subprocess.Popen(
[FFMPEG_CMD, "-i", p.as_posix(), "-f", "streamhash", "-hash", "md5", "-"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# transcode *.mov to *_prsv.mkv
print(f"⏳ transcoding source *.mov to *_prsv.mkv in the background")
transcode = subprocess.Popen(
[
FFMPEG_CMD,
"-hide_banner",
"-nostats",
"-i",
p.as_posix(),
"-map",
"0",
"-dn",
"-c:v",
"ffv1",
"-level",
"3",
"-g",
"1",
"-slicecrc",
"1",
"-slices",
"4",
"-c:a",
"copy",
f"{p.parent}/{p.parent.name}_prsv.mkv",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
with open(f"{p.as_posix()}.md5") as f:
saved_md5_mov_file = f.read().split()[0].lower()
# wait for *.mov file MD5 calculation to complete
calculated_md5_mov_file = calculating_md5_mov_file.communicate()[0].split()[0]
# compare calculated MD5 of *.mov file with saved MD5 checksum file
if calculated_md5_mov_file != saved_md5_mov_file:
print("\n❌ MD5 FILE MISMATCH")
print(f"{p.name}: {calculated_md5_mov_file}")
print(f"{p.name}.md5: {saved_md5_mov_file}")
transcode.terminate()
calculating_md5_mov_streams.terminate()
exit(1)
else:
print("\n✅ MD5 FILE MATCH")
print(f"{p.name}: {calculated_md5_mov_file}")
print(f"{p.name}.md5: {saved_md5_mov_file}")
with open(f"{p.parent}/{p.parent.name}_prsv.mkv.md", "a") as f:
f.write(
"Calculated the MD5 checksum of the source MOV file and compared it with the saved MD5 checksum.\n\n"
)
f.write("Calculated MD5:\n")
f.write(f"```\n$ md5sum {p.name}\n{calculated_md5_mov_file} {p.name}\n```\n\n")
f.write("Saved MD5:\n")
f.write(f"```\n$ cat {p.name}.md5\n{saved_md5_mov_file} {p.name}.md5\n```\n\n")
f.write("FFmpeg version used to transcode the file.\n")
print_ffmpeg_version = subprocess.run(
[
FFMPEG_CMD,
"-version",
],
capture_output=True,
text=True,
).stdout.strip()
with open(f"{p.parent}/{p.parent.name}_prsv.mkv.md", "a") as f:
f.write(f"```\n$ {FFMPEG_CMD} -version\n{print_ffmpeg_version}\n```\n\n")
# wait for transcode to complete; ffmpeg writes its message output to stderr
ffmpeg_output = transcode.communicate()[1]
if transcode.returncode != 0:
print("\n❌ FFMPEG TRANSCODE FAILED")
print(ffmpeg_output)
calculating_md5_mov_streams.terminate()
exit(1)
with open(f"{p.parent}/{p.parent.name}_prsv.mkv.md", "a") as f:
f.write("FFmpeg output.\n")
f.write(
f"```\n$ {FFMPEG_CMD} -hide_banner -nostats -i {p.name} -map 0 -dn -c:v ffv1 -level 3 -g 1 -slicecrc 1 -slices 4 -c:a copy {p.parent.name}_prsv.mkv\n{ffmpeg_output}\n```\n\n"
)
# calculate MD5 hashes of *_prsv.mkv audio/video streams
calculated_md5_mkv_streams = subprocess.run(
[
FFMPEG_CMD,
"-i",
f"{p.parent}/{p.parent.name}_prsv.mkv",
"-f",
"streamhash",
"-hash",
"md5",
"-",
],
capture_output=True,
text=True,
).stdout.strip()
# wait for *.mov streamhash MD5 calculation to complete
calculated_md5_mov_streams = calculating_md5_mov_streams.communicate()[0].strip()
# compare MD5 hashes of *.mov audio/video streams with MD5 hashes of *_prsv.mkv audio/video streams
if calculated_md5_mov_streams != calculated_md5_mkv_streams:
print("\n❌ MD5 STREAM MISMATCH")
print(f"{p.name}:\n{calculated_md5_mov_streams}")
print(f"{p.parent.name}_prsv.streamhashmd5:\n{calculated_md5_mkv_streams}")
exit(1)
else:
print("\n✅ MD5 STREAM MATCH")
print(f"{p.name} (streams):\n{calculated_md5_mov_streams}")
print(f"{p.parent.name}_prsv.mkv (streams):\n{calculated_md5_mkv_streams}")
with open(f"{p.parent}/{p.parent.name}_prsv.streamhashmd5", "w") as f:
f.write(calculated_md5_mkv_streams)
with open(f"{p.parent}/{p.parent.name}_prsv.mkv.md", "a") as f:
f.write(
"Compared the calculated MD5 stream hashes from the source MOV file with those from the transcoded MKV file. Future stream hash calculations must use the same or a compatible version of FFmpeg, otherwise the output will differ.\n\n"
)
f.write("MOV stream hashes:\n")
f.write(
f"```\n$ {FFMPEG_CMD} -i {p.name} -f streamhash -hash md5 -\n{calculated_md5_mov_streams}\n```\n\n"
)
f.write("MKV stream hashes:\n")
f.write(
f"```\n$ {FFMPEG_CMD} -i {p.parent.name}_prsv.mkv -f streamhash -hash md5 -\n{calculated_md5_mkv_streams}\n```\n\n"
)
# copy everything to destination with exceptions
print(f"\n⏳ moving files to destination")
shutil.copytree(
p.parent.as_posix(),
os.path.join(args.destination, p.parent.name),
ignore=shutil.ignore_patterns("*.mov*", "*.mp4*"),
)
os.remove(f"{p.parent}/{p.parent.name}_prsv.mkv")
os.remove(f"{p.parent}/{p.parent.name}_prsv.mkv.md")
os.remove(f"{p.parent}/{p.parent.name}_prsv.streamhashmd5")
print("\n✅ DONE")