Skip to content

Instantly share code, notes, and snippets.

@nc9
Last active May 8, 2024 20:39
Show Gist options
  • Save nc9/995d37289e55017dd06ee35e95970236 to your computer and use it in GitHub Desktop.
Save nc9/995d37289e55017dd06ee35e95970236 to your computer and use it in GitHub Desktop.
Convert video files into WhatsApp compatible format

Convert Video File To WhatsApp Compatible Format

This script uses ffmpeg to convert video files to a WhatsApp compatible format. Will also compress the size down making it more suitable.

Install

Ensure you have ffmpeg installed:

$ brew install ffmpeg

Install the script into a directory on $PATH

$ wget -O ~/.local/bin/whatsapp_video_convert https://gist.githubusercontent.com/nc9/995d37289e55017dd06ee35e95970236/raw/c72eb31f4bddc66c0edcb45ad73c48ee6a713dd0/whatsapp_video_convert.sh
$ chmod +x ~/.local/bin/whatsapp_video_convert 
$ whatsapp_video_convert test_video.mp4
#!/bin/bash
# Check if the input video path is provided
if [ $# -eq 0 ]; then
echo "Please provide the path to the input video."
exit 1
fi
input_video="$1"
input_dir=$(dirname "$input_video")
input_file=$(basename "$input_video")
output_file="${input_file%.*}_out.mp4"
output_video="$input_dir/$output_file"
# 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
# Convert the video using FFmpeg
ffmpeg -i "$input_video" -c:v libx264 -profile:v baseline -level 3.0 -pix_fmt yuv420p -s 640x360 -r 30 -b:v 1M -c:a aac -b:a 96k -ar 44100 -strict -2 "$output_video"
echo "Video formatted for WhatsApp: $output_video"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment