Giới thiệu project & Demo
Trong một ngày lang thang trên Gihub thì mình có tìm được một repo rất hay, nó hỗ trợ gen các video từ đầu vào là các câu quote và tác giả của câu quote đó. Output là một video quote, để hình dung rõ hơn thì mọi người có thể xem qua demo các video mà mình đã gen từ repo qua channel https://www.youtube.com/@DeManejarChannel/shorts:
Tuy nhiên, repo gốc của tác giả đang thiếu mất file requirements.txt
và hướng dẫn cài đặt chi tiết cũng không có, vì thế mình quyết định fork
lại project và tìm hiểu để thêm file requirements.txt
và viết lại 1 file notebook ShortMaker.ipynb
cho từng bước chạy:
Repo mình đã fork
và chỉnh sửa thêm các bạn có thể xem tại: https://github.com/trannguyenhan/Shorts-Maker
Hướng dẫn chạy được mô tả chi tiết từng bước trong notebook. Video hướng dẫn chi tiết và notebook mình để dưới phần MÔ TẢ của video:
Mọi người thấy video và bài viết hữu ích có thể cho mình 1 like vào video và 1 sub vào kênh nha, mình cũng rất muốn sẽ làm thêm nhiều bài viết để chia sẻ về opensource, các cài đặt như này hơn nữa và sẽ đăng tải lên kênh youtube cá nhân của mình https://www.youtube.com/@tran_nguyenhan.
Hướng dẫn cài đặt
Lưu ý: file notebook mình để dưới phần mô tả của video https://youtu.be/SyMHMLuXUuA?si=h7MF75ULMPyIVP2b (Mọi người thấy hay có thể cho mình 1 like vào video và 1 sub vào kênh nha, rất cảm ơn mọi người), phần này mình sẽ giải thích chi tiết từng bước trong notebook.
Install Python 3.11 và clone project
Bước này để cài đặt Python 3.11, Pip và sử dụng Python 3.11 vừa cài làm môi trường chính cho phần bên dưới.
Clone project:
%cd /content/
!git clone https://github.com/trannguyenhan/Shorts-Maker.git
sau đó cd
tới folder chứa Project.
Install dependencies
Chạy !pip3.11 install -r requirements.txt
để cài đặt dependencies cho project, trong project gốc tác giả thiếu mất file này, mình đã tìm hiểu và thêm các depens cần thiết cho project:
Cài đặt ffmpeg
Tải ffmpeg từ trang chủ và gán vào biến môi trường để sử dụng trong notebook: https://ffmpeg.org.
Sau khi cài đặt xong kiểm tra phiên bản ffmpeg đã tải về:
Chuẩn bị data
Data sẽ có dạng:
{ "verses": [ "Test quote 1." ], "references": [ "trannguyenhan" ]
}
Với verses
là câu quote còn references
là tác giả, chúng ta có thể gen 1 lúc nhiều video bằng cách thêm nhiêu câu quotes như này:
{ "verses": [ "Big data is at the foundation of all the megatrends that are happening today.", "Without big data, you are blind and deaf and in the middle of a freeway.", "Data is the new oil.", "Information is the oil of the 21st century, and analytics is the combustion engine.", "In God we trust. All others must bring data.", "The goal is to turn data into information, and information into insight.", "Data are just summaries of thousands of stories – tell a few of those stories to help make the data meaningful.", "Torture the data, and it will confess to anything.", "Big data will spell the death of customer segmentation and force the marketer to understand each customer as an individual within 18 months or risk being left in the dust.", "The world is now awash in data, and we can see consumers in a lot clearer ways.", "Artificial Intelligence is the new electricity.", "AI is likely to be either the best or worst thing to happen to humanity.", "The pace of progress in artificial intelligence is incredibly fast.", "Some people call this artificial intelligence, but the reality is this technology will enhance us. So instead of artificial intelligence, I think we’ll augment our intelligence.", "By far, the greatest danger of Artificial Intelligence is that people conclude too early that they understand it.", "The real question is, when will we draft an artificial intelligence bill of rights? What will that consist of? And who will get to decide that?", "It’s going to be interesting to see how society deals with artificial intelligence, but it will definitely be cool.", "The future is big data-driven. We have to adapt and innovate.", "Machine learning is the last invention that humanity will ever need to make.", "The greatest benefit of AI and machine learning is not replacing humans, but augmenting human decision-making capabilities." ], "references": [ "Bernard Marr", "Geoffrey Moore", "Clive Humby", "Peter Sondergaard", "W. Edwards Deming", "Carly Fiorina", "Chip & Dan Heath", "Ronald Coase", "Ginni Rometty", "Max Levchin", "Andrew Ng", "Stephen Hawking", "Elon Musk", "Ginni Rometty", "Eliezer Yudkowsky", "Gray Scott", "Mark Zuckerberg", "Tim O'Reilly", "Nick Bostrom", "Fei-Fei Li" ] }
Thay đổi file main.py
File main.py
bạn cần phải sửa các phần sau:
json_file = f"{project_dir}/sources/verses_data/bigdata_data.json"
output_folder = f"{project_dir}/customers"
image_file = f"{project_dir}/sources/demanejar.png"
customer_name = "demanejar"
json_file
là đường dẫn tới file bạn chuẩn bị data ở phần trên, trong notebook mình có viết mộtcell
để sinh ra file này, các bạn chỉ cần chuẩn bị nội dung file thôioutput_folder
là thư mục outputimage_file
là đường dẫn file logo hình ảnhcustomer_name
là tên sub folder output, là các file output của bạn sinh ra sẽ ở trong thư muccustomers/demanejar
Chạy project và xem kết quả
Chạy !python3.11 main.py
:
Chờ một lúc để hệ thống gen video, sau đó kiểm tra output sẽ có log sinh ra như sau:
Output: /content/Shorts-Maker/customers/demanejar/0-trannguyenhan_23_25_9.mp4
Đây là đường dẫn file video sinh ra, vào đường dẫn tương ứng để tải video về với chất lượng cao. Display để xem trên notebook bằng cell
cuối cùng mình viết như sau:
from IPython.display import HTML
from base64 import b64encode # Path to the video file
video_path = '/content/Shorts-Maker/customers/demanejar/0-trannguyenhan_23_25_9.mp4' # Function to display video
def display_video(video_path, width=640, height=480): # Load video video_file = open(video_path, "rb").read() video_url = "data:video/mp4;base64," + b64encode(video_file).decode() return HTML(f""" <video width={width} height={height} controls> <source src="{video_url}" type="video/mp4"> </video> """) # Display video
display_video(video_path)
Lưu ý: mọi người phải tải video trực tiếp từ folder cạnh notebook để có video chất lượng cao nhất, không tải từ output của cell
cuối cùng.
Bài viết được tài trợ bởi: https://demanejar.github.io/