def addLeadInMessage(inputPath, outputPath): global mp3FileIndex if not os.path.exists(inputPath): fail('Input does not exist: ' + os.path.abspath(inputPath)) if os.path.isdir(inputPath): if os.path.exists(outputPath): if not os.path.isdir(outputPath): fail('Input is a directory, but output isn\'t: ' + os.path.abspath(outputPath)) elif not args.dry_run: os.mkdir(outputPath) mp3FileIndex = 0 for child in sorted(os.listdir(inputPath)): addLeadInMessage(os.path.join(inputPath, child), os.path.join(outputPath, child)) return inputFileNameSplit = os.path.splitext(os.path.basename(inputPath)) inputFileName = inputFileNameSplit[0] inputFileExt = inputFileNameSplit[1].lower() if inputFileExt != '.mp3': print('Ignoring {} (no mp3 file)'.format(os.path.abspath(inputPath))) return if args.add_numbering: outputPathSplit = os.path.split(outputPath) outputPath = os.path.join( outputPathSplit[0], '{:0>3}_{}'.format(mp3FileIndex + 1, outputPathSplit[1])) mp3FileIndex += 1 if os.path.isfile(outputPath): print('Skipping {} (file already exists)'.format( os.path.abspath(outputPath))) return text = re.sub(fileRegex, titlePattern, inputFileName).replace('_', ' ').strip() print('Adding lead-in "{}" to {}'.format(text, os.path.abspath(outputPath))) if not args.dry_run: tempLeadInFile = 'temp-lead-in.mp3' tempLeadInFileAdjusted = 'temp-lead-in_adjusted.mp3' text_to_speech.textToSpeechUsingArgs(text=text, targetFile=tempLeadInFile, args=args) # Adjust sample rate and mono/stereo print('Detecting sample rate and channels') detectionInfo = detectAudioData(inputPath) if detectionInfo is None: # We can't adjust print( 'Detecting sample rate and channels failed -> Skipping adjustment' ) tempLeadInFileAdjusted = tempLeadInFile else: print('Adjust sample rate to {} and channels to {}'.format( detectionInfo['sampleRate'], detectionInfo['channels'])) subprocess.call([ 'ffmpeg', '-i', tempLeadInFile, '-vn', '-ar', detectionInfo['sampleRate'], '-ac', detectionInfo['channels'], tempLeadInFileAdjusted ]) print('Concat') subprocess.call([ 'ffmpeg', '-i', 'concat:{}|{}'.format(tempLeadInFileAdjusted, inputPath), '-acodec', 'copy', outputPath, '-map_metadata', '0:1' ]) os.remove(tempLeadInFile) os.remove(tempLeadInFileAdjusted) print('\n')
targetDir = args.output if os.path.isdir(targetDir): print("Directory `" + targetDir + "` already exists.") exit(1) else: os.mkdir(targetDir) os.mkdir(targetDir + '/advert') os.mkdir(targetDir + '/mp3') if not args.skip_numbers: for i in range(1, 256): targetFile1 = '{}/mp3/{:0>4}.mp3'.format(targetDir, i) targetFile2 = '{}/advert/{:0>4}.mp3'.format(targetDir, i) text_to_speech.textToSpeechUsingArgs(text='{}'.format(i), targetFile=targetFile1, args=args) shutil.copy(targetFile1, targetFile2) with open(audioMessagesFile) as f: lineRe = re.compile('^([^|]+)\\|(.*)$') for line in f: match = lineRe.match(line.strip()) if match: fileName = match.group(1) text = match.group(2) text_to_speech.textToSpeechUsingArgs(text=text, targetFile=targetDir + '/mp3/' + fileName, args=args)