forked from happyhorseskull/you-can-datamosh-on-linux
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvector_motion.py
51 lines (41 loc) · 1.94 KB
/
vector_motion.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
#!/usr/bin/env python3
import argparse
import subprocess
import os
import importlib.util as imp
from vector_util import get_vectors, apply_vectors
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('input_video', type=str, help='specifies input file')
parser.add_argument('-s', type=str, dest='script_path', help='path to the script', required=True)
parser.add_argument('-g', type=str, default=1000, dest='gop_period', help='I-frame period (in frames)')
parser.add_argument('-o', default='moshed.mpg', type=str, dest='output_video',
help='output file for the moshed video')
return parser.parse_args().__dict__
def get_moshing_function(path):
spec = imp.spec_from_file_location('mod', path)
mod = imp.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod.mosh_frames
if __name__ == '__main__':
args = parse_args()
input_video = args['input_video']
gop_period = args['gop_period']
output_video = args['output_video']
script_path = args['script_path']
script_type = script_path[-2:]
if script_type == 'py':
try:
# import the function responsible for moshing from the specified script
func = get_moshing_function(script_path)
# method = '' specifies to just copy the new vectors over to the original video
apply_vectors(func(get_vectors(input_video)), input_video, output_video, method='')
except Exception as e:
# TODO: proper error handling
print(f'couldn\'t apply function mosh_frames from script: {script_path}:\n{e}')
exit(0)
else:
subprocess.call(f'ffgac -i {input_video} -an -mpv_flags +nopimb+forcemv -qscale:v 0 -g {gop_period}' +
' -vcodec mpeg2video -f rawvideo -y tmp.mpg', shell=True)
subprocess.call(f'ffedit -i tmp.mpg -f mv -s {script_path} -o {output_video}', shell=True)
os.remove('tmp.mpg')