~ubuntu-branches/ubuntu/karmic/tovid/karmic

« back to all changes in this revision

Viewing changes to libtovid/spumux.py

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2008-01-24 22:04:40 UTC
  • Revision ID: james.westby@ubuntu.com-20080124220440-x7cheljduf1rdgnq
Tags: upstream-0.31
ImportĀ upstreamĀ versionĀ 0.31

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# spumux.py
 
3
 
 
4
"""This module is for adding subtitles to MPEG video files using spumux.
 
5
 
 
6
Defined here are two functions for adding subtitles to an MPEG file:
 
7
 
 
8
    add_subpictures:  Add image files (.png)
 
9
    add_subtitles:    Add subtitle files (.sub, .srt etc.)
 
10
 
 
11
Use these if you just want to add subpictures or subtitles, and don't want
 
12
to think much about the XML internals.
 
13
"""
 
14
 
 
15
__all__ = [\
 
16
    'add_subpictures',
 
17
    'add_subtitles']
 
18
 
 
19
from libtovid import utils
 
20
from libtovid import xml
 
21
 
 
22
# spumux XML elements and valid attributes
 
23
"""
 
24
subpictures
 
25
stream
 
26
textsub
 
27
    ['filename',
 
28
    'characterset',
 
29
    'fontsize',
 
30
    'font',
 
31
    'horizontal-alignment',
 
32
    'vertical-alignment',
 
33
    'left-margin',
 
34
    'right-margin',
 
35
    'subtitle-fps',
 
36
    'movie-fps',
 
37
    'movie-width',
 
38
    'movie-height']
 
39
button
 
40
    ['name',
 
41
    'x0', 'y0', # Upper-left corner, inclusively
 
42
    'x1', 'y1', # Lower-right corner, exclusively
 
43
    'up', 'down', 'left', 'right']
 
44
action
 
45
    ['name']
 
46
spu
 
47
    ['start',
 
48
    'end',
 
49
    'image',
 
50
    'highlight',
 
51
    'select',
 
52
    'transparent',   # color code
 
53
    'force',         # 'yes' (force display, required for menus)
 
54
    'autooutline',   # 'infer'
 
55
    'outlinewidth',
 
56
    'autoorder',     # 'rows' or 'columns'
 
57
    'xoffset',
 
58
    'yoffset']
 
59
"""
 
60
 
 
61
###
 
62
### Internal functions
 
63
###
 
64
 
 
65
def get_xml(textsub_or_spu):
 
66
    subpictures = xml.Element('subpictures')
 
67
    stream = subpictures.add('stream')
 
68
    stream.add_child(textsub_or_spu)
 
69
    return str(subpictures)
 
70
 
 
71
 
 
72
def get_xmlfile(textsub_or_spu):
 
73
    """Write spumux XML file for the given Textsub or Spu element, and
 
74
    return the written filename.
 
75
    """
 
76
    xmldata = get_xml(textsub_or_spu)
 
77
    xmlfile = utils.temp_file(suffix=".xml")
 
78
    xmlfile.write(xmldata)
 
79
    xmlfile.close()
 
80
    return xmlfile.name
 
81
 
 
82
 
 
83
def mux_subs(subtitle, movie_filename, stream_id=0):
 
84
    """Run spumux to multiplex the given subtitle with an .mpg file.
 
85
    
 
86
        subtitle:       Textsub or Spu element
 
87
        movie_filename: Name of an .mpg file to multiplex subtitle into
 
88
        stream_id:      Stream ID number to pass to spumux
 
89
    """
 
90
    # Create temporary .mpg file in the same directory
 
91
    base_dir = os.path.dirname(movie_filename)
 
92
    subbed_filename = temp_name(suffix=".mpg", dir=base_dir)
 
93
    # spumux xmlfile < movie_filename > subbed_filename
 
94
    spumux = Command('spumux',
 
95
                     '-s%s' % stream_id,
 
96
                     xmlfile.name)
 
97
    spumux.run_redir(movie_filename, subbed_filename)
 
98
    spumux.wait()
 
99
 
 
100
    # Remove old file
 
101
    os.remove(movie_filename)
 
102
    # Rename temporary file to new file
 
103
    os.rename(subbed_filename, movie_filename)
 
104
    # Remove the XML file
 
105
    os.remove(xmlfile.name)
 
106
 
 
107
 
 
108
###
 
109
### Exported functions
 
110
###
 
111
 
 
112
def add_subpictures(movie_filename, select, image=None, highlight=None):
 
113
    """Adds PNG image subpictures to an .mpg video file to create a DVD menu.
 
114
    
 
115
        select:    Image shown as the navigational selector or "cursor"
 
116
        image:     Image shown for non-selected regions
 
117
        highlight: Image shown when "enter" is pressed
 
118
        
 
119
    All images must be indexed, 4-color, transparent, non-antialiased PNG.
 
120
    Button regions are auto-inferred.
 
121
    """
 
122
    spu = xml.Element('spu')
 
123
    spu.set(start='0',
 
124
            force='yes',
 
125
            select=select,
 
126
            image=image,
 
127
            highlight=highlight)
 
128
    # TODO Find a good default outlinewidth
 
129
    spu.set(autooutline=infer, outlinewidth=10)
 
130
    # TODO: Support explicit button regions
 
131
    mux_subs(spu, movie_filename)
 
132
 
 
133
 
 
134
def add_subtitles(movie_filename, sub_filenames):
 
135
    """Adds one or more subtitle files to an .mpg video file.
 
136
    
 
137
        movie_filename: Name of .mpg file to add subtitles to
 
138
        sub_filenames:  Filename or list of filenames of subtitle
 
139
                        files to include (.sub/.srt etc.)
 
140
 
 
141
    """
 
142
    infile = load_media(movie_filename)
 
143
    width, height = infile.scale
 
144
 
 
145
    # Convert sub_filenames to list if necessary
 
146
    if type(sub_filenames) == str:
 
147
        sub_filenames = [sub_filenames]
 
148
    # spumux each subtitle file with its own stream ID
 
149
    for stream_id, sub_filename in enumerate(sub_filenames):
 
150
        # <textsub attribute="value" .../>
 
151
        textsub = xml.Element('textsub')
 
152
        textsub.set(movie_fps=infile.fps,
 
153
                    movie_width=width,
 
154
                    movie_height=height,
 
155
                    filename=sub_filename)
 
156
        mux_subs(textsub, movie_filename, stream_id)
 
157
 
 
158
 
 
159
if __name__ == '__main__':
 
160
    print "spumux XML examples"
 
161
 
 
162
    print "Subpicture example:"
 
163
    spu = xml.Element('spu')
 
164
    spu.add('button', name='but1', down='but2')
 
165
    spu.add('button', name='but2', up='but1')
 
166
    print get_xml(spu)
 
167
 
 
168
    print "Text subtitle example:"
 
169
    textsub = xml.Element('textsub')
 
170
    textsub.set(filename='foo.sub',
 
171
                fontsize=14.0,
 
172
                font="Luxi Mono")
 
173
    print get_xml(textsub)