[430] | 1 | import numpy as np |
---|
| 2 | import subprocess |
---|
[444] | 3 | from os import system |
---|
[430] | 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), |
---|
[444] | 13 | '-o', filename+'_raw.avi', |
---|
| 14 | '-nosound', |
---|
| 15 | '-ovc', 'x264', |
---|
[448] | 16 | '-msglevel', 'all=-1' |
---|
[444] | 17 | # '-ovc', 'lavc', |
---|
[430] | 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()) |
---|
[444] | 24 | |
---|
[445] | 25 | def first_pass(self,filename="output",quality=False,rate=10) : |
---|
[444] | 26 | |
---|
| 27 | |
---|
| 28 | bitrate="7200" |
---|
| 29 | if quality:bitrate="50000" |
---|
| 30 | cmdstring = ('mencoder', |
---|
[864] | 31 | 'movie_raw.avi', |
---|
| 32 | '-of', 'avi', |
---|
[444] | 33 | '-nosound', |
---|
[445] | 34 | '-ofps',str(rate), |
---|
[444] | 35 | '-ovc', 'x264', |
---|
| 36 | '-x264encopts', 'subq=1:frameref=1:bitrate='+bitrate+':bframes=1:pass=1', |
---|
| 37 | '-vf', 'scale=1280:720', |
---|
[864] | 38 | '-o', filename+'.avi' |
---|
[444] | 39 | ) |
---|
[864] | 40 | ## si second_pass '-o', filename+'_first.264' |
---|
| 41 | ## ... et seconde ligne: filename+'_raw.avi' |
---|
[444] | 42 | subprocess.call(cmdstring,shell=False) |
---|
| 43 | |
---|
[445] | 44 | def second_pass(self,filename="output",quality=False,rate=10) : |
---|
[444] | 45 | bitrate="7200" |
---|
| 46 | if quality:bitrate="50000" |
---|
| 47 | cmdstring = ('mencoder', |
---|
| 48 | filename+'_first.264', |
---|
| 49 | '-of', 'rawvideo', |
---|
| 50 | '-nosound', |
---|
[445] | 51 | '-ofps',str(rate), |
---|
[444] | 52 | '-ovc', 'x264', |
---|
| 53 | '-x264encopts', 'subq=6:frameref=5:bitrate='+bitrate+':me=umh:partitions=all:bframes=1:me_range=16:cabac:weightb:deblock:pass=2', |
---|
| 54 | '-vf', 'scale=1280:720', |
---|
| 55 | '-o', filename+'.avi' |
---|
| 56 | ) |
---|
| 57 | system('rm -f '+filename+'_raw.avi') |
---|
| 58 | subprocess.call(cmdstring,shell=False) |
---|
| 59 | system('rm -f '+filename+'_first.264') |
---|
| 60 | |
---|
[430] | 61 | def close(self) : |
---|
| 62 | self.p.stdin.close() |
---|
[444] | 63 | self.p.wait() |
---|
| 64 | |
---|