How to convert a video using FFmpeg on Termux


Sometimes we need to convert video or export the audio file from the video. What are we used to doing that? Well, we simply use some software. But, if you use Termux, you do not need any other application except FFmpeg.

Ffmpeg is a free and open-source CLI software. With FFmpeg, you can do almost everything that you can do on video converter software. In fact, most of the video converter software was built by FFmpeg.

In this article, I will not teach you any FFmpeg commands. Instead, I will give you a shell script. With that script, anyone can easily convert a video. Export audio from video with different quality. Convert video to 360p, 480p, 720p, 1080p resolution.
Change video bitrate.

Installation


Before doing anything, we need to update and upgrade our Termux system, so execute this command.

apt-get update & apt-get upgrade -y


Now install FFmpeg on your Termux

apt install ffmpeg


Now check the version to ensure the installation.

ffmpeg -version


Now go to the bin folder of Termux by using this command

cd ~/../usr/bin/


At this point, create a file using this command. It will create a file named "convert_video.sh" and open it via the editor. Note: you can use any other name that you like.

nano convert_video.sh


Now copy this 190-line code and paste it into the nano editor.

#!/bin/bash

# Check if ffmpeg is installed
if ! command -v ffmpeg &> /dev/null; then
  echo "ffmpeg is not installed. Please install ffmpeg and try again."
  exit 1
fi

# Function to convert video to audio using the selected audio quality
function convert_to_audio() {
  local input_file=$1

  # Output audio file with the same name but ".mp3" extension
  local output_file="${input_file%.*}.mp3"

  # Check if the output file already exists
  if [ -f "$output_file" ]; then
    echo "Output file already exists: $output_file"
    exit 1
  fi

  # Audio quality options
  local quality_options=("128K" "192K" "256K" "320K")

  # Function to convert video to audio using the selected audio quality
  function convert_audio() {
    local selected_quality=${quality_options[$selected_index]}
    echo "Conversion in progress..."
    ffmpeg -i "$input_file" -b:a "$selected_quality" -vn "$output_file" ## 2> /dev/null
    echo "Conversion complete. The audio file is saved as: $output_file"
    exit 0
  }

  # Main loop for audio conversion
  while true; do
    clear
    echo "Audio Conversion Options:"
    echo "1) Convert to MP3"
    echo "2) Back to Main Menu"
    read -p "Enter your choice: " audio_choice

    case $audio_choice in
      1)
        # Display audio quality options
        clear
        echo "Select Audio Quality:"
        for i in "${!quality_options[@]}"; do
          echo "$(($i+1))) ${quality_options[$i]}"
        done
        read -p "Enter the number for the desired audio quality: " selected_index

        # Check if the selected index is within the valid range
        if [[ "$selected_index" =~ ^[0-9]+$ ]] && ((selected_index >= 1 && selected_index <= ${#quality_options[@]})); then
          selected_index=$((selected_index - 1))
          convert_audio
        else
          echo "Invalid selection. Please try again."
        fi
        ;;
      2)
        exit 0
        ;;
      *)
        echo "Invalid choice. Please try again."
        ;;
    esac
  done
}

# Function to convert video to different video quality options
function convert_to_video() {
  local input_file=$1

  # Output video file with the same name but different quality and extension
  local output_file_base="${input_file%.*}"
  local output_file=""
  local selected_resolution=""
  local bitrate=""

  # Video quality options
  local resolution_options=("1920:1080" "1280:720" "854:480" "640:360")

  # Function to convert video using the selected resolution and bitrate
  function convert_video() {
    echo "Conversion in progress..."

    # Check if bitrate is set
    if [ -n "$bitrate" ]; then
      ffmpeg -i "$input_file" -b:v "$bitrate" -c:v libx264 -preset medium -vf "scale=$selected_resolution" "$output_file" ## 2> /dev/null
    else
      ffmpeg -i "$input_file" -c:v libx264 -preset medium -vf "scale=$selected_resolution" "$output_file" ## 2> /dev/null
    fi

    echo "Conversion complete. The video file is saved as: $output_file"
    exit 0
  }

  # Main loop for video conversion
  while true; do
    clear
    echo "Video Conversion Options:"
    echo "1) Convert to different video resolution"
    echo "2) Back to Main Menu"
    read -p "Enter your choice: " video_choice

    case $video_choice in
      1)
        # Display video resolution options
        clear
        echo "Select Video Resolution:"
        for i in "${!resolution_options[@]}"; do
          echo "$(($i+1))) ${resolution_options[$i]}"
        done
        read -p "Enter the number for the desired video resolution: " selected_index

        # Check if the selected index is within the valid range
        if [[ "$selected_index" =~ ^[0-9]+$ ]] && ((selected_index >= 1 && selected_index <= ${#resolution_options[@]})); then
          selected_index=$((selected_index - 1))
          selected_resolution="${resolution_options[$selected_index]}"

          # Display bitrate options
          clear
          echo "Select Bitrate Option for $selected_resolution:"
          echo "1) Default Bitrate"
          echo "2) Custom Bitrate"
          read -p "Enter your choice: " bitrate_choice

          case $bitrate_choice in
            1)
              bitrate=""
              ;;
            2)
              read -p "Enter the custom bitrate (e.g., 1000k): " bitrate
              ;;
            *)
              echo "Invalid choice. Using default bitrate."
              ;;
          esac

          # Set output file name
          output_file="${output_file_base}_${selected_resolution}.mp4"

          convert_video
        else
          echo "Invalid selection. Please try again."
        fi
        ;;
      2)
        exit 0
        ;;
      *)
        echo "Invalid choice. Please try again."
        ;;
    esac
  done
}

