import os, whisper, asyncio, re import edge_tts from googletrans import Translator from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip import moviepy.video.fx.all as vfx # --- ⚙️ ការកំណត់ (Configuration) --- VIDEO_INPUT = "input_video/your_video.mp4" # ដាក់ឈ្មោះ File វីដេអូម្ចាស់គ្រូ OUTPUT_NAME = "final_result/BM7_DUBBED_V3.mp4" translator = Translator() # --- 🎙️ មុខងារបែងចែកភេទតួអង្គ និងកែសម្រួលភាសា --- def filter_and_improve_khmer(text): # ១. បកប្រែពីចិន មកខ្មែរ translation = translator.translate(text, src='zh-cn', dest='km').text # ២. កំណត់ភេទតួអង្គតាមពាក្យគន្លឹះ (Keywords) female_words = ['នាង', 'ស្រី', 'ព្រះនាង', 'អ្នកនាង', 'ម៉ាក់', 'បងស្រី', 'អូន', 'ម្ចាស់ក្សត្រី'] gender = "FEMALE" if any(word in translation for word in female_words) else "MALE" # ៣. កែសម្រួលពាក្យឱ្យសមជារឿងភាគ (Drama Style) replacements = { "អ្នក": "ឯង", "ខ្ញុំ": "យើង", "តើអ្នកសុខសប្បាយទេ": "ឯងសុខសប្បាយទេ?", "តើមានរឿងអ្វី": "មានរឿងអីហ្នឹង?", "ពិតជា": "ពិតមែនហើយ", "អរគុណ": "អរគុណហើយ" } for old, new in replacements.items(): translation = translation.replace(old, new) return translation.strip(), gender # --- 🔊 មុខងារបង្កើតសំឡេង AI --- async def generate_voice(text, start, duration): try: kh_text, gender = filter_and_improve_khmer(text) if not kh_text: return None, None # ជ្រើសរើសសំឡេង (Piseth សម្រាប់ប្រុស, Sreymom សម្រាប់ស្រី) voice = "km-KH-PisethNeural" if gender == "MALE" else "km-KH-SreymomNeural" tmp_path = f"temp_audio_{start}.mp3" communicate = edge_tts.Communicate(kh_text, voice, rate="+5%") # បន្ថែមល្បឿន ៥% ឱ្យសមស្រប await communicate.save(tmp_path) audio = AudioFileClip(tmp_path).set_start(start).volumex(5.0) # បង្កើនកម្រិតសំឡេងឱ្យច្បាស់ # ធ្វើឱ្យសំឡេងត្រូវនឹងរូបភាព (Lip-sync Adjustment) if audio.duration > duration: return vfx.speedx(audio, factor=audio.duration/duration).set_duration(duration), tmp_path return audio, tmp_path except Exception as e: print(f"Error creating voice: {e}") return None, None async def run_dubbing_process(): # ១. វិភាគវីដេអូជាមួយ Whisper (ប្រើភាសាចិន) print("🔍 កំពុងស្កែនសាច់រឿង... (Whisper Medium)") model = whisper.load_model("medium") result = model.transcribe(VIDEO_INPUT, task="transcribe", language="zh") video = VideoFileClip(VIDEO_INPUT) audio_tracks = [video.audio.volumex(0.2)] # បន្ថយសំឡេងដើម ២០% ដើម្បីឱ្យឮសំឡេង AI ច្បាស់ # ២. បង្កើតសំឡេងខ្មែរដាក់ចូលវីដេអូ print("🎬 កំពុងបញ្ចូលសំឡេងខ្មែរដោយ AI...") temp_files = [] for segment in result['segments']: aud, path = await generate_voice(segment['text'], segment['start'], segment['end'] - segment['start']) if aud: audio_tracks.append(aud) temp_files.append(path) # ៣. រួមបញ្ចូល និង Export វីដេអូចុងក្រោយ final_audio = CompositeAudioClip(audio_tracks) final_video = video.set_audio(final_audio) final_video.write_videofile(OUTPUT_NAME, codec="libx264", audio_codec="aac") # លុប File បណ្តោះអាសន្ន for f in temp_files: if os.path.exists(f): os.remove(f) print(f"✅ រួចរាល់ហើយម្ចាស់គ្រូ! វីដេអូស្ថិតនៅ៖ {OUTPUT_NAME}") if __name__ == "__main__": asyncio.run(run_dubbing_process())
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

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