| 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 | 'movie_raw.avi', |
|---|
| 32 | '-of', 'avi', |
|---|
| 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+'.avi' |
|---|
| 39 | ) |
|---|
| 40 | ## si second_pass '-o', filename+'_first.264' |
|---|
| 41 | ## ... et seconde ligne: filename+'_raw.avi' |
|---|
| 42 | subprocess.call(cmdstring,shell=False) |
|---|
| 43 | |
|---|
| 44 | def second_pass(self,filename="output",quality=False,rate=10) : |
|---|
| 45 | bitrate="7200" |
|---|
| 46 | if quality:bitrate="50000" |
|---|
| 47 | cmdstring = ('mencoder', |
|---|
| 48 | filename+'_first.264', |
|---|
| 49 | '-of', 'rawvideo', |
|---|
| 50 | '-nosound', |
|---|
| 51 | '-ofps',str(rate), |
|---|
| 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 | |
|---|
| 61 | def close(self) : |
|---|
| 62 | self.p.stdin.close() |
|---|
| 63 | self.p.wait() |
|---|
| 64 | |
|---|