File size: 7,010 Bytes
e62cec6
 
 
 
 
 
 
 
 
f87ca8b
e62cec6
 
 
 
 
 
 
 
552f457
e62cec6
552f457
e62cec6
 
 
 
 
 
8773ee1
e62cec6
 
 
 
 
 
 
5997829
62cb98a
e62cec6
 
62cb98a
e62cec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62cb98a
2433c72
62cb98a
 
 
 
e62cec6
 
2433c72
62cb98a
 
e62cec6
62cb98a
 
e62cec6
62cb98a
e62cec6
 
 
 
 
 
f87ca8b
e62cec6
f87ca8b
e62cec6
f87ca8b
 
 
e62cec6
f87ca8b
e62cec6
f87ca8b
 
 
 
e62cec6
f87ca8b
 
 
e62cec6
f87ca8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e62cec6
 
8773ee1
 
f87ca8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from moviepy.video.io.VideoFileClip import VideoFileClip, AudioFileClip
from moviepy.video.VideoClip import TextClip, ImageClip
from moviepy.video.compositing.CompositeVideoClip import concatenate_videoclips, CompositeVideoClip
from moviepy.audio.AudioClip import concatenate_audioclips
from moviepy.video.tools.subtitles import SubtitlesClip
from moviepy.video.VideoClip import ColorClip
import os
from itertools import accumulate
import pysrt
import subprocess


def format_time(seconds):
    """Chuyển đổi thời gian (giây) thành định dạng SRT hh:mm:ss,ms"""
    mins, sec = divmod(seconds, 60)
    hours, mins = divmod(mins, 60)
    return f"{int(hours):02}:{int(mins):02}:{int(sec):02},{int((sec % 1) * 1000):03}"
def get_audio_duration(audio_path):
    # Lọc các file có đuôi .mp3
    audio_paths = os.listdir(audio_path)
    audio_list = [file for file in audio_paths if file.endswith(".mp3")]
    
    # Khởi tạo danh sách audio duration
    duration_list = []
    
    for audio_path in audio_list:
        # Mở file âm thanh và lấy thời gian
        with AudioFileClip(f"{audio_path}") as audio:
            duration_list.append(audio.duration)
    # Tính tổng tích lũy thời gian
    duration_list = [format_time(time) for time in list(accumulate(duration_list))]
    return [format_time(0.0)] + duration_list
def create_srt_from_time_and_text(duration_time, text_folder, output_srt):
    subtitle = ""
    subtitle_index = 1
    text_list = sorted([file for file in os.listdir(text_folder) if file.endswith('.txt') and file != "text.txt" and file != "requirements.txt"])
    print(f"Accessing duration_time list: {len(text_list)} elements")
    # Duyệt qua các mốc thời gian và file text
    for i in range(len(duration_time) - 1):
        print(f"Accessing text_list at index {i}, length of text_list: {len(text_list)}")
        start_time = duration_time[i]
        end_time = duration_time[i + 1]
        
        # Lấy tên file text tương ứng
        text_file = text_list[i]  
        
        text_path = os.path.join(text_folder, text_file)
        
        if os.path.exists(text_path):
            with open(text_path, 'r', encoding='utf-8') as f:
                text = f.read().strip()
                # Thêm phần subtitle vào chuỗi kết quả
                subtitle += f"{subtitle_index}\n{start_time} --> {end_time}\n{text}\n\n"
                subtitle_index += 1
        else:
            print(f"File {text_file} không tồn tại!")
    
    # Lưu vào file SRT
    with open(output_srt, 'w', encoding='utf-8') as f:
        f.write(subtitle)
def concatenate_audio_files(audio_folder, output_audio_path):
    # Lọc tất cả các file âm thanh .mp3 trong thư mục
    audio_clips = []
    
    for file in sorted(os.listdir(audio_folder)):
        if file.endswith('.mp3'):
            audio_path = os.path.join(audio_folder, file)
            audio_clip = AudioFileClip(audio_path)
            audio_clips.append(audio_clip)
    
    # Ghép tất cả các audio clip lại với nhau
    final_audio = concatenate_audioclips(audio_clips)
    
    # Lưu kết quả vào file output
    final_audio.write_audiofile(output_audio_path, codec = 'libmp3lame')

    print(f"File audio đã được lưu tại: {output_audio_path}")
