Clone voice

# ១. ដំឡើងបណ្ណាល័យចាំបាច់ (រង់ចាំប្រហែល ២-៣ នាទី) !pip install coqui-tts openai-whisper moviepy==1.0.3 googletrans==4.0.0-rc1 torch torchvision torchaudio import os, whisper, asyncio, torch from TTS.api import TTS from googletrans import Translator from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip import moviepy.video.fx.all as vfx # ២. កំណត់ផ្លូវ File (ម្ចាស់គ្រូអាចប្តូរឈ្មោះតាមចិត្ត) VIDEO_IN = "jade_video.mp4" MALE_VOICE = "male_ref.wav" FEMALE_VOICE = "female_ref.wav" OUTPUT_NAME = "BM7_CLONED_COLAB.mp4" # ៣. ដាស់ម៉ាស៊ីន AI (ប្រើ GPU ឥតគិតថ្លៃរបស់ Google) device = "cuda" if torch.cuda.is_available() else "cpu" print(f"🚀 កំពុងប្រើអំណាច GPU: {device}") tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device) translator = Translator() model = whisper.load_model("medium") # ៤. មុខងារក្លូនសំឡេង def clone_voice(text, start, gender): ref_path = MALE_VOICE if gender == "MALE" else FEMALE_VOICE out_path = f"tmp_{start}.wav" tts.tts_to_file(text=text, speaker_wav=ref_path, language="kh", file_path=out_path) return out_path # ៥. ចាប់ផ្ដើមដំណើរការ Dubbing print("🔍 កំពុងវិភាគសាច់រឿង និងបកប្រែ...") transcribe = model.transcribe(VIDEO_IN, language="zh") video = VideoFileClip(VIDEO_IN) audio_tracks = [video.audio.volumex(0.15)] for s in transcribe['segments']: kh_text = translator.translate(s['text'], src='zh-cn', dest='km').text # បែងចែកភេទតួអង្គ (Logic BM7 ម្ចាស់គ្រូ) gender = "FEMALE" if any(w in kh_text for w in ['នាង', 'ស្រី', 'អូន', 'អ្នកនាង']) else "MALE" print(f"🎙️ [{s['start']}s] កំពុងក្លូន៖ {kh_text}") wave_file = clone_voice(kh_text, s['start'], gender) if wave_file: duration = s['end'] - s['start'] clip = AudioFileClip(wave_file).set_start(s['start']).volumex(4.5) # សម្រួលល្បឿនឱ្យត្រូវមាត់ (Lip-sync) if clip.duration > duration: clip = vfx.speedx(clip, factor=clip.duration/duration).set_duration(duration) audio_tracks.append(clip) # ៦. រួមបញ្ចូល និងទាញយកវីដេអូ print("🎬 កំពុងផលិតវីដេអូចុងក្រោយ...") final_video = video.set_audio(CompositeAudioClip(audio_tracks)) final_video.write_videofile(OUTPUT_NAME, codec="libx264", audio_codec="aac") print(f"✅ រួចរាល់! ម្ចាស់គ្រូអាច Download File {OUTPUT_NAME} បានហើយ!")
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

 
Copyright © 2026. BM7//ANGKOR - All Rights Reserved
Template Created by ThemeXpose | BM7//ANGKOR