Hieucyber2208 commited on
Commit
049f3db
·
verified ·
1 Parent(s): a0510b2

Delete test

Browse files
Files changed (1) hide show
  1. test/test.ipynb +0 -391
test/test.ipynb DELETED
@@ -1,391 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 70,
6
- "metadata": {},
7
- "outputs": [],
8
- "source": [
9
- "from moviepy.video.io.VideoFileClip import VideoFileClip, AudioFileClip\n",
10
- "from moviepy.video.VideoClip import TextClip, ImageClip\n",
11
- "from moviepy.video.compositing.CompositeVideoClip import concatenate_videoclips, CompositeVideoClip\n",
12
- "from moviepy.audio.AudioClip import concatenate_audioclips\n",
13
- "from moviepy.video.tools.subtitles import SubtitlesClip\n",
14
- "from moviepy.video.VideoClip import ColorClip\n",
15
- "import os\n",
16
- "from tqdm import tqdm\n",
17
- "from itertools import accumulate\n",
18
- "import pysrt"
19
- ]
20
- },
21
- {
22
- "cell_type": "code",
23
- "execution_count": 29,
24
- "metadata": {},
25
- "outputs": [],
26
- "source": [
27
- "def format_time(seconds):\n",
28
- " \"\"\"Chuyển đổi thời gian (giây) thành định dạng SRT hh:mm:ss,ms\"\"\"\n",
29
- " mins, sec = divmod(seconds, 60)\n",
30
- " hours, mins = divmod(mins, 60)\n",
31
- " return f\"{int(hours):02}:{int(mins):02}:{int(sec):02},{int((sec % 1) * 1000):03}\"\n",
32
- "def get_audio_duration(audio_path):\n",
33
- " # Lọc các file có đuôi .wav\n",
34
- " audio_paths = os.listdir(audio_path)\n",
35
- " audio_list = [file for file in audio_paths if file.endswith(\".wav\")]\n",
36
- " \n",
37
- " # Khởi tạo danh sách audio duration\n",
38
- " duration_list = []\n",
39
- " \n",
40
- " for audio_path in audio_list:\n",
41
- " # Mở file âm thanh và lấy thời gian\n",
42
- " with AudioFileClip(f\"../data/audio/{audio_path}\") as audio:\n",
43
- " duration_list.append(audio.duration)\n",
44
- " # Tính tổng tích lũy thời gian\n",
45
- " duration_list = [format_time(time) for time in list(accumulate(duration_list))]\n",
46
- " return [format_time(0.0)] + duration_list"
47
- ]
48
- },
49
- {
50
- "cell_type": "code",
51
- "execution_count": 31,
52
- "metadata": {},
53
- "outputs": [],
54
- "source": [
55
- "duration_time = get_audio_duration(\"../data/audio\")"
56
- ]
57
- },
58
- {
59
- "cell_type": "code",
60
- "execution_count": 43,
61
- "metadata": {},
62
- "outputs": [],
63
- "source": [
64
- "def create_srt_from_time_and_text(duration_time, text_folder, output_srt):\n",
65
- " subtitle = \"\"\n",
66
- " subtitle_index = 1\n",
67
- " text_list = sorted([file for file in os.listdir(text_folder) if file.endswith('txt')])\n",
68
- " # Duyệt qua các mốc thời gian và file text\n",
69
- " for i in range(len(duration_time) - 1):\n",
70
- " start_time = duration_time[i]\n",
71
- " end_time = duration_time[i + 1]\n",
72
- " \n",
73
- " # Lấy tên file text tương ứng\n",
74
- " text_file = text_list[i] # Giả sử các file có tên như '1.txt', '2.txt', ...\n",
75
- " \n",
76
- " text_path = os.path.join(text_folder, text_file)\n",
77
- " \n",
78
- " if os.path.exists(text_path):\n",
79
- " with open(text_path, 'r', encoding='utf-8') as f:\n",
80
- " text = f.read().strip()\n",
81
- " # Thêm phần subtitle vào chuỗi kết quả\n",
82
- " subtitle += f\"{subtitle_index}\\n{start_time} --> {end_time}\\n{text}\\n\\n\"\n",
83
- " subtitle_index += 1\n",
84
- " else:\n",
85
- " print(f\"File {text_file} không tồn tại!\")\n",
86
- " \n",
87
- " # Lưu vào file SRT\n",
88
- " with open(output_srt, 'w', encoding='utf-8') as f:\n",
89
- " f.write(subtitle)"
90
- ]
91
- },
92
- {
93
- "cell_type": "code",
94
- "execution_count": 44,
95
- "metadata": {},
96
- "outputs": [],
97
- "source": [
98
- "create_srt_from_time_and_text(duration_time, '../data/text', 'subtitle.srt')"
99
- ]
100
- },
101
- {
102
- "cell_type": "code",
103
- "execution_count": 56,
104
- "metadata": {},
105
- "outputs": [],
106
- "source": [
107
- "def concatenate_audio_files(audio_folder, output_audio_path):\n",
108
- " # Lọc tất cả các file âm thanh .wav trong thư mục\n",
109
- " audio_clips = []\n",
110
- " \n",
111
- " for file in sorted(os.listdir(audio_folder)):\n",
112
- " if file.endswith('.wav'):\n",
113
- " audio_path = os.path.join(audio_folder, file)\n",
114
- " audio_clip = AudioFileClip(audio_path)\n",
115
- " audio_clips.append(audio_clip)\n",
116
- " \n",
117
- " # Ghép tất cả các audio clip lại với nhau\n",
118
- " final_audio = concatenate_audioclips(audio_clips)\n",
119
- " \n",
120
- " # Lưu kết quả vào file output\n",
121
- " final_audio.write_audiofile(output_audio_path, codec = 'pcm_s16le')\n",
122
- "\n",
123
- " print(f\"File audio đã được lưu tại: {output_audio_path}\")"
124
- ]
125
- },
126
- {
127
- "cell_type": "code",
128
- "execution_count": 58,
129
- "metadata": {},
130
- "outputs": [
131
- {
132
- "name": "stderr",
133
- "output_type": "stream",
134
- "text": [
135
- "chunk: 1%| | 107/11954 [00:14<27:40, 7.14it/s, now=None]"
136
- ]
137
- },
138
- {
139
- "name": "stdout",
140
- "output_type": "stream",
141
- "text": [
142
- "MoviePy - Writing audio in final_audio.wav\n"
143
- ]
144
- },
145
- {
146
- "name": "stderr",
147
- "output_type": "stream",
148
- "text": [
149
- "chunk: 1%| | 107/11954 [00:25<46:35, 4.24it/s, now=None]"
150
- ]
151
- },
152
- {
153
- "name": "stdout",
154
- "output_type": "stream",
155
- "text": [
156
- "MoviePy - Done.\n",
157
- "File audio đã được lưu tại: final_audio.wav\n"
158
- ]
159
- }
160
- ],
161
- "source": [
162
- "concatenate_audio_files(\"../data/audio\",\"final_audio.wav\")"
163
- ]
164
- },
165
- {
166
- "cell_type": "code",
167
- "execution_count": 111,
168
- "metadata": {},
169
- "outputs": [],
170
- "source": [
171
- "def create_video_from_images(image_folder, audio_path, output_video_path):\n",
172
- " # Đọc file âm thanh để lấy thời lượng\n",
173
- " audio = AudioFileClip(audio_path)\n",
174
- " total_duration = audio.duration # Tổng thời lượng video bằng thời lượng audio\n",
175
- "\n",
176
- " # Đọc tất cả các file ảnh trong thư mục và sắp xếp theo tên\n",
177
- " image_files = [file for file in sorted(os.listdir(image_folder))]\n",
178
- " \n",
179
- " if not image_files:\n",
180
- " raise ValueError(\"Không tìm thấy ảnh nào trong thư mục!\")\n",
181
- "\n",
182
- " # Tính thời lượng hiển thị cho mỗi ảnh\n",
183
- " duration_per_image = total_duration / len(image_files)\n",
184
- "\n",
185
- " # Tạo danh sách các clip ảnh\n",
186
- " clips = [ImageClip(f\"../data/image/{img}\").with_duration(duration_per_image).resized(width=1280) for img in image_files]\n",
187
- "\n",
188
- " # Ghép các clip ảnh lại với nhau\n",
189
- " final_video = concatenate_videoclips(clips, method=\"chain\")\n",
190
- " \n",
191
- " # Gán âm thanh vào video\n",
192
- " final_video .audio = audio\n",
193
- "\n",
194
- " # Xuất video\n",
195
- " final_video.write_videofile(output_video_path, codec=\"libx264\", audio_codec=\"pcm_s16le\", fps=24)\n",
196
- "\n",
197
- " print(f\"Video đã được lưu tại: {output_video_path}\")"
198
- ]
199
- },
200
- {
201
- "cell_type": "code",
202
- "execution_count": 112,
203
- "metadata": {},
204
- "outputs": [
205
- {
206
- "name": "stdout",
207
- "output_type": "stream",
208
- "text": [
209
- "MoviePy - Building video output.mp4.\n",
210
- "MoviePy - Writing audio in outputTEMP_MPY_wvf_snd.wav\n"
211
- ]
212
- },
213
- {
214
- "name": "stderr",
215
- "output_type": "stream",
216
- "text": [
217
- " \r"
218
- ]
219
- },
220
- {
221
- "name": "stdout",
222
- "output_type": "stream",
223
- "text": [
224
- "MoviePy - Done.\n",
225
- "MoviePy - Writing video output.mp4\n",
226
- "\n"
227
- ]
228
- },
229
- {
230
- "name": "stderr",
231
- "output_type": "stream",
232
- "text": [
233
- " \r"
234
- ]
235
- },
236
- {
237
- "name": "stdout",
238
- "output_type": "stream",
239
- "text": [
240
- "MoviePy - Done !\n",
241
- "MoviePy - video ready output.mp4\n",
242
- "Video đã được lưu tại: output.mp4\n"
243
- ]
244
- }
245
- ],
246
- "source": [
247
- "create_video_from_images(\"../data/image\",\"final_audio.wav\",\"output.mp4\")"
248
- ]
249
- },
250
- {
251
- "cell_type": "code",
252
- "execution_count": 91,
253
- "metadata": {},
254
- "outputs": [],
255
- "source": [
256
- "def wrap_text(text, max_width):\n",
257
- " \"\"\"\n",
258
- " Tự động xuống dòng để vừa với chiều rộng max_width.\n",
259
- " \"\"\"\n",
260
- " import textwrap\n",
261
- " return \"\\n\".join(textwrap.wrap(text, width=max_width))"
262
- ]
263
- },
264
- {
265
- "cell_type": "code",
266
- "execution_count": 96,
267
- "metadata": {},
268
- "outputs": [],
269
- "source": [
270
- "def add_subtitles_to_video(video_path, subtitle_path, output_video_path):\n",
271
- " \"\"\"\n",
272
- " Thêm phụ đề từ file .srt trực tiếp vào video.\n",
273
- " \n",
274
- " :param video_path: Đường dẫn video gốc\n",
275
- " :param subtitle_path: Đường dẫn file .srt\n",
276
- " :param output_video_path: Đường dẫn lưu video đầu ra\n",
277
- " \"\"\"\n",
278
- " \n",
279
- " # Đọc file video\n",
280
- " video = VideoFileClip(video_path)\n",
281
- "\n",
282
- " # Đọc file .srt\n",
283
- " subs = pysrt.open(subtitle_path)\n",
284
- "\n",
285
- " subtitle_clips = [] # Danh sách các đoạn phụ đề\n",
286
- "\n",
287
- " # Xử lý từng dòng phụ đề\n",
288
- " for sub in subs:\n",
289
- " # Chuyển thời gian thành giây\n",
290
- " start_time = sub.start.ordinal / 1000 # Chuyển từ milliseconds sang giây\n",
291
- " end_time = sub.end.ordinal / 1000\n",
292
- " font = \"../BeVietnamPro-Light.ttf\"\n",
293
- " # Tạo clip phụ đề\n",
294
- " txt_clip = TextClip(font=font, text=wrap_text(sub.text, max_width=75), font_size=10, stroke_color=\"black\", stroke_width=3, color=\"#fff\")\n",
295
- " \n",
296
- " # Đặt vị trí hiển thị (giữa phía dưới video)\n",
297
- " txt_clip = txt_clip.with_position(('center', 'bottom')).with_duration(end_time - start_time).with_start(start_time)\n",
298
- " \n",
299
- " subtitle_clips.append(txt_clip)\n",
300
- "\n",
301
- " # Ghép phụ đề vào video\n",
302
- " final_video = CompositeVideoClip([video] + subtitle_clips)\n",
303
- "\n",
304
- " # Xuất video với phụ đề\n",
305
- " final_video.write_videofile(output_video_path, fps=video.fps, codec='libx264', threads=4)\n",
306
- "\n",
307
- " print(f\"Video với phụ đề đã được lưu tại: {output_video_path}\")"
308
- ]
309
- },
310
- {
311
- "cell_type": "code",
312
- "execution_count": 97,
313
- "metadata": {},
314
- "outputs": [
315
- {
316
- "name": "stdout",
317
- "output_type": "stream",
318
- "text": [
319
- "{'video_found': True, 'audio_found': True, 'metadata': {'major_brand': 'isom', 'minor_version': '512', 'compatible_brands': 'isomiso2avc1mp41', 'encoder': 'Lavf61.7.100'}, 'inputs': [{'streams': [{'input_number': 0, 'stream_number': 0, 'stream_type': 'video', 'language': None, 'default': True, 'size': [400, 258], 'bitrate': 24, 'fps': 24.0, 'codec_name': 'h264', 'profile': '(High)', 'metadata': {'Metadata': '', 'handler_name': 'VideoHandler', 'vendor_id': '[0][0][0][0]', 'encoder': 'Lavc61.19.100 libx264'}}, {'input_number': 0, 'stream_number': 1, 'stream_type': 'audio', 'language': None, 'default': True, 'fps': 44100, 'bitrate': 127, 'metadata': {'Metadata': '', 'handler_name': 'SoundHandler', 'vendor_id': '[0][0][0][0]'}}], 'input_number': 0}], 'duration': 542.12, 'bitrate': 159, 'start': 0.0, 'default_video_input_number': 0, 'default_video_stream_number': 0, 'video_codec_name': 'h264', 'video_profile': '(High)', 'video_size': [400, 258], 'video_bitrate': 24, 'video_fps': 24.0, 'default_audio_input_number': 0, 'default_audio_stream_number': 1, 'audio_fps': 44100, 'audio_bitrate': 127, 'video_duration': 542.12, 'video_n_frames': 13010}\n",
320
- "/opt/anaconda3/lib/python3.12/site-packages/imageio_ffmpeg/binaries/ffmpeg-macos-aarch64-v7.1 -i output.mp4 -loglevel error -f image2pipe -vf scale=400:258 -sws_flags bicubic -pix_fmt rgb24 -vcodec rawvideo -\n",
321
- "MoviePy - Building video final_output.mp4.\n",
322
- "MoviePy - Writing audio in final_outputTEMP_MPY_wvf_snd.mp3\n"
323
- ]
324
- },
325
- {
326
- "name": "stderr",
327
- "output_type": "stream",
328
- "text": [
329
- " \r"
330
- ]
331
- },
332
- {
333
- "name": "stdout",
334
- "output_type": "stream",
335
- "text": [
336
- "MoviePy - Done.\n",
337
- "MoviePy - Writing video final_output.mp4\n",
338
- "\n"
339
- ]
340
- },
341
- {
342
- "name": "stderr",
343
- "output_type": "stream",
344
- "text": [
345
- " "
346
- ]
347
- },
348
- {
349
- "name": "stdout",
350
- "output_type": "stream",
351
- "text": [
352
- "MoviePy - Done !\n",
353
- "MoviePy - video ready final_output.mp4\n",
354
- "Video với phụ đề đã được lưu tại: final_output.mp4\n"
355
- ]
356
- },
357
- {
358
- "name": "stderr",
359
- "output_type": "stream",
360
- "text": [
361
- "\r"
362
- ]
363
- }
364
- ],
365
- "source": [
366
- "add_subtitles_to_video(\"output.mp4\", \"subtitle.srt\", \"final_output.mp4\")"
367
- ]
368
- }
369
- ],
370
- "metadata": {
371
- "kernelspec": {
372
- "display_name": "base",
373
- "language": "python",
374
- "name": "python3"
375
- },
376
- "language_info": {
377
- "codemirror_mode": {
378
- "name": "ipython",
379
- "version": 3
380
- },
381
- "file_extension": ".py",
382
- "mimetype": "text/x-python",
383
- "name": "python",
384
- "nbconvert_exporter": "python",
385
- "pygments_lexer": "ipython3",
386
- "version": "3.12.7"
387
- }
388
- },
389
- "nbformat": 4,
390
- "nbformat_minor": 2
391
- }