Skip to content

Instantly share code, notes, and snippets.

@Ppang0405
Created December 2, 2025 13:18
Show Gist options
  • Select an option

  • Save Ppang0405/a3cd21353eeee8b1c25b3e13ada5636e to your computer and use it in GitHub Desktop.

Select an option

Save Ppang0405/a3cd21353eeee8b1c25b3e13ada5636e to your computer and use it in GitHub Desktop.
#!/bin/bash
# Video conversion script using ffmpeg
# Converts video files to mp4 format and moves original files to trash
# Check if folder path is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <folder_path>"
echo "Example: $0 /path/to/video/folder"
exit 1
fi
FOLDER_PATH="$1"
# Check if folder exists
if [ ! -d "$FOLDER_PATH" ]; then
echo "Error: Folder '$FOLDER_PATH' does not exist"
exit 1
fi
# Check if trash command is available
if ! command -v trash &> /dev/null; then
echo "Warning: 'trash' command not found. Files will be permanently deleted."
echo "Install trash-cli with: brew install trash (macOS) or apt install trash-cli (Ubuntu/Debian)"
read -p "Continue without trash? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
USE_TRASH=false
else
USE_TRASH=true
fi
# Supported video formats to convert
VIDEO_EXTENSIONS=("mkv" "avi" "mov" "wmv" "flv" "webm" "m4v" "3gp" "mpg" "mpeg")
echo "Starting video conversion in folder: $FOLDER_PATH"
echo "========================================"
# Change to the target folder
cd "$FOLDER_PATH" || exit 1
# Counter for processed files
processed=0
skipped=0
# Process each video file
for ext in "${VIDEO_EXTENSIONS[@]}"; do
for file in *."$ext"; do
if [ -f "$file" ]; then
filename="${file%.*}"
output_file="${filename}.mp4"
# Skip if mp4 version already exists
if [ -f "$output_file" ]; then
echo "⏭️ Skipping: $file (mp4 already exists)"
((skipped++))
continue
fi
echo "🎬 Converting: $file β†’ $output_file"
# Convert video using ffmpeg
if ffmpeg -i "$file" -c:v libx264 -c:a aac -movflags +faststart -y "$output_file" 2>/dev/null; then
echo "βœ… Success: $file converted to $output_file"
# Move original file to trash or delete permanently
if [ "$USE_TRASH" = true ]; then
trash "$file"
echo "πŸ—‘οΈ Moved to trash: $file"
else
rm "$file"
echo "πŸ—‘οΈ Deleted: $file"
fi
((processed++))
else
echo "❌ Error: Failed to convert $file"
fi
echo "----------------------------------------"
fi
done
done
echo "========================================"
echo "Conversion complete!"
echo "βœ… Processed: $processed files"
echo "⏭️ Skipped: $skipped files"
if [ $processed -eq 0 ]; then
echo "No video files were found for conversion."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment