Wednesday, August 7, 2024

Create Termux Bot which helps to create shorts and reels from YouTube video

Convert YouTube Videos to 30-Second Reels in Termux

Convert YouTube Videos to 30-Second Reels in Termux

Follow these steps to create a bot in Termux that converts YouTube videos into multiple 30-second reels.

Step 1: Set Up Termux

Ensure you have Termux installed on your Android device. Then, update and upgrade the packages:

pkg update && pkg upgrade

Step 2: Install Required Packages

Install the necessary packages: youtube-dl, ffmpeg, and python.

pkg install python ffmpeg
pip install youtube-dl imageio[ffmpeg]

Step 3: Create the Python Script

Create a Python script to download the video, split it into 30-second parts, and save them.

Create a file named yt_reels.py:

nano yt_reels.py

Step 4: Write the Script

Copy and paste the following code into yt_reels.py:

import os
import sys
import youtube_dl
import imageio_ffmpeg as ffmpeg
import math
import subprocess

def download_video(url, output_path):
    ydl_opts = {
        'format': 'best',
        'outtmpl': output_path
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])

def get_video_duration(input_path):
    result = subprocess.run(
        ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', input_path],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    return float(result.stdout)

def split_video(input_path, output_folder, segment_duration=30):
    duration = get_video_duration(input_path)
    num_segments = math.ceil(duration / segment_duration)

    for i in range(num_segments):
        start_time = i * segment_duration
        output_path = os.path.join(output_folder, f"short_video_{i+1}.mp4")
        subprocess.run(
            ['ffmpeg', '-ss', str(start_time), '-i', input_path, '-t', str(segment_duration), '-c', 'copy', output_path]
        )
        print(f"Saved {output_path}")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python yt_reels.py ")
        sys.exit(1)

    url = sys.argv[1]
    input_video = "downloaded_video.mp4"
    output_folder = "reels"

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    print("Downloading video...")
    download_video(url, input_video)
    print("Splitting video into 30-second reels...")
    split_video(input_video, output_folder)
    print(f"All reels saved in {output_folder}")

Step 5: Run the Script

To run the script, use the following command:

python yt_reels.py <YouTube URL>

Replace <YouTube URL> with the actual URL of the YouTube video you want to convert.

Explanation

Download Video: The script downloads the video from YouTube using youtube-dl.

Get Video Duration: It uses ffprobe to get the total duration of the video.

Split Video: It splits the video into multiple 30-second parts using ffmpeg and saves them in the reels folder.

Additional Features

You can further enhance the script to:

  • Handle errors and edge cases more robustly.
  • Allow custom segment durations.
  • Automate file management (e.g., organize output videos into subfolders).

Ensure you have enough storage space and necessary permissions to read/write files in Termux.

No comments:

Post a Comment