1 | #if 0 /* |
---|
2 | # ----------------------------------------------------------------------- |
---|
3 | # dvdinfo.py - parse dvd title structure |
---|
4 | # ----------------------------------------------------------------------- |
---|
5 | # $Id: dvdinfo.py,v 1.18 2005/01/13 20:20:20 dischi Exp $ |
---|
6 | # |
---|
7 | # ----------------------------------------------------------------------- |
---|
8 | # Copyright (C) 2003 Thomas Schueppel, Dirk Meyer |
---|
9 | # |
---|
10 | # This program is free software; you can redistribute it and/or modify |
---|
11 | # it under the terms of the GNU General Public License as published by |
---|
12 | # the Free Software Foundation; either version 2 of the License, or |
---|
13 | # (at your option) any later version. |
---|
14 | # |
---|
15 | # This program is distributed in the hope that it will be useful, but |
---|
16 | # WITHOUT ANY WARRANTY; without even the implied warranty of MER- |
---|
17 | # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
18 | # Public License for more details. |
---|
19 | # |
---|
20 | # You should have received a copy of the GNU General Public License along |
---|
21 | # with this program; if not, write to the Free Software Foundation, Inc., |
---|
22 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
23 | # |
---|
24 | # ----------------------------------------------------------------------- */ |
---|
25 | #endif |
---|
26 | |
---|
27 | |
---|
28 | import os |
---|
29 | import ifoparser |
---|
30 | from mmpython import mediainfo |
---|
31 | import mmpython |
---|
32 | from discinfo import DiscInfo |
---|
33 | |
---|
34 | class DVDAudio(mediainfo.AudioInfo): |
---|
35 | def __init__(self, title, number): |
---|
36 | mediainfo.AudioInfo.__init__(self) |
---|
37 | self.number = number |
---|
38 | self.title = title |
---|
39 | self.id, self.language, self.codec, self.channels, self.samplerate = \ |
---|
40 | ifoparser.audio(title, number) |
---|
41 | |
---|
42 | |
---|
43 | class DVDTitle(mediainfo.AVInfo): |
---|
44 | def __init__(self, number): |
---|
45 | mediainfo.AVInfo.__init__(self) |
---|
46 | self.number = number |
---|
47 | self.chapters, self.angles, self.length, audio_num, \ |
---|
48 | subtitles_num = ifoparser.title(number) |
---|
49 | |
---|
50 | self.keys.append('chapters') |
---|
51 | self.keys.append('subtitles') |
---|
52 | |
---|
53 | self.mime = 'video/mpeg' |
---|
54 | for a in range(1, audio_num+1): |
---|
55 | self.audio.append(DVDAudio(number, a)) |
---|
56 | |
---|
57 | for s in range(1, subtitles_num+1): |
---|
58 | self.subtitles.append(ifoparser.subtitle(number, s)[0]) |
---|
59 | |
---|
60 | |
---|
61 | class DVDInfo(DiscInfo): |
---|
62 | def __init__(self, device): |
---|
63 | DiscInfo.__init__(self) |
---|
64 | self.context = 'video' |
---|
65 | self.offset = 0 |
---|
66 | |
---|
67 | if mediainfo.DEBUG > 1: |
---|
68 | print 'trying buggy dvd detection' |
---|
69 | |
---|
70 | if isinstance(device, file): |
---|
71 | self.valid = self.isDVDiso(device) |
---|
72 | elif os.path.isdir(device): |
---|
73 | self.valid = self.isDVDdir(device) |
---|
74 | else: |
---|
75 | self.valid = self.isDisc(device) |
---|
76 | |
---|
77 | if self.valid and self.tracks: |
---|
78 | self.keys.append('length') |
---|
79 | self.length = 0 |
---|
80 | first = 0 |
---|
81 | |
---|
82 | for t in self.tracks: |
---|
83 | self.length += t.length |
---|
84 | if not first: |
---|
85 | first = t.length |
---|
86 | |
---|
87 | if self.length/len(self.tracks) == first: |
---|
88 | # badly mastered dvd |
---|
89 | self.length = first |
---|
90 | |
---|
91 | self.mime = 'video/dvd' |
---|
92 | self.type = 'DVD' |
---|
93 | self.subtype = 'video' |
---|
94 | |
---|
95 | |
---|
96 | def isDVDdir(self, dirname): |
---|
97 | if not (os.path.isdir(dirname+'/VIDEO_TS') or \ |
---|
98 | os.path.isdir(dirname+'/video_ts') or \ |
---|
99 | os.path.isdir(dirname+'/Video_ts')): |
---|
100 | return 0 |
---|
101 | |
---|
102 | # OK, try libdvdread |
---|
103 | title_num = ifoparser.open(dirname) |
---|
104 | if not title_num: |
---|
105 | return 0 |
---|
106 | |
---|
107 | for title in range(1, title_num+1): |
---|
108 | ti = DVDTitle(title) |
---|
109 | ti.trackno = title |
---|
110 | ti.trackof = title_num |
---|
111 | self.appendtrack(ti) |
---|
112 | |
---|
113 | ifoparser.close() |
---|
114 | return 1 |
---|
115 | |
---|
116 | |
---|
117 | def isDisc(self, device): |
---|
118 | if DiscInfo.isDisc(self, device) != 2: |
---|
119 | return 0 |
---|
120 | |
---|
121 | # brute force reading of the device to find out if it is a DVD |
---|
122 | f = open(device,'rb') |
---|
123 | f.seek(32768, 0) |
---|
124 | buffer = f.read(60000) |
---|
125 | |
---|
126 | if buffer.find('UDF') == -1: |
---|
127 | f.close() |
---|
128 | return 0 |
---|
129 | |
---|
130 | # seems to be a DVD, read a little bit more |
---|
131 | buffer += f.read(550000) |
---|
132 | f.close() |
---|
133 | |
---|
134 | if buffer.find('VIDEO_TS') == -1 and buffer.find('VIDEO_TS.IFO') == -1 and \ |
---|
135 | buffer.find('OSTA UDF Compliant') == -1: |
---|
136 | return 0 |
---|
137 | |
---|
138 | # OK, try libdvdread |
---|
139 | title_num = ifoparser.open(device) |
---|
140 | |
---|
141 | if not title_num: |
---|
142 | return 0 |
---|
143 | |
---|
144 | for title in range(1, title_num+1): |
---|
145 | ti = DVDTitle(title) |
---|
146 | ti.trackno = title |
---|
147 | ti.trackof = title_num |
---|
148 | self.appendtrack(ti) |
---|
149 | |
---|
150 | ifoparser.close() |
---|
151 | return 1 |
---|
152 | |
---|
153 | |
---|
154 | def isDVDiso(self, f): |
---|
155 | # brute force reading of the device to find out if it is a DVD |
---|
156 | f.seek(32768, 0) |
---|
157 | buffer = f.read(60000) |
---|
158 | |
---|
159 | if buffer.find('UDF') == -1: |
---|
160 | return 0 |
---|
161 | |
---|
162 | # seems to be a DVD, read a little bit more |
---|
163 | buffer += f.read(550000) |
---|
164 | |
---|
165 | if buffer.find('VIDEO_TS') == -1 and buffer.find('VIDEO_TS.IFO') == -1 and \ |
---|
166 | buffer.find('OSTA UDF Compliant') == -1: |
---|
167 | return 0 |
---|
168 | |
---|
169 | # OK, try libdvdread |
---|
170 | title_num = ifoparser.open(f.name) |
---|
171 | |
---|
172 | if not title_num: |
---|
173 | return 0 |
---|
174 | |
---|
175 | for title in range(1, title_num+1): |
---|
176 | ti = DVDTitle(title) |
---|
177 | ti.trackno = title |
---|
178 | ti.trackof = title_num |
---|
179 | self.appendtrack(ti) |
---|
180 | |
---|
181 | ifoparser.close() |
---|
182 | return 1 |
---|
183 | |
---|
184 | |
---|
185 | if not mmpython.gettype('video/dvd', mediainfo.EXTENSION_DEVICE): |
---|
186 | mmpython.registertype( 'video/dvd', mediainfo.EXTENSION_DEVICE, |
---|
187 | mediainfo.TYPE_AV, DVDInfo ) |
---|
188 | |
---|
189 | if not mmpython.gettype('video/dvd', mediainfo.EXTENSION_DIRECTORY): |
---|
190 | mmpython.registertype('video/dvd', mediainfo.EXTENSION_DIRECTORY, |
---|
191 | mediainfo.TYPE_AV, DVDInfo) |
---|
192 | |
---|
193 | mmpython.registertype('video/dvd', ['iso'], mediainfo.TYPE_AV, DVDInfo) |
---|