TDN-M commited on
Commit
3b677f1
1 Parent(s): aec0642

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image, ImageDraw, ImageFont
5
+ from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip
6
+ import gradio as gr
7
+
8
+ def create_video(imageFolder, textToInsertLine1, textToInsertLine2, language):
9
+ temp_folder = 'temp_images'
10
+ if not os.path.exists(temp_folder):
11
+ os.makedirs(temp_folder)
12
+
13
+ image_paths = []
14
+ for image in imageFolder:
15
+ image_filename = os.path.basename(image.name)
16
+ image_path = os.path.join(temp_folder, image_filename)
17
+ with open(image_path, "wb") as buffer:
18
+ buffer.write(image.read())
19
+ image_paths.append(image_path)
20
+
21
+ video_path = 'output.mp4'
22
+ fps = 30
23
+ frame_size = (1080, 1920)
24
+ total_duration = 60 # Thời lượng video mặc định là 60 giây
25
+ frame_duration = total_duration / len(image_paths) # Thời lượng mỗi ảnh
26
+
27
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
28
+ video_writer = cv2.VideoWriter(video_path, fourcc, fps, frame_size)
29
+
30
+ logo_path = os.path.join("public", 'logo.png')
31
+ logo = cv2.imread(logo_path, cv2.IMREAD_UNCHANGED)
32
+ if logo is None:
33
+ return "Không tìm thấy file logo"
34
+ if logo.shape[2] == 4: # Kiểm tra nếu logo có kênh alpha
35
+ logo = cv2.cvtColor(logo, cv2.COLOR_RGBA2RGB)
36
+ logo = cv2.resize(logo, (100, 100))
37
+
38
+ font_path = os.path.join("public", 'fonts', 'Montserrat-Bold.ttf')
39
+ if not os.path.exists(font_path):
40
+ return "Không tìm thấy file font"
41
+
42
+ try:
43
+ font = ImageFont.truetype(font_path, 50)
44
+ except OSError as e:
45
+ return f"Không thể mở file font: {e}"
46
+
47
+ for image_path in image_paths:
48
+ image = cv2.imread(image_path)
49
+ if image is None:
50
+ return f"Không thể đọc ảnh: {image_path}"
51
+
52
+ # Crop và resize ảnh để đảm bảo kích thước 1080x1920
53
+ height, width = image.shape[:2]
54
+ if height != 1920 or width != 1080:
55
+ image = cv2.resize(image, frame_size)
56
+
57
+ # Chèn logo và text vào ảnh
58
+ image[10:110, 10:110] = logo
59
+ img_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
60
+ draw = ImageDraw.Draw(img_pil)
61
+
62
+ # Vẽ dòng text thứ nhất
63
+ text_bbox1 = draw.textbbox((0, 0), textToInsertLine1, font=font)
64
+ text_size1 = (text_bbox1[2] - text_bbox1[0], text_bbox1[3] - text_bbox1[1])
65
+ text_position1 = ((frame_size[0] - text_size1[0]) // 2, (frame_size[1] - text_size1[1]) // 2 - 50)
66
+ draw.text(text_position1, textToInsertLine1, font=font, fill=(255, 255, 255), stroke_width=2, stroke_fill=(255, 0, 0))
67
+
68
+ # Vẽ dòng text thứ hai
69
+ text_bbox2 = draw.textbbox((0, 0), textToInsertLine2, font=font)
70
+ text_size2 = (text_bbox2[2] - text_bbox2[0], text_bbox2[3] - text_bbox2[1])
71
+ text_position2 = ((frame_size[0] - text_size2[0]) // 2, (frame_size[1] - text_size2[1]) // 2 + 50)
72
+ draw.text(text_position2, textToInsertLine2, font=font, fill=(255, 255, 255), stroke_width=2, stroke_fill=(255, 0, 0))
73
+
74
+ image = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR)
75
+
76
+ for _ in range(int(frame_duration * fps)):
77
+ video_writer.write(image)
78
+
79
+ video_writer.release()
80
+
81
+ # Chèn âm thanh nền mặc định vào video
82
+ default_audio_path = os.path.join("public", 'bg.mp3')
83
+ if not os.path.exists(default_audio_path):
84
+ return "Không tìm thấy file âm thanh nền mặc định"
85
+
86
+ video_clip = VideoFileClip(video_path)
87
+ audio_clip = AudioFileClip(default_audio_path)
88
+ final_audio = CompositeAudioClip([audio_clip])
89
+ final_clip = video_clip.set_audio(final_audio)
90
+ final_clip.write_videofile('final_output.mp4', codec='libx264')
91
+
92
+ for image_path in image_paths:
93
+ os.remove(image_path)
94
+
95
+ return 'final_output.mp4'
96
+
97
+ # Tạo giao diện Gradio
98
+ iface = gr.Interface(
99
+ fn=create_video,
100
+ inputs=[
101
+ gr.File(label="Upload Folder Dataset", file_count="directory"),
102
+ gr.Textbox(label="Text to Insert Line 1"),
103
+ gr.Textbox(label="Text to Insert Line 2"),
104
+ gr.Dropdown(label="Language", choices=["English", "Vietnamese"])
105
+ ],
106
+ outputs=gr.Video(label="Output Video"),
107
+ title="Video Production Tool",
108
+ description="Upload a folder containing images to create a video with text overlay."
109
+ )
110
+
111
+ # Chạy ứng dụng Gradio
112
+ iface.launch()