def wrap_text(text, max_width):
    """
    Tự động xuống dòng để vừa với chiều rộng max_width.
    """
    import textwrap
    return "\n".join(textwrap.wrap(text, width=max_width))
def create_video(image_folder=None, audio_path=None, subtitle_path=None, output_video_path=None, convert_audio=False):
    """
    Tạo video từ ảnh và âm thanh, có thể thêm phụ đề và chuyển đổi định dạng âm thanh.
    
    :param image_folder: Thư mục chứa ảnh (nếu tạo video từ ảnh)
    :param audio_path: Đường dẫn file âm thanh (nếu tạo video từ ảnh và âm thanh)
    :param subtitle_path: Đường dẫn file phụ đề .srt (nếu muốn thêm phụ đề)
    :param output_video_path: Đường dẫn lưu video đầu ra
    :param convert_audio: Nếu True, chuyển đổi âm thanh sang AAC
    """
    if image_folder and audio_path:
        # Tạo video từ ảnh và âm thanh
        audio = AudioFileClip(audio_path)
        total_duration = audio.duration

        image_files = [file for file in sorted(os.listdir(image_folder)) if file.endswith(".png")]
        if not image_files:
            raise ValueError("Không tìm thấy ảnh nào trong thư mục!")
        
        duration_per_image = total_duration / len(image_files)
        clips = [ImageClip(f"{img}").with_duration(duration_per_image).resized(width=1280) for img in image_files]
        final_video = concatenate_videoclips(clips, method="chain")
        final_video.audio = audio
        final_video.write_videofile(output_video_path, codec="libx264", audio_codec="aac", fps=30)
        print(f"✅ Video đã được tạo tại: {output_video_path}")
    
    if convert_audio:
        # Chuyển đổi định dạng âm thanh của video
        command = [
            "ffmpeg", "-i", output_video_path,
            "-c:v", "copy", "-c:a", "aac", "-b:a", "192k",
            "-y", output_video_path.replace(".mp4", "_aac.mp4")
        ]
        try:
            subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            print(f"✅ Đã chuyển đổi âm thanh: {output_video_path.replace('.mp4', '_aac.mp4')}")
        except subprocess.CalledProcessError as e:
            print(f"❌ Lỗi khi chuyển đổi âm thanh: {e.stderr.decode()}")
    
    if subtitle_path:
        # Thêm phụ đề vào video
        video = VideoFileClip(output_video_path)
        subs = pysrt.open(subtitle_path)
        subtitle_clips = []
        
        for sub in subs:
            start_time = sub.start.ordinal / 1000
            end_time = sub.end.ordinal / 1000
            font = "BeVietnamPro-Light.ttf"
            txt_clip = TextClip(font=font, text=wrap_text(sub.text, max_width=85), font_size=30, stroke_color="black", stroke_width=3, color="#fff")
            txt_clip = txt_clip.with_position(('center', 'bottom')).with_duration(end_time - start_time).with_start(start_time)
            subtitle_clips.append(txt_clip)
        
        final_video = CompositeVideoClip([video] + subtitle_clips)
        final_video.write_videofile(output_video_path.replace(".mp4", "_subtitled.mp4"), fps=video.fps, codec='libx264', threads=4)
        print(f"✅ Video với phụ đề đã được lưu tại: {output_video_path.replace('.mp4', '_subtitled.mp4')}")

def text_to_video():
    duration_time = get_audio_duration("./")
    create_srt_from_time_and_text(duration_time, './', 'subtitle.srt')
    concatenate_audio_files("./","final_audio.mp3")create_video(image_folder="./", audio_path="final_audio.mp3", subtitle_path="subtitle.srt", output_video_path="final_output.mp4")