summaryrefslogtreecommitdiff
path: root/hacks/python-playback/player.py
blob: 5cc8da711f8e65d9859f47e1080729c3e632f6ad (plain)
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
import os
import threading
import subprocess
import shlex
import select
import film
import config
import mplayer

class CommandLine:
    def __init__(self):
        self.position_x = 0
        self.position_y = 0
        self.output_width = None
        self.output_height = None
        self.mov = False
        self.delay = None
        self.dvd = False
        self.dvd_title = 1
        self.content = None
        self.extra = ''
        self.crop_x = None
        self.crop_y = None
        self.crop_w = None
        self.crop_h = None
        self.deinterlace = False
        self.aid = None
        
    def get(self, with_binary):
        # hqdn3d?
        # nr, unsharp?
        # -vo x11 appears to be necessary to prevent unwanted hardware scaling
        # -noaspect stops mplayer rescaling to the movie's specified aspect ratio
        args = '-vo x11 -noaspect -ao pulse -noborder -noautosub -nosub -sws 10 '
        args += '-geometry %d:%d ' % (self.position_x, self.position_y)

        # Video filters (passed to -vf)

        filters = []

        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:
            crop = 'crop='
            if self.crop_w is not None and self.crop_h is not None:
                crop += '%d:%d' % (self.crop_w, self.crop_h)
                if self.crop_x is not None and self.crop_x is not None:
                    crop += ':%d:%d' % (self.crop_x, self.crop_y)
                filters.append(crop)

        if self.output_width is not None or self.output_height is not None:
            filters.append('scale=%d:%d' % (self.output_width, self.output_height))

        # Post processing
        pp = []
        if self.deinterlace:
            pp.append('lb')

        # Deringing filter
        pp.append('dr')

        if len(pp) > 0:
            pp_string = 'pp='
            for i in range(0, len(pp)):
                pp_string += pp[i]
                if i < len(pp) - 1:
                    pp += ','

            filters.append(pp_string)

        if len(filters) > 0:
            args += '-vf '
            for i in range(0, len(filters)):
                args += filters[i]
                if i < len(filters) - 1:
                    args += ','
            args += ' '

        if self.mov:
            args += '-demuxer mov '
        if self.delay is not None:
            args += '-delay %f ' % float(args.delay)
        if self.aid is not None:
            args += '-aid %s ' % self.aid

        args += self.extra
        
        if self.dvd:
            data_specifier = 'dvd://%d -dvd-device \"%s\"' % (self.dvd_title, self.content)
        else:
            data_specifier = '\"%s\"' % self.content

        if with_binary:
            return 'mplayer %s %s' % (args, data_specifier)
        
        return '%s %s' % (args, data_specifier)
  
def get_player(film, format):
    cl = CommandLine()
    cl.dvd = film.dvd
    cl.dvd_title = film.dvd_title
    cl.content = film.content
    cl.crop_w = film.width - film.left_crop - film.right_crop
    cl.crop_h = film.height - film.top_crop - film.bottom_crop
    cl.position_x = format.x
    if format.external:
        cl.position_x = format.x + config.LEFT_SCREEN_WIDTH
        cl.position_y = format.y
    cl.output_width = format.width
    cl.output_height = format.height
    cl.deinterlace = film.deinterlace
    cl.aid = film.aid
    return mplayer.Player(cl.get(False))