Example #1
0
    def get_duration(self, input_file):

        rval = 0
        logger_name = '{0}.get_duration'.format(__name__)
        logger = logging.getLogger(logger_name)

        exe = self.config_dict['recoder']['ffprobe_exe']
        out_dir = self.config_dict['recoder']['output_dir']

        executer = Executer()

        log_msg = "Getting info for: {0}".format(input_file)
        logger.debug(log_msg)

        com = ("{0} -i {1} -show_entries format=duration -v quiet"
        " -of csv='p=0'").format(exe, input_file)

        log_msg = "command: {0}".format(com)
        logger.debug(log_msg)
        try:
            rval, stdout, stderr = executer.exec_com(com)
        except Exception:
            log_msg = "Exception occurred attempting to ger duration of {0}"
            log_msg = log_msg.format(input_file)
            logging.exception(log_msg)
            rval = 1

        if rval == 0:
            log_msg = "Duration={0}".format(stdout)
            logger.debug(log_msg)
            rval = float(stdout)
        else:
            rval = 0

        return rval
Example #2
0
    def flv_to_mp3(self, file_list, increments, remove_recoded_files):

        rval = True
        logger_name = '{0}.flv_to_mp3'.format(__name__)
        logger = logging.getLogger(logger_name)

        file_name_regex = '(.*)\.flv$'
        file_name_pattern = re.compile(r'{0}'.format(file_name_regex))

        exe = self.config_dict['recoder']['ffmpeg_exe']
        out_dir = self.config_dict['recoder']['output_dir']

        executer = Executer()

        coded_file_count = 0
        failed_count = 0
        for flv_file in file_list:

            try:
                size = os.path.getsize(flv_file)
                if size > 0:

                    flv_file_name = os.path.basename(flv_file)
                    file_name_match = file_name_pattern.match(flv_file_name)
                    if file_name_match:
                        if file_name_match.group(1):
                            mp3_file_name_prefix = file_name_match.group(1)
                        else:
                            log_msg = ("Skipping file because failed to parse"
                            " the input file name: {0}".format(flv_file_name))
                            logger.error(log_msg)
                            continue

                    duration = self.get_duration(flv_file)

                    minutes = int(duration / 60)
                    if minutes <= 0:
                        minutes = 1

                    for position in range(0, minutes):
                        mp3_file_name = '{0}-{1}.mp3'.format(mp3_file_name_prefix,
                                                             str(position).zfill(3))
                        log_msg = "Creating: {0}".format(mp3_file_name)
                        logger.debug(log_msg)

                        mp3_path = os.path.join(out_dir, mp3_file_name)
                        com = "{0} -y -ss {3} -t {4} -i {1} {2}".format(exe,
                                                                flv_file,
                                                                mp3_path,
                                                                (position * 60),
                                                                increments)
                        log_msg = "command: {0}".format(com)
                        logger.debug(log_msg)

                        try:
                            com_result = executer.exec_com(com)

                            if com_result:
                                coded_file_count += 1
                            else:
                                failed_count += 1

                        except Exception:
                            log_msg = "Exception occurred attempting to recode {0}"
                            log_msg = log_msg.format(flv_file)
                            logging.exception(log_msg)
                            failed_count += 1

                    if remove_recoded_files:
                        if failed_count:
                            log_msg = ("not removing the flv file: {0}"
                                       .format(flv_file))
                            logger.info(log_msg)
                        else:
                            log_msg = ("removing the recoded file: {0}"
                                       .format(flv_file))
                            logger.info(log_msg)
                            os.remove(flv_file)

                else:
                    log_msg = "File {0} has no content so ignoring it for now".format(flv_file)
                    logger.debug(log_msg) 

            except Exception:
                log_msg = "Exception occurred attempting to get size of {0}"
                log_msg = log_msg.format(flv_file)
                logging.exception(log_msg)
                rval = False

        if failed_count:
            log_msg = ("{0} files coded successfully / "
                       "{1} failures"
                       .format(coded_file_count, failed_count))
            logger.warn(log_msg)
            rval = False
        else:
            log_msg = ("All {0} files recoded successfully"
                       .format(coded_file_count))
            logger.info(log_msg)


        return rval