forked from kevmitch/mpv-autocrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpv_utils.py
138 lines (126 loc) · 4.41 KB
/
mpv_utils.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
import os,sys,shutil
from subprocess import Popen,PIPE,STDOUT
from contextlib import contextmanager
import mpv_utils
def import_err_msg(module=None,package=None):
assert module and packge
print """ This script requires %s
On Debian/Ubuntu:
apt-get install %s
"""%(module,package)
sys.exit(1)
try:
import numpy as np
except ImportError:
import_err_msg("numpy","python-numpy")
try:
from scipy.ndimage import imread
except ImportError:
import_err_msg("scipy","python-scipy")
try:
import PIL
except ImportError:
import_err_msg("python imaging library","python-imaging")
@contextmanager
def tmp_dir():
"""
safe temporary directory context manager
creates a temporary directory and returns it's path
to be used in a with statment
"""
import tempfile
tmp_dir_path=tempfile.mkdtemp()
yield tmp_dir_path
#execute body of with statment
shutil.rmtree(tmp_dir_path)
@contextmanager
def tmp_file():
"""
safe temporary file context manager
creates a temporary file and returns it's path
to be used in a with statment
"""
import tempfile
tmp_fd,tmp_file_path=tempfile.mkstemp(text='true')
yield tmp_file_path
#execute body of with statment
os.close(tmp_fd)
os.remove(tmp_file_path)
script_dir,this_file=os.path.split(__file__)
default_playlist_script=os.path.join(script_dir,'write_playlist.lua')
def get_playlist_files(mpv_args,mpv_lua_script=default_playlist_script):
"""
invoke mpv with the write_playlist.lua script and return the playlist as a python list
"""
for func in os.path.expanduser,os.path.abspath:
mpv_lua_script=func(mpv_lua_script)
mpv_lua_script_name,ext=os.path.splitext(os.path.basename(mpv_lua_script))
with tmp_file() as tmp_path:
cmd=['mpv']+mpv_args
cmd+=['--lua=%s'%mpv_lua_script,
'--lua-opts=%s.out_file=%s'%(mpv_lua_script_name,tmp_path),
'--vo=null',
'--ao=null',
'--no-audio',
'--no-cache',
'--no-sub']
p=Popen(cmd,stdout=PIPE,stderr=STDOUT)
stdout,stderr=p.communicate()
rc=p.wait()
if rc!=0:
print 'mpv get playlist command exited with non-zero status'
print 'COMMAND WAS'
print cmd
print 'STDOUT/STDERR was'
print stdout
sys.exit(1)
with open(tmp_path,'r') as tmp_object:
playlist=tmp_object.read().strip('\0')
if len(playlist)==0:
playlist=[]
else:
playlist=playlist.split('\0')
return playlist
default_scan_script=os.path.join(script_dir,'scan.lua')
def sample_screenshots(nshots,mpv_lua_script=default_scan_script,mpv_args=[]):
"""
invoke mpv with the scan.lua script and image vo and return a numpy array of screenshots
"""
for func in os.path.expanduser,os.path.abspath:
mpv_lua_script=func(mpv_lua_script)
mpv_lua_script_name,ext=os.path.splitext(os.path.basename(mpv_lua_script))
# don't += here since keyword arguments are like static function variables,
# but the explicit assignment creates a new local instance
mpv_args=mpv_args+['--no-cache',
'--lua=%s'%(mpv_lua_script),
'--lua-opts=%s.num_frames=%d'%(mpv_lua_script_name,nshots)]
return dump_images(mpv_args=mpv_args)
def dump_images(mpv_args=[]):
"""
invoke mpv with the scan.lua script and image vo and return a numpy array of screenshots
"""
with tmp_dir() as tmp_dir_path:
cmd=['mpv']+mpv_args
cmd+=['--no-config',
'--no-resume-playback',
'--vo=image:outdir=%s'%tmp_dir_path,
'--ao=null',
'--no-audio']
print ' '.join(cmd)
p=Popen(cmd,stdout=PIPE,stderr=STDOUT)
stdout,stderr=p.communicate()
rc=p.wait()
if rc!=0:
print 'mpv screenshot command exited with non-zero status'
print 'COMMAND WAS'
print cmd
print 'STDOUT/STDERR was'
print stdout
sys.exit(1)
fpaths=[os.path.join(tmp_dir_path,fname) for fname in os.listdir(tmp_dir_path)]
ims=imread(fpaths[0])
shape=[len(fpaths)]+list(ims.shape)
ims.resize(shape,refcheck=False)#add spaces for the other images
for i in xrange(1,len(fpaths)):
ims[i]=imread(fpaths[i])
return ims