1# $Id: Subtitle.pm 2187 2006-08-16 19:34:38Z joern $
2
3#-----------------------------------------------------------------------
4# Copyright (C) 2001-2006 J�rn Reder <joern AT zyn.de>.
5# All Rights Reserved. See file COPYRIGHT for details.
6#
7# This module is part of Video::DVDRip, which is free software; you can
8# redistribute it and/or modify it under the same terms as Perl itself.
9#-----------------------------------------------------------------------
10
11package Video::DVDRip::Subtitle;
12
13use Locale::TextDomain qw (video.dvdrip);
14
15use base Video::DVDRip::Base;
16
17use Video::DVDRip::SrtxFile;
18
19use Carp;
20use strict;
21
22sub title                       { shift->{title}                        }
23sub id                          { shift->{id}                           }
24sub lang                        { shift->{lang}                         }
25sub tc_preview_img_cnt          { shift->{tc_preview_img_cnt}           }
26sub tc_preview_timecode         { shift->{tc_preview_timecode}          }
27sub tc_vobsub                   { shift->{tc_vobsub}                    }
28sub tc_render                   { shift->{tc_render}                    }
29sub tc_vertical_offset          { shift->{tc_vertical_offset}           }
30sub tc_time_shift               { shift->{tc_time_shift}                }
31sub tc_postprocess              { shift->{tc_postprocess}               }
32sub tc_antialias                { shift->{tc_antialias}                 }
33sub tc_color_manip              { shift->{tc_color_manip}               }
34sub tc_color_a                  { shift->{tc_color_a}                   }
35sub tc_color_b                  { shift->{tc_color_b}                   }
36sub tc_assign_color_a           { shift->{tc_assign_color_a}            }
37sub tc_assign_color_b           { shift->{tc_assign_color_b}            }
38sub tc_test_image_cnt           { shift->{tc_test_image_cnt}            }
39
40sub set_title                   { shift->{title}                = $_[1] }
41sub set_id                      { shift->{id}                   = $_[1] }
42sub set_lang                    { shift->{lang}                 = $_[1] }
43sub set_tc_preview_img_cnt      { shift->{tc_preview_img_cnt}   = $_[1] }
44sub set_tc_preview_timecode     { shift->{tc_preview_timecode}  = $_[1] }
45sub set_tc_vobsub               { shift->{tc_vobsub}            = $_[1] }
46sub set_tc_render               { shift->{tc_render}            = $_[1] }
47sub set_tc_vertical_offset      { shift->{tc_vertical_offset}   = $_[1] }
48sub set_tc_time_shift           { shift->{tc_time_shift}        = $_[1] }
49sub set_tc_postprocess          { shift->{tc_postprocess}       = $_[1] }
50sub set_tc_antialias            { shift->{tc_antialias}         = $_[1] }
51sub set_tc_color_manip          { shift->{tc_color_manip}       = $_[1] }
52sub set_tc_color_a              { shift->{tc_color_a}           = $_[1] }
53sub set_tc_color_b              { shift->{tc_color_b}           = $_[1] }
54sub set_tc_assign_color_a       { shift->{tc_assign_color_a}    = $_[1] }
55sub set_tc_assign_color_b       { shift->{tc_assign_color_b}    = $_[1] }
56sub set_tc_test_image_cnt       { shift->{tc_test_image_cnt}    = $_[1] }
57sub set_ripped_images_cnt       { shift->{ripped_images_cnt}    = $_[1] }
58
59sub ripped_images_cnt {
60    my $self = shift;
61
62    return $self->{ripped_images_cnt}
63        if $self->{ripped_images_cnt};
64
65    my $dir = $self->preview_dir;
66
67    my @files = glob("$dir/*.{pgm,png}");
68    my $cnt   = @files;
69
70    $self->{ripped_images_cnt} = $cnt;
71
72    return $cnt;
73}
74
75sub new {
76    my $class = shift;
77    my %par   = @_;
78    my ( $id, $lang, $tc_preview_img_cnt, $tc_preview_timecode )
79        = @par{ 'id', 'lang', 'tc_preview_img_cnt', 'tc_preview_timecode' };
80    my ( $tc_vobsub, $tc_render, $tc_vertical_offset, $tc_time_shift )
81        = @par{ 'tc_vobsub', 'tc_render', 'tc_vertical_offset',
82        'tc_time_shift' };
83    my ( $tc_antialias, $tc_color_a, $tc_color_b, $tc_assign_color_a )
84        = @par{ 'tc_antialias', 'tc_color_a', 'tc_color_b',
85        'tc_assign_color_a' };
86    my ( $tc_assign_color_b, $tc_test_image_cnt, $title, $tc_color_manip )
87        = @par{
88        'tc_assign_color_b', 'tc_test_image_cnt',
89        'title',             'tc_color_manip'
90        };
91    my ($tc_postprocess) = @par{'tc_postprocess'};
92
93    $tc_preview_img_cnt  = 20         if not defined $tc_preview_img_cnt;
94    $tc_preview_timecode = "00:00:00" if not defined $tc_preview_timecode;
95
96    $tc_test_image_cnt  ||= 1;
97    $tc_color_a         ||= 0;
98    $tc_color_b         ||= 0;
99    $tc_assign_color_a  ||= 0;
100    $tc_assign_color_b  ||= 0;
101    $tc_antialias       ||= 1;
102    $tc_vertical_offset ||= 0;
103    $tc_time_shift      ||= 0;
104    $tc_color_manip     ||= 0;
105    $tc_postprocess     ||= 0;
106
107    my $self = {
108        title               => $title,
109        id                  => $id,
110        lang                => $lang,
111        tc_preview_img_cnt  => $tc_preview_img_cnt,
112        tc_preview_timecode => $tc_preview_timecode,
113        tc_vobsub           => $tc_vobsub,
114        tc_render           => $tc_render,
115        tc_vertical_offset  => $tc_vertical_offset,
116        tc_time_shift       => $tc_time_shift,
117        tc_postprocess      => $tc_postprocess,
118        tc_antialias        => $tc_antialias,
119        tc_color_manip      => $tc_color_manip,
120        tc_color_a          => $tc_color_a,
121        tc_color_b          => $tc_color_b,
122        tc_assign_color_a   => $tc_assign_color_a,
123        tc_assign_color_b   => $tc_assign_color_b,
124        tc_test_image_cnt   => $tc_test_image_cnt,
125    };
126
127    return bless $self, $class;
128}
129
130sub info {
131    my $self = shift;
132
133    my $lang = $self->lang;
134    $lang = "??" if $lang eq "<unknown>";
135
136    if ( $self->is_ripped ) {
137        my $cnt = $self->ripped_images_cnt;
138        my $images = __x( "{cnt} images", cnt => $cnt );
139        return sprintf( "%02d - %s - $images", $self->id, $lang, $cnt );
140    }
141    else {
142        return sprintf( "%02d - %s", $self->id, $lang );
143    }
144}
145
146sub vobsub_prefix {
147    my $self      = shift;
148    my %par       = @_;
149    my ($file_nr) = @par{'file_nr'};
150
151    my $title = $self->title;
152
153    my $file = "";
154    if ( defined $file_nr ) {
155        if ( $self->title->is_ogg ) {
156            $file = sprintf( "%06d-", $file_nr + 1 );
157        }
158        else {
159            $file = sprintf( "%04d-", $file_nr );
160        }
161    }
162
163    return sprintf( "%s-%03d-${file}sid%02d",
164        $title->project->name, $title->nr, $self->id );
165}
166
167sub preview_dir {
168    my $self = shift;
169
170    return $self->title->get_subtitle_preview_dir( $self->id );
171}
172
173sub ifo_file {
174    my $self = shift;
175    my %par  = @_;
176    my ($nr) = @par{'nr'};
177
178    $nr ||= 0;
179
180    my @ifo_files = glob( $self->title->project->ifo_dir . "/*" );
181    $nr = 0 if $nr > @ifo_files - 1;
182
183    return $ifo_files[$nr];
184}
185
186sub ps1_file {
187    my $self = shift;
188
189    return $self->title->project->snap_dir . "/"
190        . $self->vobsub_prefix . ".ps1";
191}
192
193sub vobsub_file_exists {
194    my $self = shift;
195
196    my $mask
197        = $self->title->avi_dir . "/" . $self->vobsub_prefix . ".{sub,rar}";
198    my @files = glob($mask);
199
200    return scalar(@files);
201}
202
203sub is_ripped {
204    my $self = shift;
205
206    return -f $self->preview_dir . "/.ripped";
207}
208
209sub get_first_entry {
210    my $self = shift;
211
212    my $srtx = Video::DVDRip::SrtxFile->new;
213    $srtx->set_filename_from_dir ($self->preview_dir);
214
215    $srtx->open;
216    my $entry = $srtx->read_entry;
217    $srtx->close;
218
219    return $entry;
220}
221
222sub get_nth_entry {
223    my $self = shift;
224    my ($nr) = @_;
225
226    my $srtx = Video::DVDRip::SrtxFile->new;
227    $srtx->set_filename_from_dir ($self->preview_dir);
228
229    $srtx->open;
230    my $entry;
231    while ( $entry = $srtx->read_entry ) {
232        --$nr;
233        last if $nr == 0;
234    }
235    $srtx->close;
236
237    return if $nr != 0;
238    return $entry;
239}
240
241package Video::DVDRip::Subtitle::PreviewImage;
242use Locale::TextDomain qw (video.dvdrip);
243
244sub nr       { shift->{nr} }
245sub filename { shift->{filename} }
246sub time     { shift->{time} }
247sub height   { shift->{height} }
248sub width    { shift->{width} }
249
250sub new {
251    my $class = shift;
252    my %par   = @_;
253    my ( $filename, $time, $nr ) = @par{ 'filename', 'time', 'nr' };
254
255    my $catch = qx[identify -ping $filename 2>&1];
256    my ( $width, $height );
257    ( $width, $height ) = ( $catch =~ /(\d+)x(\d+)/ );
258
259    my $self = {
260        nr       => $nr,
261        filename => $filename,
262        time     => $time,
263        height   => $height,
264        width    => $width,
265    };
266
267    return bless $self, $class;
268}
269
2701;
271