Salman11223 commited on
Commit
ad48ff9
·
verified ·
1 Parent(s): 705aa43

Create translator.py

Browse files
Files changed (1) hide show
  1. translator.py +82 -0
translator.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import assemblyai as aai
3
+ import requests
4
+ import moviepy.editor as mp
5
+ from TTS.api import TTS
6
+
7
+ class translation:
8
+ def __init__(self, video_path, original_language, target_language):
9
+ self.video_path = video_path
10
+ self.original_language = original_language
11
+ self.target_language = target_language
12
+
13
+ def org_language_parameters(self, original_language):
14
+ if original_language == 'English':
15
+ self.lan_code = 'en'
16
+ elif original_language == 'German':
17
+ self.lan_code = 'de'
18
+ elif original_language == 'French':
19
+ self.lan_code = 'fr'
20
+ elif original_language == 'Spanish':
21
+ self.lan_code = 'es'
22
+ else:
23
+ self.lan_code = ''
24
+
25
+ def target_language_parameters(self, target_language):
26
+ if target_language == 'English':
27
+ self.tran_code = 'en'
28
+ elif target_language == 'German':
29
+ self.tran_code = 'de'
30
+ elif target_language == 'French':
31
+ self.tran_code = 'fr'
32
+ elif target_language == 'Spanish':
33
+ self.tran_code = 'es'
34
+ else:
35
+ self.tran_code = ''
36
+
37
+ def extract_audio(self):
38
+ video = mp.VideoFileClip(self.video_path)
39
+ audio = video.audio
40
+ audio_path = "output_audio.wav"
41
+ audio.write_audiofile(audio_path)
42
+ return audio_path
43
+
44
+ def transcribe_audio(self, audio_path):
45
+ aai.settings.api_key = "YOUR_ASSEMBLYAI_API_KEY"
46
+ config = aai.TranscriptionConfig(language_code=self.lan_code)
47
+ transcriber = aai.Transcriber(config=config)
48
+ transcript = transcriber.transcribe(audio_path)
49
+ return transcript.text
50
+
51
+ def translate_text(self, transcript_text):
52
+ base_url = "https://api.cognitive.microsofttranslator.com"
53
+ endpoint = "/translate"
54
+ headers = {
55
+ "Ocp-Apim-Subscription-Key": "YOUR_MICROSOFT_TRANSLATOR_API_KEY",
56
+ "Content-Type": "application/json",
57
+ "Ocp-Apim-Subscription-Region": "southeastasia"
58
+ }
59
+ params = {
60
+ "api-version": "3.0",
61
+ "from": self.lan_code,
62
+ "to": self.tran_code
63
+ }
64
+ body = [{"text": transcript_text}]
65
+ response = requests.post(base_url + endpoint, headers=headers, params=params, json=body)
66
+ return response.json()[0]["translations"][0]["text"]
67
+
68
+ def generate_audio(self, translated_text):
69
+ tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
70
+ tts.tts_to_file(translated_text, speaker_wav='output_audio.wav', file_path="output_synth.wav", language=self.tran_code)
71
+ return "output_synth.wav"
72
+
73
+ def translate_video(self):
74
+ audio_path = self.extract_audio()
75
+ self.org_language_parameters(self.original_language)
76
+ self.target_language_parameters(self.target_language)
77
+ transcript_text = self.transcribe_audio(audio_path)
78
+ translated_text = self.translate_text(transcript_text)
79
+ translated_audio_path = self.generate_audio(translated_text)
80
+
81
+ subprocess.run(['python', 'inference.py', '--checkpoint_path', 'checkpoints/wav2lip_gan.pth', '--face', self.video_path, '--audio', translated_audio_path, '--outfile', 'output_video.mp4'])
82
+ return 'output_video.mp4'