1 | import numpy as np |
---|
2 | import subprocess |
---|
3 | from os import system |
---|
4 | |
---|
5 | class VideoSink(object) : |
---|
6 | |
---|
7 | def __init__( self, size, filename="output", rate=10, byteorder="bgra") : |
---|
8 | self.size = size |
---|
9 | cmdstring = ('mencoder', |
---|
10 | '/dev/stdin', |
---|
11 | '-demuxer', 'rawvideo', |
---|
12 | '-rawvideo', 'w=%i:h=%i'%size[::-1]+":fps=%i:format=%s"%(rate,byteorder), |
---|
13 | '-o', filename+'_raw.avi', |
---|
14 | '-nosound', |
---|
15 | '-ovc', 'x264', |
---|
16 | '-msglevel', 'all=-1' |
---|
17 | # '-ovc', 'lavc', |
---|
18 | ) |
---|
19 | self.p = subprocess.Popen(cmdstring, stdin=subprocess.PIPE, shell=False) |
---|
20 | |
---|
21 | def run(self, image) : |
---|
22 | #assert image.shape[0:2] == self.size |
---|
23 | self.p.stdin.write(image.tostring()) |
---|
24 | |
---|
25 | def first_pass(self,filename="output",quality=False,rate=10) : |
---|
26 | |
---|
27 | |
---|
28 | bitrate="7200" |
---|
29 | if quality:bitrate="50000" |
---|
30 | cmdstring = ('mencoder', |
---|
31 | filename+'_raw.avi', |
---|
32 | '-of', 'rawvideo', |
---|
33 | '-nosound', |
---|
34 | '-ofps',str(rate), |
---|
35 | '-ovc', 'x264', |
---|
36 | '-x264encopts', 'subq=1:frameref=1:bitrate='+bitrate+':bframes=1:pass=1', |
---|
37 | '-vf', 'scale=1280:720', |
---|
38 | '-o', filename+'_first.264' |
---|
39 | ) |
---|
40 | subprocess.call(cmdstring,shell=False) |
---|
41 | |
---|
42 | def second_pass(self,filename="output",quality=False,rate=10) : |
---|
43 | bitrate="7200" |
---|
44 | if quality:bitrate="50000" |
---|
45 | cmdstring = ('mencoder', |
---|
46 | filename+'_first.264', |
---|
47 | '-of', 'rawvideo', |
---|
48 | '-nosound', |
---|
49 | '-ofps',str(rate), |
---|
50 | '-ovc', 'x264', |
---|
51 | '-x264encopts', 'subq=6:frameref=5:bitrate='+bitrate+':me=umh:partitions=all:bframes=1:me_range=16:cabac:weightb:deblock:pass=2', |
---|
52 | '-vf', 'scale=1280:720', |
---|
53 | '-o', filename+'.avi' |
---|
54 | ) |
---|
55 | system('rm -f '+filename+'_raw.avi') |
---|
56 | subprocess.call(cmdstring,shell=False) |
---|
57 | system('rm -f '+filename+'_first.264') |
---|
58 | |
---|
59 | def close(self) : |
---|
60 | self.p.stdin.close() |
---|
61 | self.p.wait() |
---|
62 | |
---|