# Main loop for the program
while true; do
  clear
  echo "Main Menu:"
  echo "Script created by:"
  echo "
            (       )     )          
      (     )\ ) ( /(  ( /(      (   
 (    )\   (()/( )\()) )\())(  ( )\  
 )\((((_)(  /(_)|(_)\ ((_)\ )\ )((_) 
((_))\ _ )\(_))   ((_)_ ((_|(_|(_)_  
| __(_)_\(_) __| / _ \ \ / / __| _ ) 
| _| / _ \ \__ \| (_) \ V /| _|| _ \ 
|___/_/ \_\|___/ \___/ |_| |___|___/       
         twitter.com/easoyeb                          
"
  echo "1) Convert Video to Audio"
  echo "2) Convert Video to Different Video Quality"
  echo "3) Exit"
  read -p "Enter your choice: " main_choice

  case $main_choice in
    1)
      if [ $# -eq 0 ]; then
        echo "Please provide a video file as an argument."
        exit 1
      fi
      convert_to_audio "$1"
      ;;
    2)
      if [ $# -eq 0 ]; then
        echo "Please provide a video file as an argument."
        exit 1
      fi
      convert_to_video "$1"
      ;;
    3)
      echo "Exiting the program. Goodbye!"
      exit 0
      ;;
    *)
      echo "Invalid choice. Please try again."
      ;;
  esac
done


Now use CTRL + S to save the file and CTRL + X to exit from the nano editor.


We need to change file permission; otherwise, we can't use the script. To do that, execute this command.

chmod +x convert_video.sh


Here we go; we create a file in the Termux bin folder. And also permit that file. It means we can now access the convert_video.sh, file from anywhere on Termux.

Usage


Now go to a folder containing a video file using the cd command. If you can not access your storage, then use execute this command. This command will give access to your internal storage.
termux-setup-storage

To access your internal storage, go to this path storage/shared/, or you can use this command.

cd ~/storage/shared


Now you can access your folder, which has video file or you want to convert. Also, we have an article about opening a specific directory in the Termux app without using the "cd" command. You can read that article also.

Here, I want to export an audio file from a video located in the "Videos" folder of my internal storage. So I need to go to the internal storage first using this "cd ~/storage/shared" command, and now I have to go to the "Videos" folder where my video file is located using "cd Videos".

Now time to execute our  "convert_video.sh" script. We also need to provide the file name we want to work with.

convert_video.sh myVideo.mp4


Here we write a video file name after convert_video.sh script. Suppose your video name is "New Video.mp4" then white exact after convert_video.sh as an argument.

Note: You can not use only space while providing the file as an argument. It would be best to use a backslash after the "New" keyword like this. "convert_video.sh New\ Video.mp4". If your file name is too long, write the first word and hit the tab key. If it's only a file in the directory that start with that word, then Termux will auto-complete the entire file name with also included backslash.

After executing the above command, you will see something like this.



Here I want to convert the Video file to audio, so I choose the option by writing 1 and hitting enter.

Now again, choose option 1 (Convert to MP3).

And now, which quality do you want to convert with? I want 128k, so I choose 1 option.

Now press enter to start conversion.

Conversion completed. You will see the new file in your file browser.

In conclusion this post, we learned about FFmpeg, and we also learned about how to convert a video file to audio using the script which I provided. With That script, you can also convert a video file with a different resolution, but I will not show you here. You can figure it out by yourself quickly. That's it for today. I hope this article will help you. If you have any problem, feel free to comment in the comment section, and I will give you the solution. Thank you.

Istiak Ahmmed Soyeb

I'm Soyeb, Now I'm a web designer. I chose my career as a web developer. I'm also a Blogger (Not Professional). And I love technology very much.

Post a Comment

Previous Post Next Post