Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
v1.9
Browse files Browse the repository at this point in the history
Add a tool who replaces mp4 Video file Audio with translation audio file.
  • Loading branch information
overcrash66 committed Dec 22, 2023
1 parent ee4ea9a commit dc65fb0
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
10 changes: 8 additions & 2 deletions AudioFileTranslator-S2ST.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# - T2T: Google Speech Recognizer
# - TTS: python gtts
#
# Version: 1.8
# Version: 1.9
#
##############################################################################################################################
"""
Expand Down Expand Up @@ -41,6 +41,11 @@
customtkinter.set_appearance_mode("System") # Modes: system (default), light, dark
customtkinter.set_default_color_theme("blue") # Themes: blue (default), dark-blue, green

def run_external_script():
script_path = 'ReplaceVideoAudio.py'
if script_path:
subprocess.run(['python', script_path])

def YouTubeDownloader():
def sanitize_filename(title):
# Replace non-alphanumeric characters with underscores
Expand Down Expand Up @@ -292,6 +297,7 @@ def __init__(self, master):
filedropdown.add_option(option="Convert Audio file to MP3", command=self.Convert_Audio_Files)
filedropdown.add_option(option="Extract audio from Video", command=self.extract_audio)
filedropdown.add_option(option="Youtube Downloader", command=YouTubeDownloader)
filedropdown.add_option(option="Replace Audio in Video", command=run_external_script)
filedropdown.add_option(option="Exit", command=master.destroy)

helpdropdown = CustomDropdownMenu(widget=self.help, width=50)
Expand Down Expand Up @@ -440,7 +446,7 @@ def Start(Input_file_path):
Start(Input_file_path)

def show_about(self):
messagebox.showinfo("About", "Audio File Translator - S2ST v1.8\n\nCreated by Wael Sahli\n\nSpecial Thanks TO: 7gxycn08 for GUI updates")
messagebox.showinfo("About", "Audio File Translator - S2ST v1.9\n\nCreated by Wael Sahli\n\nSpecial Thanks TO: 7gxycn08 for GUI updates")

def browse(self):
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.mp3")])
Expand Down
140 changes: 140 additions & 0 deletions ReplaceVideoAudio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
from tkinter import Tk, Label, Button, filedialog, messagebox
import os, subprocess
from CTkMenuBar import *
import customtkinter

class AudioReplacerGUI:
def change_text_color(self, btn):
new_color = 'green'
btn.configure(fg_color=new_color)

def __init__(self, master):
self.master = master
master.title("Audio Replacer")
master.geometry("300x220")
master.minsize(300,220)
master.maxsize(300,240)
master.attributes('-fullscreen', False)
self.video_path = None
self.audio_path = None
self.output_path = None

self.label = customtkinter.CTkLabel(master, text="Select Video, Audio, and Output Paths:", font=("Arial", 12, "bold"),text_color="green")
self.label.pack(pady=5)

self.video_button = customtkinter.CTkButton(master, text="Select Video", command=self.select_video)
self.video_button.pack(pady=5)

self.audio_button = customtkinter.CTkButton(master, text="Select Audio", command= self.select_audio)
self.audio_button.pack(pady=5)

self.output_button = customtkinter.CTkButton(master, text="Select Output", command=self.select_output)
self.output_button.pack(pady=5)

#Replace Audio
self.replace_button = customtkinter.CTkButton(master, text="Run", command=self.replace_audio)
self.replace_button.pack(pady=5)

def select_video(self):
self.change_text_color(self.video_button)
self.video_path = filedialog.askopenfilename(filetypes=[("Video Files", "*.mp4;*.avi")])

def select_audio(self):
self.change_text_color(self.audio_button)
self.audio_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.mp3;*.wav")])

def select_output(self):
self.change_text_color(self.output_button)
self.output_path = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("Video Files", "*.mp4")])

def change_speed(self, audio_path, video_path):
def get_audio_duration(file_path):
try:
# Run ffprobe command to get audio information
command = [
'ffprobe',
'-v', 'error',
'-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1',
file_path
]
result = subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True)

# Parse duration from the result
duration = float(result.strip())

return duration
except subprocess.CalledProcessError as e:
print(f"Error while running ffprobe: {e.output}")
return None

print("Starting change_speed")
ffprobe_command = [
'ffprobe',
'-v', 'error',
'-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1',
video_path
]

video_duration = float(subprocess.check_output(ffprobe_command))
audio_clip = get_audio_duration(audio_path)

# Calculate the speed factor to match the video duration
speed_factor = audio_clip / video_duration

if speed_factor >= 0.5 and speed_factor <=2:
speed_factor = speed_factor
else:
speed_factor = 1

New_Audio_output_path = 'output_speeded_up.mp3'

# Use FFmpeg to speed up or slow down the audio
ffmpeg_command = [
'ffmpeg',
'-i', audio_path,
'-filter:a', f'atempo={speed_factor}',
New_Audio_output_path
]
subprocess.run(ffmpeg_command, check=True)

changed_audio = New_Audio_output_path
return changed_audio

def replace_audio(self):
self.change_text_color(self.replace_button)
if self.video_path and self.audio_path and self.output_path:

adjusted_audio = self.change_speed(self.audio_path, self.video_path)
input_video = self.video_path
input_audio = adjusted_audio
output_video = self.output_path
try:
# FFmpeg command to replace audio in a video
command = [
'ffmpeg',
'-i', input_video,
'-i', input_audio,
'-c:v', 'copy',
'-c:a', 'aac',
'-strict', 'experimental',
'-map', '0:v:0',
'-map', '1:a:0',
output_video
]

# Run the FFmpeg command
subprocess.run(command, check=True)

print(f"Audio replaced successfully. Output video saved as {output_video}")
except subprocess.CalledProcessError as e:
print(f"Error while running FFmpeg: {e.stderr}")

os.remove("output_speeded_up.mp3")
messagebox.showinfo("Info", f"Conversion successful !")

if __name__ == "__main__":
root = Tk()
gui = AudioReplacerGUI(root)
root.mainloop()
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Audio file translator, Speech To Speech Translator is a tool that allows you to
you need to copy and replace 'AudioFileTranslator-S2ST.py' file from 'main' folder to '/AudioFileTranslatror-Portable/resources')
- v1.7 - New GUI updates.
- V1.8 - Some Youtube Downloader fixes and improvements.
- V1.9 - Add a tool who replaces mp4 Video file Audio with translation audio file. (Go to: file > Replace Audio in Video)

## Requirements

Expand Down

0 comments on commit dc65fb0

Please sign in to comment.