import tkinter as tk from tkinter import filedialog from moviepy.editor import…
- User
import tkinter as tk from tkinter import filedialog from moviepy.editor import AudioFileClip from moviepy.editor import VideoFileClip, ImageSequenceClip from moviepy.editor import concatenate_videoclips
def create_video(): image_paths = filedialog.askopenfilenames(title="Chọn hình ảnh", filetypes=[("Image files", "*.jpg *.jpeg .png")]) audio_path = filedialog.askopenfilename(title="Chọn tệp âm thanh", filetypes=[("Audio files", ".mp3 *.wav")])
if not image_paths: print("Vui lòng chọn ít nhất một hình ảnh.") return clips = [ImageSequenceClip([path], durations=[5]) for path in image_paths] final_clip = concatenate_videoclips(clips, method="compose") if audio_path: audio_clip = AudioFileClip(audio_path) final_clip = final_clip.set_audio(audio_clip) final_clip.write_videofile("output.mp4", fps=24) print("Video đã được tạo thành công!")root = tk.Tk() root.title("Tạo video từ hình ảnh")
create_button = tk.Button(root, text="Tạo video", command=create_video) create_button.pack() hãy tối ưu lại và thêm cho tôi chức năng tạo video ở độ phân giải 1920 x 1080, tự scale hình ảnh cho vừa khung hình, thêm hiệu ứng hình ảnh zoom và di chuyển để tạo nên một video tin tức hấp dẫn
- Assistant
Để tối ưu lại và thêm các chức năng bạn yêu cầu, ta sẽ cần update hàm
create_videovới các bước sau:- Định nghĩa độ phân giải 1920x1080 cho video.
- Scale hình ảnh để phù hợp với khung hình, nhưng vẫn giữ tỉ lệ.
- Thêm hiệu ứng zoom và pan (di chuyển) cho hình ảnh.
- Tạo video tin tức từ các hình ảnh đã được thêm hiệu ứng.
Dưới đây là code sau khi tôi đã cải tiến:
import tkinter as tk from tkinter import filedialog from moviepy.editor import AudioFileClip, VideoFileClip, ImageSequenceClip, concatenate_videoclips from moviepy.video.fx import resize, crop def create_video(): image_paths = filedialog.askopenfilenames(title="Chọn hình ảnh", filetypes=[("Image files", "*.jpg *.jpeg *.png")]) audio_path = filedialog.askopenfilename(title="Chọn tệp âm thanh", filetypes=[("Audio files", "*.mp3 *.wav")]) if not image_paths: print("Vui lòng chọn ít nhất một hình ảnh.") return # Định nghĩa độ phân giải video resolution = (1920, 1080) # Chức năng để thêm hiệu ứng zoom và pan (di chuyển) cho hình ảnh def make_zoom_pan_clip(image_path): clip = ImageSequenceClip([image_path], durations=[5]) # Zoom và pan hiệu ứng clip = clip.fx(resize, newsize=resolution).fx(crop, x_center=clip.w/2, y_center=clip.h/2, width=clip.w*0.8, height=clip.h*0.8) return clip # Tạo các clips với hiệu ứng được áp dụng clips = [make_zoom_pan_clip(path) for path in image_paths] final_clip = concatenate_videoclips(clips, method="compose") # Thêm âm thanh vào video nếu có if audio_path: audio_clip = AudioFileClip(audio_path) final_clip = final_clip.set_audio(audio_clip) # Xuất file video final_clip.write_videofile("output.mp4", fps=24, codec='libx264', audio_codec='aac') print("Video đã được tạo thành công!") root = tk.Tk() root.title("Tạo video từ hình ảnh") create_button = tk.Button(root, text="Tạo video", command=create_video) create_button.pack() root.mainloop()Lưu ý rằng, hiệu ứng zoom trong ví dụ trên thực sự chỉ là việc resize hình ảnh sau đó crop để giả lập việc zoom. Tuy nhiên để có hiệu ứng zoom và pan động thực sự, ta cần sử dụng thư viện moviepy một cách sáng tạo và có thể cần sử dụng các phương thức phức tạp hơn như
resizevới các tham số thay đổi theo thời gian haycropđộng.Hiệu ứng zoom và pan trong video news thường rất phức tạp và cần sự tỉ mỉ, việc mô phỏng chúng chỉ bằng một đoạn code ngắn như trên chỉ mang tính chất minh họa cơ bản, không thể hiện đầy đủ tính nghệ thuật và sự phức tạp của chúng trong thực tế.
Bạn cũng cần cài đặt thư viện ffmpeg để xuất video với codec 'libx264' và 'aac'.
- User
hãy sử dụng opencv2 để thực hiện yêu cầu tương tự
- Assistant
OpenCV không hỗ trợ directly việc combine nhiều hình ảnh và âm thanh để tạo video như moviepy có thể làm. Tuy nhiên, bạn có thể sử dụng OpenCV để tạo ra một video từ các hình ảnh với hiệu ứng zoom và pan, sau đó bạn sẽ cần phải use một công cụ khác như
ffmpegđể thêm âm thanh vào video.Dưới đây là một ví dụ về cách sử dụng OpenCV để tạo video từ các hình ảnh, bao gồm hiệu ứng pan (di chuyển) và zoom:
import cv2 import numpy as np import tkinter as tk from tkinter import filedialog def create_video(): image_paths = filedialog.askopenfilenames(title="Chọn hình ảnh", filetypes=[("Image files", "*.jpg *.jpeg *.png")]) if not image_paths: print("Vui lòng chọn ít nhất một hình ảnh.") return # Định nghĩa độ phân giải video resolution = (1920, 1080) fps = 24 fourcc = cv2.VideoWriter_fourcc(*'MP4V') video = cv2.VideoWriter('output.mp4', fourcc, fps, resolution) duration_per_image = 5 # seconds for image_path in image_paths: # Đọc hình ảnh và resize với tỉ lệ giữa chiều cao và chiều rộng img = cv2.imread(image_path) img = cv2.resize(img, resolution, interpolation=cv2.INTER_LANCZOS4) # Tạo hiệu ứng zoom and pan zh, zw = resolution[1]//2, resolution[0]//2 center_x, center_y = img.shape[1] // 2, img.shape[0] // 2 for i in range(fps * duration_per_image): # Example of zoom: simply resize image zoomed_img = cv2.resize(img, (0, 0), fx=1.1, fy=1.1, interpolation=cv2.INTER_LANCZOS4) # Example of pan: move the crop window y1, x1 = center_y - zh - (i//10), center_x - zw + (i//10) y2, x2 = center_y + zh - (i//10), center_x + zw + (i//10) cropped_img = zoomed_img[y1:y2, x1:x2] cropped_img = cv2.resize(cropped_img, resolution, interpolation=cv2.INTER_LANCZOS4) # Add image to video video.write(cropped_img) # Release video writer video.release() print("Video đã được tạo thành công!") root = tk.Tk() root.title("Tạo video từ hình ảnh") create_button = tk.Button(root, text="Tạo video", command=create_video) create_button.pack() root.mainloop()Code trên thực hiện các bước sau:
- Yêu cầu người dùng chọn hình ảnh thông qua giao diện Tkinter.
- Định nghĩa độ phân giải và tốc độ khung hình cho video.
- Tạo hiệu ứng zoom bằng cách phóng to (resize) hình ảnh.
- Tạo hiệu ứng pan bằng cách di chuyển cửa sổ cắt (crop window).
- Ghi từng khung hình đã được chỉnh sửa vào tệp video.
Lưu ý rằng, sau khi bạn đã tạo video, bạn sẽ cần sử dụng một công cụ khác như
ffmpegđể thêm âm thanh và/hoặc kết hợp với các clips khác. OpenCV không hỗ trợ thao tác này một cách trực tiếp.