# Copyright (C) 2024 Jason Anderson, Lunatixz # # # This file is part of PseudoTV Live. # # PseudoTV Live is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # PseudoTV Live is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with PseudoTV Live. If not, see . from globals import * class AVIChunk: def __init__(self): self.empty() def empty(self): self.size = 0 self.fourcc = '' self.datatype = 1 self.chunk = '' def read(self, thefile): data = thefile.readBytes(4) try: self.size = struct.unpack(' 0 and self.size < 10000: self.chunk = thefile.readBytes(self.size) else: self.chunk = '' self.size = 0 class AVIList: def __init__(self): self.empty() def empty(self): self.size = 0 self.fourcc = '' self.datatype = 2 def read(self, thefile): data = thefile.readBytes(4) self.size = struct.unpack(' int and float: log("AVIParser: determineLength %s"%filename) try: self.File = FileAccess.open(filename, "rb", None) except: log("AVIParser: Unable to open the file") return 0 dur = int(self.readHeader()) self.File.close() log('AVIParser: Duration is %s'%(dur)) return dur def readHeader(self): # AVI Chunk data = self.getChunkOrList() if data.datatype != 2: log("AVIParser: Not an avi") return 0 #todo fix if data.fourcc[0:4] != "AVI ": log("AVIParser: Wrong FourCC") return 0 # Header List data = self.getChunkOrList() if data.fourcc != "hdrl": log("AVIParser: Header not found") return 0 # Header chunk data = self.getChunkOrList() if data.fourcc != 'avih': log('Header chunk not found') return 0 self.parseHeader(data) # Stream list data = self.getChunkOrList() if self.Header.dwStreams > 10: self.Header.dwStreams = 10 for i in range(self.Header.dwStreams): if data.datatype != 2: log("AVIParser: Unable to find streams") return 0 listsize = data.size # Stream chunk number 1, the stream header data = self.getChunkOrList() if data.datatype != 1: log("AVIParser: Broken stream header") return 0 self.StreamHeader.empty() self.parseStreamHeader(data) # If this is the video header, determine the duration if self.StreamHeader.fccType == 'vids': return self.getStreamDuration() # If this isn't the video header, skip through the rest of these # stream chunks try: if listsize - data.size - 12 > 0: self.File.seek(listsize - data.size - 12, 1) data = self.getChunkOrList() except: log("AVIParser: Unable to seek") log("AVIParser: Video stream not found") return 0 def getStreamDuration(self): try: return int(self.StreamHeader.dwLength / (float(self.StreamHeader.dwRate) / float(self.StreamHeader.dwScale))) except: return 0 def parseHeader(self, data): try: header = struct.unpack('