China Movies Speak Khmer.
May 13, 2026
១. ការដំ ឡើងប ណ្ណា ល័យ (Run ក្នុង Cell ទឡើ១)
!pip install -q -U google-generativeai spleeter whisper moviepy edge-tts
២. កូដម "The Beast" (Run ក្នុង Cell ទឡើ២)
import os, whisper, asyncio, edge_tts, re, torch import google.generativeai as
genai from spleeter.separator import Separator from moviepy.editor import
VideoFileClip, AudioFileClip, CompositeAudioClip import moviepy.video.fx.all as vfx
# --- ⚙️ កំណត់សោរ API របស់ ម្ចា ស់គ្រូ --- API_KEY = "AIzaSyDr5B0ZvrFwBUz69iUddF-KMHSb4IGENWA"
genai.configure(api_key=API_KEY) gemini_ai = genai.GenerativeModel('gemini-1.5-
flash') Video_Name = "Ai.mp4" VIDEO_PATH = f"input_video/{Video_Name}" OUTPUT_PATH
= f"final_result/BM7_BEAST_V6_{Video_Name}"# --- 🧠 មុខងារវិភាគ្រភេទ និងបកបដោយ Gemini ---
async def ai_process_context(text): try: prompt = ( f"Translate this Chinese drama
line to natural spoken Khmer: '{text}'. " f"Also identify if the speaker is Male or
Female. " f"Format: [Gender] | [Translation]" ) response =
gemini_ai.generate_content(prompt) result = response.text.strip() gender = "Male"
if "Male" in result else "Female" khmer_text = result.split('|')[-1].strip()
khmer_text = re.sub(r'[.!?៖។]'
, ' ', khmer_text) return gender, khmer_text except:
return "Male",
"" async def generate_beast_audio(text, start, duration, index):
gender, kh_text = await ai_process_context(text) if not kh_text: return None, None
# ប្ដូរសំឡេងតាមភេទស្វ័យបវត្តិ voice = "km-KH-PisethNeural" if gender == "Male" else "km-KH-
SreymomNeural" tmp = f"tmp_{index}.mp3" comm = edge_tts.Communicate(kh_text, voice,
rate="+15%", pitch="-1Hz") await comm.save(tmp) audio =
AudioFileClip(tmp).set_start(start).volumex(6.0) if audio.duration > duration:
audio = vfx.speedx(audio, factor=audio.duration/duration).set_duration(duration)
return audio, tmp async def start_beast_dubbing(): # ១. បំបកសំឡេង (Spleeter) print("
Step 1: Removing Original Vocals (Spleeter)...") separator = Separator('spleeter:
2stems') separator.separate_to_file(VIDEO_PATH, "temp_beast") base =
os.path.splitext(os.path.basename(VIDEO_PATH))[0] bg_music_path = f"temp_beast/
{base}/accompaniment.wav"# ២. សោ ្ដ ប់ (Whisper) print("
🚀 Step 2: AI Listening
(Whisper)...") model = whisper.load_model("medium").to("cuda" if
torch.cuda.is_available() else "cpu") result = model.transcribe(VIDEO_PATH,
language="zh") audio_tracks = [AudioFileClip(bg_music_path).volumex(0.3)]
temp_files = [] # ៣. ផលិតសំឡេងបកប print("
🎙️ Step 3: Dubbing with Gemini Context...")
for i, s in enumerate(result['segments']): aud, path = await
🧹
generate_beast_audio(s['text'], s['start'], s['end']-s['start'], i) if aud:
audio_tracks.append(aud) temp_files.append(path) # ៤. រ ួ មបញម្ចាូលវីដេអូ video =
VideoFileClip(VIDEO_PATH) final_video =
video.set_audio(CompositeAudioClip(audio_tracks))
final_video.write_videofile(OUTPUT_PATH, codec="libx264", audio_codec="aac")
print(f"
✅ DONE! Video saved at: {OUTPUT_PATH}") # ⚠️ សប់ Google Colab តូវបប្រើ await
await start_beast_dubbing()
- Blogger Comment
- Facebook Comment
0 comments:
Post a Comment