Last active
October 24, 2023 01:05
-
-
Save shehbajdhillon/abf214112c9d6f535faffaebd6dd2d75 to your computer and use it in GitHub Desktop.
Layer Video Segment with Dubbed Audio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Previous Code (Unchanged) | |
| import subprocess | |
| def get_media_file_length(file_path): | |
| # The following ffprobe (part of ffmpeg) command will help us get the duration of a media file. | |
| ffmpeg_cmd = \ | |
| f"ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 file:{file_path}" | |
| cmd_tokens = ffmpeg_cmd.split(" ") | |
| output = subprocess.run(cmd_tokens, text=True, capture_output=True, check=True) | |
| return float(output.stdout.strip()) | |
| def layer_video_audio(video_segment_path, audio_file_path): | |
| video_length = get_media_file_length(video_segment_path) | |
| audio_length = get_media_file_length(audio_file_path) | |
| # Calculate the stretching ratio for the audio file so that it fits over the video segment properly. | |
| audio_stretching_ratio = round(audio_length / video_length, 3) | |
| stretched_audio_file_path = get_stretched_audio_file_path(audio_file_path) | |
| dubbed_video_segment_path = get_dubbed_video_segment_path(video_segment_path) | |
| # Ffmpeg command to stretch audio file | |
| audio_stretch_cmd = \ | |
| f"ffmpeg -i file:{audio_file_path} -filter:a 'atempo={audio_stretching_ratio}' file:{stretched_audio_file_path}" | |
| os.system(audio_stretch_cmd) | |
| # Ffmpeg command to layer over an audio file over a video file | |
| layer_elements_cmd = \ | |
| f"ffmpeg -i file:{video_segment_path} -i file:{stretched_audio_file_path} -c:v copy -map 0:v:0 -map 1:a:0 file:{dubbed_video_segment_path}" | |
| os.system(layer_elements_cmd) | |
| delete_files([stretched_audio_file_path]) | |
| return dubbed_video_segment_path | |
| def main(): | |
| file_path, input_language = sys.argv[1:] | |
| input_language = str(input_language).lower() | |
| transcript = transcribe_audio(file_path=file_path) | |
| segments = transcript.segments | |
| # Cut out the first segment from the input video for voice cloning | |
| initial_segment = segments[0] | |
| initial_segment_path = get_video_segment_path(file_path, initial_segment) | |
| cut_segment(file_path, initial_segment.start, initial_segment.end, initial_segment_path) | |
| voice_object = clone_voice(initial_segment_path) | |
| delete_files([initial_segment_path]) | |
| for segment in segments: | |
| translated_text = translate_segment(segment.text, input_language) | |
| audio_file_path = get_audio_file_path(file_path, segment, input_language) | |
| generate_speech(translated_text, voice_object, audio_file_path) | |
| video_segment_path = get_video_segment_path(file_path, segment) | |
| cut_segment(file_path, segment.start, segment.end, video_segment_path) | |
| dubbed_video_segment = layer_video_audio(video_segment_path, audio_file_path) | |
| delete_files([audio_file_path, video_segment_path]) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment