- vừa được xem lúc

Tạo video short cực nhanh để đăng lên Youtube/Tiktok/Reel với Python

0 0 1

Người đăng: Nguyễn Quang Huy

Theo Viblo Asia

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:

image.png

image.png

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:

maxresdefault.png

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

image.png

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:

image.png

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ề:

image.png

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ột cell để sinh ra file này, các bạn chỉ cần chuẩn bị nội dung file thôi
  • output_folder là thư mục output
  • image_file là đường dẫn file logo hình ảnh
  • customer_name là tên sub folder output, là các file output của bạn sinh ra sẽ ở trong thư muc customers/demanejar

Chạy project và xem kết quả

Chạy !python3.11 main.py:

image.png

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)

image.png

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/

Bình luận

Bài viết tương tự

- vừa được xem lúc

Thao tác với File trong Python

Python cung cấp các chức năng cơ bản và phương thức cần thiết để thao tác các file. Bài viết này tôi xin giới thiệu những thao tác cơ bản nhất với file trong Python.

1 1 142

- vừa được xem lúc

Tập tành crawl dữ liệu với Scrapy Framework

Lời mở đầu. Chào mọi người, mấy hôm nay mình có tìm hiểu được 1 chút về Scrapy nên muốn viết vài dòng để xem mình đã học được những gì và làm 1 demo nho nhỏ.

1 1 243

- vừa được xem lúc

Sử dụng Misoca API (oauth2) với Python

Với bài viết này giúp chúng ta có thể nắm được. ・Tìm hiểu cách xử lý API misoca bằng Python.

1 1 127

- vừa được xem lúc

[Series Pandas DataFrame] Phân tích dữ liệu cùng Pandas (Phần 3)

Tiếp tục phần 2 của series Pandas DataFrame nào. Let's go!!. Ở phần trước, các bạn đã biết được cách lấy dữ liệu một row hoặc column trong Pandas DataFame rồi phải không nào. 6 Hoc.

1 1 144

- vừa được xem lúc

Lập trình socket bằng Python

Socket là gì. Một chức năng khác của socket là giúp các tầng TCP hoặc TCP Layer định danh ứng dụng mà dữ liệu sẽ được gửi tới thông qua sự ràng buộc với một cổng port (thể hiện là một con số cụ thể), từ đó tiến hành kết nối giữa client và server.

0 0 152

- vừa được xem lúc

[Series Pandas DataFrame] Phân tích dữ liệu cùng Pandas (Phần 2)

Nào, chúng ta cùng đến với phần 2 của series Pandas DataFrame. Truy xuất Labels và Data. Bạn đã biết cách khởi tạo 1 DataFrame của mình, và giờ bạn có thể truy xuất thông tin từ đó. Với Pandas, bạn có thể thực hiện các thao tác sau:.

0 0 174