Fix build again.
[dcpomatic.git] / hacks / python-playback / player.py
1 import os
2 import threading
3 import subprocess
4 import shlex
5 import select
6 import film
7 import config
8 import mplayer
9
10 class CommandLine:
11     def __init__(self):
12         self.position_x = 0
13         self.position_y = 0
14         self.output_width = None
15         self.output_height = None
16         self.mov = False
17         self.delay = None
18         self.dvd = False
19         self.dvd_title = 1
20         self.content = None
21         self.extra = ''
22         self.crop_x = None
23         self.crop_y = None
24         self.crop_w = None
25         self.crop_h = None
26         self.deinterlace = False
27         self.aid = None
28         
29     def get(self, with_binary):
30         # hqdn3d?
31         # nr, unsharp?
32         # -vo x11 appears to be necessary to prevent unwanted hardware scaling
33         # -noaspect stops mplayer rescaling to the movie's specified aspect ratio
34         args = '-vo x11 -noaspect -ao pulse -noborder -noautosub -nosub -sws 10 '
35         args += '-geometry %d:%d ' % (self.position_x, self.position_y)
36
37         # Video filters (passed to -vf)
38
39         filters = []
40
41         if self.crop_x is not None or self.crop_y is not None or self.crop_w is not None or self.crop_h is not None:
42             crop = 'crop='
43             if self.crop_w is not None and self.crop_h is not None:
44                 crop += '%d:%d' % (self.crop_w, self.crop_h)
45                 if self.crop_x is not None and self.crop_x is not None:
46                     crop += ':%d:%d' % (self.crop_x, self.crop_y)
47                 filters.append(crop)
48
49         if self.output_width is not None or self.output_height is not None:
50             filters.append('scale=%d:%d' % (self.output_width, self.output_height))
51
52         # Post processing
53         pp = []
54         if self.deinterlace:
55             pp.append('lb')
56
57         # Deringing filter
58         pp.append('dr')
59
60         if len(pp) > 0:
61             pp_string = 'pp='
62             for i in range(0, len(pp)):
63                 pp_string += pp[i]
64                 if i < len(pp) - 1:
65                     pp += ','
66
67             filters.append(pp_string)
68
69         if len(filters) > 0:
70             args += '-vf '
71             for i in range(0, len(filters)):
72                 args += filters[i]
73                 if i < len(filters) - 1:
74                     args += ','
75             args += ' '
76
77         if self.mov:
78             args += '-demuxer mov '
79         if self.delay is not None:
80             args += '-delay %f ' % float(args.delay)
81         if self.aid is not None:
82             args += '-aid %s ' % self.aid
83
84         args += self.extra
85         
86         if self.dvd:
87             data_specifier = 'dvd://%d -dvd-device \"%s\"' % (self.dvd_title, self.content)
88         else:
89             data_specifier = '\"%s\"' % self.content
90
91         if with_binary:
92             return 'mplayer %s %s' % (args, data_specifier)
93         
94         return '%s %s' % (args, data_specifier)
95   
96 def get_player(film, format):
97     cl = CommandLine()
98     cl.dvd = film.dvd
99     cl.dvd_title = film.dvd_title
100     cl.content = film.content
101     cl.crop_w = film.width - film.left_crop - film.right_crop
102     cl.crop_h = film.height - film.top_crop - film.bottom_crop
103     cl.position_x = format.x
104     if format.external:
105         cl.position_x = format.x + config.LEFT_SCREEN_WIDTH
106         cl.position_y = format.y
107     cl.output_width = format.width
108     cl.output_height = format.height
109     cl.deinterlace = film.deinterlace
110     cl.aid = film.aid
111     return mplayer.Player(cl.get(False))
112