def _encode_srt_from_meta(self, m): """ Always output an srt (even if it contains no subtitles) This is required for the importer to verify the processed fileset is complete """ target_file = m.processed_files["srt"] if target_file.exists: log.debug("Processed srt exists - no parsing required") return True subtitles = [] source_file_absolute = m.source_files["sub"].get("absolute") if not source_file_absolute: log.warning("No sub file listed in source set. {}".format(m.name)) elif not os.path.exists(source_file_absolute): log.warning("The source file to extract subs from does not exist {}".format(source_file_absolute)) else: subtitles = subtitle_processor.parse_subtitles(filename=source_file_absolute) if not subtitles: log.warning("No subtiles parsed from sub file {}".format(source_file_absolute)) with tempfile.TemporaryDirectory() as tempdir: srt_file = os.path.join(tempdir, "subs.srt") with open(srt_file, "w", encoding="utf-8") as subfile: subfile.write(subtitle_processor.create_srt(subtitles)) target_file.move(srt_file) return True
def _encode_srt_from_meta(self, m): """ Always output an srt (even if it contains no subtitles) This is required for the importer to verify the processed fileset is complete """ target_file = m.processed_files['srt'] if target_file.exists: log.debug('Processed srt exists - no parsing required') return True subtitles = [] source_file_absolute = m.source_files['sub'].get('absolute') if not source_file_absolute: log.warning('No sub file listed in source set. {}'.format(m.name)) elif not os.path.exists(source_file_absolute): log.warning( 'The source file to extract subs from does not exist {}'. format(source_file_absolute)) else: subtitles = subtitle_processor.parse_subtitles( filename=source_file_absolute) if not subtitles: log.warning('No subtiles parsed from sub file {}'.format( source_file_absolute)) with tempfile.TemporaryDirectory() as tempdir: srt_file = os.path.join(tempdir, 'subs.srt') with open(srt_file, 'w', encoding='utf-8') as subfile: subfile.write(subtitle_processor.create_srt(subtitles)) target_file.move(srt_file) return True
def _get_lyrics(): processed_file_srt = m.processed_files.get('srt') if not processed_file_srt or not processed_file_srt.exists: log.warning('srt file missing unable to import lyrics') return return "\n".join(subtitle.text for subtitle in subtitle_processor.parse_subtitles( filename=processed_file_srt.absolute))
def _add_lyrics(self, track, processed_file_srt): if not processed_file_srt or not processed_file_srt.exists: log.warning('srt file missing unable to import lyrics') return subtitles = subtitle_processor.parse_subtitles(filename=processed_file_srt.absolute) track.lyrics = "\n".join(subtitle.text for subtitle in subtitles)
def _encode_primary_video_from_meta(self, m): target_file = m.processed_files["video"] if target_file.exists: log.debug("Processed Destination was created with the same input sources - no encoding required") return True extract_source_details_status = self._extract_source_details_safeguard(m, force=True) if not extract_source_details_status: return False with tempfile.TemporaryDirectory() as tempdir: # 2.) Convert souce formats into appropriate formats for video encoding absolute_video_to_encode = m.source_files["video"].get("absolute") # 2.a) Convert Image to Video if m.source_files["image"] and not absolute_video_to_encode: absolute_video_to_encode = os.path.join(tempdir, "image.mp4") external_tools.encode_image_to_video( source=m.source_files["image"]["absolute"], destination=absolute_video_to_encode, **m.source_details ) if not absolute_video_to_encode: log.error("Unable to encode as no video was provided {}".format(absolute_video_to_encode)) return False if not os.path.exists(absolute_video_to_encode): log.error("Video to encode does not exist {}".format(absolute_video_to_encode)) return False # 2.b) Read + normalize subtitle file absolute_ssa_to_encode = None if m.source_files["sub"]: # Parse input subtitles absolute_subtitle = m.source_files["sub"]["absolute"] if not os.path.exists(absolute_subtitle): log.error("Subtitles to encode does not exist {}".format(absolute_subtitle)) return False subtitles = subtitle_processor.parse_subtitles(filename=absolute_subtitle) if not subtitles: log.error( "Subtitle file explicity given, but was unable to parse any subtitles from it. " "There may be an issue with parsing. " "A Common cause is SSA files that have subtitles aligned at top are ignored. " "{}".format(m.source_files["sub"]["absolute"]) ) return False # Output styled subtiles as SSA absolute_ssa_to_encode = os.path.join(tempdir, "subs.ssa") with open(absolute_ssa_to_encode, "w", encoding="utf-8") as subfile: subfile.write( subtitle_processor.create_ssa( subtitles, width=m.source_details["width"], height=m.source_details["height"] ) ) # 3.) Encode encode_steps = ( # 3.a) Render audio and normalize lambda: external_tools.encode_audio( source=m.source_files["audio"].get("absolute") or m.source_files["video"].get("absolute"), destination=os.path.join(tempdir, "audio.wav"), ), # 3.b) Render video with subtitles and mux new audio. lambda: external_tools.encode_video( video_source=absolute_video_to_encode, audio_source=os.path.join(tempdir, "audio.wav"), subtitle_source=absolute_ssa_to_encode, destination=os.path.join(tempdir, "video.mp4"), ), ) for encode_step in encode_steps: encode_success, cmd_result = encode_step() if not encode_success: if self.pdb: import pdb pdb.set_trace() log.error(cmd_result) return False # 4.) Move the newly encoded file to the target path target_file.move(os.path.join(tempdir, "video.mp4")) return True
def _encode_primary_video_from_meta(self, m): target_file = m.processed_files['video'] if target_file.exists: log.debug( 'Processed Destination was created with the same input sources - no encoding required' ) return True extract_source_details_status = self._extract_source_details_safeguard( m, force=True) if not extract_source_details_status: return False with tempfile.TemporaryDirectory() as tempdir: # 2.) Convert souce formats into appropriate formats for video encoding absolute_video_to_encode = m.source_files['video'].get('absolute') # 2.a) Convert Image to Video if m.source_files['image'] and not absolute_video_to_encode: absolute_video_to_encode = os.path.join(tempdir, 'image.mp4') external_tools.encode_image_to_video( source=m.source_files['image']['absolute'], destination=absolute_video_to_encode, **m.source_details) if not absolute_video_to_encode: log.error( 'Unable to encode as no video was provided {}'.format( absolute_video_to_encode)) return False if not os.path.exists(absolute_video_to_encode): log.error('Video to encode does not exist {}'.format( absolute_video_to_encode)) return False # 2.b) Read + normalize subtitle file absolute_ssa_to_encode = None if m.source_files['sub']: # Parse input subtitles absolute_subtitle = m.source_files['sub']['absolute'] if not os.path.exists(absolute_subtitle): log.error('Subtitles to encode does not exist {}'.format( absolute_subtitle)) return False subtitles = subtitle_processor.parse_subtitles( filename=absolute_subtitle) if not subtitles: log.error( 'Subtitle file explicity given, but was unable to parse any subtitles from it. ' 'There may be an issue with parsing. ' 'A Common cause is SSA files that have subtitles aligned at top are ignored. ' '{}'.format(m.source_files['sub']['absolute'])) return False # Output styled subtiles as SSA absolute_ssa_to_encode = os.path.join(tempdir, 'subs.ssa') with open(absolute_ssa_to_encode, 'w', encoding='utf-8') as subfile: subfile.write( subtitle_processor.create_ssa( subtitles, width=m.source_details['width'], height=m.source_details['height'])) # 3.) Encode encode_steps = ( # 3.a) Render audio and normalize lambda: external_tools.encode_audio( source=m.source_files['audio'].get('absolute') or m. source_files['video'].get('absolute'), destination=os.path.join(tempdir, 'audio.wav'), ), # 3.b) Render video with subtitles and mux new audio. lambda: external_tools.encode_video( video_source=absolute_video_to_encode, audio_source=os.path.join(tempdir, 'audio.wav'), subtitle_source=absolute_ssa_to_encode, destination=os.path.join(tempdir, 'video.mp4'), ), ) for encode_step in encode_steps: encode_success, cmd_result = encode_step() if not encode_success: if self.pdb: import pdb pdb.set_trace() log.error(cmd_result) return False # 4.) Move the newly encoded file to the target path target_file.move(os.path.join(tempdir, 'video.mp4')) return True
def _get_lyrics(): processed_file_srt = m.processed_files.get('srt') if not processed_file_srt or not processed_file_srt.exists: log.warning('srt file missing unable to import lyrics') return return "\n".join(subtitle.text for subtitle in subtitle_processor.parse_subtitles(filename=processed_file_srt.absolute))