Spaces:
Sleeping
Sleeping
Update Milestone5API.py
Browse files- Milestone5API.py +10 -15
Milestone5API.py
CHANGED
|
@@ -8,22 +8,17 @@ import torch
|
|
| 8 |
from TTS.api import TTS
|
| 9 |
from moviepy.editor import VideoFileClip
|
| 10 |
from pydub import AudioSegment
|
| 11 |
-
from moviepy.editor import VideoFileClip, AudioFileClip, TextClip, CompositeVideoClip
|
| 12 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def add_text_to_video(input_video_path, output_video_path, text, new_video_path):
|
| 15 |
-
try:
|
| 16 |
-
subprocess.run([
|
| 17 |
-
'ffmpeg',
|
| 18 |
-
'-y',
|
| 19 |
-
'-i', input_video_path,
|
| 20 |
-
'-vf', f"drawtext=text='{text}':fontsize=24:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2",
|
| 21 |
-
'-c:a', 'copy',
|
| 22 |
-
new_video_path
|
| 23 |
-
],check=True, stdout=subprocess.PIPE)
|
| 24 |
-
print("Text added to video successfully.")
|
| 25 |
-
except subprocess.CalledProcessError as e:
|
| 26 |
-
print("Error adding text to video:", e)
|
| 27 |
|
| 28 |
def replace_video_audio(video_file_path, new_audio_file_path, output_video_file_path, translated_text,new_video_path):
|
| 29 |
print("Loading video...")
|
|
@@ -44,7 +39,7 @@ def replace_video_audio(video_file_path, new_audio_file_path, output_video_file_
|
|
| 44 |
|
| 45 |
try:
|
| 46 |
video.write_videofile(output_video_file_path, codec="libx264", audio_codec="aac", remove_temp=True)
|
| 47 |
-
add_text_to_video(output_video_file_path,
|
| 48 |
print("Video file written successfully.")
|
| 49 |
except Exception as e:
|
| 50 |
print("Error writing video file:", e)
|
|
|
|
| 8 |
from TTS.api import TTS
|
| 9 |
from moviepy.editor import VideoFileClip
|
| 10 |
from pydub import AudioSegment
|
|
|
|
| 11 |
import subprocess
|
| 12 |
+
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
| 13 |
+
|
| 14 |
+
def add_text_to_video(input_video_path, output_video_path, text):
|
| 15 |
+
video = VideoFileClip(input_video_path)
|
| 16 |
+
txt_clip = TextClip(text, fontsize=70, color='white')
|
| 17 |
+
txt_clip = txt_clip.set_duration(video.duration)
|
| 18 |
+
txt_clip = txt_clip.set_position(('center', 'bottom'))
|
| 19 |
+
video_with_text = CompositeVideoClip([video, txt_clip])
|
| 20 |
+
video_with_text.write_videofile(output_video_path, codec="libx264", audio_codec="aac", remove_temp=True)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def replace_video_audio(video_file_path, new_audio_file_path, output_video_file_path, translated_text,new_video_path):
|
| 24 |
print("Loading video...")
|
|
|
|
| 39 |
|
| 40 |
try:
|
| 41 |
video.write_videofile(output_video_file_path, codec="libx264", audio_codec="aac", remove_temp=True)
|
| 42 |
+
add_text_to_video(output_video_file_path, new_video_path, translated_text)
|
| 43 |
print("Video file written successfully.")
|
| 44 |
except Exception as e:
|
| 45 |
print("Error writing video file:", e)
|