Changeset 289 for indytube/trunk

Show
Ignore:
Timestamp:
11/26/07 10:54:01 (14 months ago)
Author:
andy
Message:

Bugfix the refactoring of indytube main script. Mainly this gives us a
simple method that can be called externally to transcode a file.

Moved the file name for configuration into a config.py script

Location:
indytube/trunk/indytube
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • indytube/trunk/indytube/indytube.py

    r249 r289  
    1616from twisted.internet import reactor 
    1717 
    18 CONF_FILE_DEFAULT = '/etc/indytube/indytube.conf' 
     18from config import CONF_FILE_DEFAULT 
    1919 
    2020class IndyTubeTranscoder(object): 
     
    101101                for f in files: 
    102102                        ok = self.attempt_transcode_file(f) 
    103                         self.checked = self.checked + 1 
    104103                        if ok: 
    105104                                self.converted = self.converted + 1 
     
    111110        ## end : do_transcoding_loop 
    112111 
    113 def attempt_transcode_file(self, f): 
     112   def attempt_transcode_file(self, f): 
    114113        '''Start the transcoding attempt with file 'f'. Converts to FLV format, and OGG/Theora. Produces HTML snippets for including 
    115114           the players appropriate for the produced video format. At the moment, this is FlowPlayer for FLV, and Cortado java applet for OGG/Theora ''' 
     
    120119        if extension.lower() in self.CONVERT_THESE:  #we should convert the file 
    121120 
    122                 relative_directory=root[len(self.VIDEO_FILE_DIRECTORY):] 
     121                #get last character, or first one 
     122                relative_directory=self.VIDEO_FILE_DIRECTORY[len(self.VIDEO_FILE_DIRECTORY):] 
    123123                if relative_directory.startswith(os.sep): 
    124124                        relative_directory=relative_directory[1:]  # make sure we are relative 
    125125 
    126                         videofile = os.path.join(root,f) 
    127                         lockfile = os.path.join(root,stem+".wetube_lock")  # we are encoding already 
    128                         skipfile = os.path.join(root,stem+".wetube_skip")  # we tried and failed, don't bother again 
    129                         flvfile  = os.path.join(self.FLV_FILE_DIRECTORY,relative_directory,stem+".flv") 
    130                         theorafile = os.path.join(self.FLV_FILE_DIRECTORY,relative_directory,stem+".ogg") 
    131                         includefile  = os.path.join(self.INCLUDE_FILE_DIRECTORY,relative_directory,stem+self.INCLUDE_FILE_SUFFIX) 
    132                         #logging.info("check for %s, %s, %s " % (lockfile, skipfile, flvfile)) 
    133  
    134                         #check that another encoder isnt already processing this file (lockfile) or that we havent tried and failed before (skipfile) 
    135                         if not(os.path.exists(lockfile) or os.path.exists(skipfile)): 
    136                                 #OK, valid video file ready to try to transcode 
    137                                 logging.debug("Checking file %s, using extension %s " % ( os.path.join(root,f), extension)) 
    138                                 try: 
    139                                         # touch the lock file 
    140                                         os.mknod(lockfile) 
    141  
    142                                         # if the flv file (autogenerated) or html snippet is not there, then reencode! 
    143                                         if not(os.path.exists(flvfile)) or not(os.path.exists(includefile)): 
    144                                                 logging.info('OK to try encoding: '+videofile) 
     126                root = self.VIDEO_FILE_DIRECTORY 
     127                videofile = os.path.join(root,f) 
     128                lockfile = os.path.join(root,stem+".wetube_lock")  # we are encoding already 
     129                skipfile = os.path.join(root,stem+".wetube_skip")  # we tried and failed, don't bother again 
     130                flvfile  = os.path.join(self.FLV_FILE_DIRECTORY,relative_directory,stem+".flv") 
     131                theorafile = os.path.join(self.FLV_FILE_DIRECTORY,relative_directory,stem+".ogg") 
     132                includefile  = os.path.join(self.INCLUDE_FILE_DIRECTORY,relative_directory,stem+self.INCLUDE_FILE_SUFFIX) 
     133                logging.info("check for %s, %s, %s " % (lockfile, skipfile, flvfile)) 
     134 
     135                #check that another encoder isnt already processing this file (lockfile) or that we havent tried and failed before (skipfile) 
     136                if not(os.path.exists(lockfile) or os.path.exists(skipfile)): 
     137                        #OK, valid video file ready to try to transcode 
     138                        logging.info("Checking file %s, using extension %s " % ( os.path.join(root,f), extension)) 
     139                        self.checked = self.checked + 1 
     140                        try: 
     141                                # touch the lock file 
     142                                os.mknod(lockfile) 
     143 
     144                                # if the flv file (autogenerated) or html snippet is not there, then reencode! 
     145                                if not(os.path.exists(flvfile)) or not(os.path.exists(includefile)): 
     146                                        logging.info('OK to try encoding: '+videofile) 
    145147                                                         
    146                                                 #pipe_to_null = '> /dev/null 2>&1' 
    147                                                 if self.DO_ENCODING: #maybe we just want to regenerate the include file! 
    148                                                         #mencoder flv conversion 
    149                                                         start_time=time.time() 
    150                                                         encoder_command = self.MENCODER_LOCATION + " -quiet " + videofile + " -o " + flvfile + " " + self.MENCODER_OPTIONS 
    151                                                         os.system('nice -n '+self.BE_HOW_NICE+' '+encoder_command) 
    152                                                         finish_time=time.time() 
    153                                                         logging.info("Encoded %s in %.2f seconds, using cmd -- %s" % (videofile,finish_time-start_time,encoder_command)) 
    154                                                         flvtool_command = self.FLVTOOL_LOCATION+" -U stdin "+flvfile 
    155                                                         os.system("cat "+ flvfile +" | "+ 'nice -n '+ self.BE_HOW_NICE+' '+flvtool_command)  
    156  
    157                                                         #ffmpeg2theora , theora/ogg conversion 
    158                                                         start_time=time.time()   
    159                                                         theora_cmd =  self.FFMPEG2THEORA_COMMAND + ' ' + videofile + " -o " + theorafile 
    160                                                         os.system('nice -n '+ self.BE_HOW_NICE+' '+ theora_cmd) 
    161                                                         finish_time=time.time() 
    162                                                         logging.info("Encoded %s in %.2f seconds, using cmd -- %s" % (videofile,finish_time-start_time,theora_cmd)) 
    163  
    164                                                 else: 
    165                                                         logging.debug("skipped encoding, will just do html template generation, if flv exists as non-zero size") 
     148                                        #pipe_to_null = '> /dev/null 2>&1' 
     149                                        if self.DO_ENCODING: #maybe we just want to regenerate the include file! 
     150                                                #mencoder flv conversion 
     151                                                start_time=time.time() 
     152                                                encoder_command = self.MENCODER_LOCATION + " -quiet " + videofile + " -o " + flvfile + " " + self.MENCODER_OPTIONS 
     153                                                os.system('nice -n '+self.BE_HOW_NICE+' '+encoder_command) 
     154                                                finish_time=time.time() 
     155                                                logging.info("Encoded %s in %.2f seconds, using cmd -- %s" % (videofile,finish_time-start_time,encoder_command)) 
     156                                                flvtool_command = self.FLVTOOL_LOCATION+" -U stdin "+flvfile 
     157                                                os.system("cat "+ flvfile +" | "+ 'nice -n '+ self.BE_HOW_NICE+' '+flvtool_command)  
     158 
     159                                                #ffmpeg2theora , theora/ogg conversion 
     160                                                start_time=time.time()   
     161                                                theora_cmd =  self.FFMPEG2THEORA_COMMAND + ' ' + videofile + " -o " + theorafile 
     162                                                os.system('nice -n '+ self.BE_HOW_NICE+' '+ theora_cmd) 
     163                                                finish_time=time.time() 
     164                                                logging.info("Encoded %s in %.2f seconds, using cmd -- %s" % (videofile,finish_time-start_time,theora_cmd)) 
     165 
     166                                        else: 
     167                                                logging.debug("skipped encoding, will just do html template generation, if flv exists as non-zero size") 
    166168                     
    167                                         else: 
    168                                                 logging.debug("flv file and html file already exists, not doing transcoding") 
    169  
    170                                         #make the flash HTML snippet if the flv got created correctly. 
    171                                         #XXX todo, separate out the flv and ogg theora (java applet) html snippet 
    172                                         if os.path.exists(flvfile) and os.path.getsize(flvfile)>0: 
    173                                                 logging.info("Making html template - original size of %s: %.1fMB, Encoded size: %.1fMB" % (videofile,os.path.getsize(videofile)/1000000.0,os.path.getsize(flvfile)/1000000.0)) 
    174                                                 data_map={ 
    175                                                         'flowplayer_location':self.FLOWPLAYER_LOCATION,  
    176                                                         'videofile':relative_directory+'/'+stem+".flv",  
    177                                                         'videobaseurl':self.VIDEO_SERVER_URL,  
    178                                                         'splashbaseurl':self.SPLASH_IMAGE_BASE,  
    179                                                         'splashfile':self.SPLASH_IMAGE_FILE,  
    180                                                         'cortado_location':self.CORTADO_LOCATION,  
    181                                                         'oggfile':stem+".ogg",  
    182                                                         'mirid':stem 
    183                                                         } 
    184                                                 t = Template(file=self.INCLUDE_TEMPLATE, searchList=[data_map])   
    185                                                 f=open(includefile, 'w') 
    186                                                 f.write(t.respond()) 
    187                                                 f.close() 
    188  
    189                                                 # OK ! It Worked. 
    190                                                 worked = True 
    191  
    192                                         else: 
    193                                                 logging.info("FLV file size is zero - assuming encoding failed! Permanently skipping file!") 
    194                                                 os.mknod(skipfile) 
     169                                else: 
     170                                        logging.debug("flv file and html file already exists, not doing transcoding") 
     171 
     172                                #make the flash HTML snippet if the flv got created correctly. 
     173                                #XXX todo, separate out the flv and ogg theora (java applet) html snippet 
     174                                if os.path.exists(flvfile) and os.path.getsize(flvfile)>0: 
     175                                        logging.info("Making html template - original size of %s: %.1fMB, Encoded size: %.1fMB" % (videofile,os.path.getsize(videofile)/1000000.0,os.path.getsize(flvfile)/1000000.0)) 
     176                                        data_map={ 
     177                                                'flowplayer_location':self.FLOWPLAYER_LOCATION,  
     178                                                'videofile':relative_directory+'/'+stem+".flv",  
     179                                                'videobaseurl':self.VIDEO_SERVER_URL,  
     180                                                'splashbaseurl':self.SPLASH_IMAGE_BASE,  
     181                                                'splashfile':self.SPLASH_IMAGE_FILE,  
     182                                                'cortado_location':self.CORTADO_LOCATION,  
     183                                                'oggfile':stem+".ogg",  
     184                                                'mirid':stem 
     185                                                } 
     186                                        t = Template(file=self.INCLUDE_TEMPLATE, searchList=[data_map])   
     187                                        f=open(includefile, 'w') 
     188                                        f.write(t.respond()) 
     189                                        f.close() 
     190 
     191                                        # OK ! It Worked. 
     192                                        worked = True 
     193 
     194                                else: 
     195                                        logging.info("FLV file size is zero - assuming encoding failed! Permanently skipping file!") 
     196                                        os.mknod(skipfile) 
    195197                        
    196                                         #finished transcoding block , remove lock file  
    197                                         os.remove(lockfile) 
    198  
    199                                 except: 
    200                                         logging.info("Error while processing %s: %s" % (videofile,traceback.format_exc())) 
    201                                         os.remove(lockfile) 
    202                                         #remove this process's lockfile, this exception will stop the entire process 
    203                                         os.remove(self.ENCODER_LOCKFILE) 
    204  
    205                         else: 
    206                                 logging.debug(' lock file or skip file present for file %s ' % videofile) 
    207          
     198                                #finished transcoding block , remove lock file  
     199                                os.remove(lockfile) 
     200 
     201                        except: 
     202                                logging.info("Error while processing %s: %s" % (videofile,traceback.format_exc())) 
     203                                os.remove(lockfile) 
     204 
     205                else: 
     206                        logging.debug(' lock file or skip file present for file %s ' % videofile) 
     207 
    208208        #return a status value 
    209209        return worked 
     
    241241 
    242242    #install a signal handler to re-read configuration files. 
     243    # eg kill -USR2 <pid> 
    243244    signal.signal(signal.SIGUSR2, indytuber.rereadConfig) 
    244245