Extract Video Frames Using Termux on Android: A Step-by-Step Guide

In today’s digital age, our smartphones can handle a variety of tasks, from capturing stunning photos and videos to processing complex data. But did you know that you can turn your Android device into a powerful video processing tool? With the help of Termux, Python, and FFmpeg, you can easily extract frames from a video directly on your Android device. In this guide, we’ll walk you through the entire process, step by step.

What You’ll Need:

  • An Android device with Termux installed
  • Basic knowledge of command-line operations
  • FFmpeg and Python installed on Termux

Step 1: Installing Termux

First things first, you need to have Termux installed on your Android device. Termux is a powerful terminal emulator that allows you to run Linux commands on Android. You can find it on the Google Play Store or the F-Droid repository.

Once you have Termux installed, open it up. You should see a command-line interface where you can type commands and interact with your device.

Step 2: Granting Storage Access

To interact with files on your Android device, Termux needs permission to access your storage. Here’s how you can grant these permissions:

Open Termux and type the following command:

termux-setup-storage

Press Enter, and you will see a storage permission request pop up on your screen. Make sure to grant Termux access by tapping Allow.

Step 3: Installing FFmpeg and Python

Now that Termux is set up, we need to install the required packages, namely FFmpeg and Python. Follow these steps:

  1. Update your package list:
    pkg update && pkg upgrade
  2. Install FFmpeg:
    pkg install ffmpeg
  3. Install Python:
    pkg install python

Step 4: Writing the Python Script

Let’s create a Python script that will handle the extraction of frames from your video file. This script will use FFmpeg to process the video and save the frames as images.

Open a text editor in Termux:

nano frames.py

Enter the following Python code into the editor:

import subprocess
import os

def video_to_images_ffmpeg(video_path, output_folder):
    if not os.path.exists(output_folder):
        try:
            os.makedirs(output_folder, exist_ok=True)
            print(f"Created directory: {output_folder}")
        except PermissionError as e:
            print(f"PermissionError: {str(e)}")
            return

    if not os.path.isfile(video_path):
        print("Error: The video file does not exist.")
        return

    command = [
        'ffmpeg',
        '-i', video_path,
        '-vf', 'fps=1',
        os.path.join(output_folder, 'frame_%04d.jpg')
    ]

    try:
        result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        print("Frames extracted successfully using ffmpeg.")
    except subprocess.CalledProcessError as e:
        print(f"An error occurred while extracting frames: {e.stderr.decode()}")

video_path = input("Enter the path to the video file: ")
output_folder = input("Enter the path to the output folder: ")

video_to_images_ffmpeg(video_path, output_folder)

Save the script by pressing Control + X, then Y, and Enter.

Step 5: Running the Script

Now that your script is ready, it’s time to run it and extract frames from your video file.

  1. Run the Python script:
    python frames.py
  2. Please enter the path to your video file when prompted. For instance, if your video file is named input.mp4 and is located in a folder named Tutorial, with the desired output folder being output inside the Tutorial folder, you should enter the path accordingly. For example, if the Tutorial folder is in your current directory, the input file path would be Tutorial/input.mp4 and the output folder path would be Tutorial/output.
    /storage/emulated/0/Tutorial/input.mp4
  3. Enter the path to the output folder:
    /storage/emulated/0/Tutorial/output

The script will process your video and save frames as individual images in the specified output folder.

Conclusion

Congratulations! You’ve successfully extracted frames from a video using Termux, Python, and FFmpeg on your Android device. This setup is not only useful but also incredibly versatile for various video processing tasks directly from your smartphone.

Additional Resources: