|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
3 |
| -# This is a legacy script that converts all .m4v files in a directory to .mp4 files using ffmpeg. |
| 3 | +# This script converts all .m4v files in a directory to .mp4 files using ffmpeg. |
4 | 4 |
|
5 | 5 | # Directory containing the .m4v files
|
6 | 6 | DIRECTORY="./input"
|
7 | 7 |
|
| 8 | +# Exit immediately if a command exits with a non-zero status |
| 9 | +set -e |
| 10 | + |
| 11 | +# Function to install ffmpeg |
| 12 | +install_ffmpeg() { |
| 13 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 14 | + # macOS |
| 15 | + if ! command -v brew &>/dev/null; then |
| 16 | + echo "Homebrew is not installed. Please install Homebrew first." |
| 17 | + exit 1 |
| 18 | + fi |
| 19 | + echo "Installing ffmpeg using Homebrew..." |
| 20 | + brew install ffmpeg |
| 21 | + elif [[ "$OSTYPE" == "linux-gnu"* ]]; then |
| 22 | + # Linux |
| 23 | + if command -v apt-get &>/dev/null; then |
| 24 | + echo "Installing ffmpeg using apt-get..." |
| 25 | + sudo apt-get update |
| 26 | + sudo apt-get install -y ffmpeg |
| 27 | + else |
| 28 | + echo "Unsupported Linux distribution. Please install ffmpeg manually." |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | + else |
| 32 | + echo "Unsupported OS. Please install ffmpeg manually." |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | +} |
| 36 | + |
| 37 | +# Check if ffmpeg is installed |
| 38 | +if ! command -v ffmpeg &>/dev/null; then |
| 39 | + echo "ffmpeg could not be found. Attempting to install ffmpeg..." |
| 40 | + install_ffmpeg |
| 41 | + if ! command -v ffmpeg &>/dev/null; then |
| 42 | + echo "ffmpeg installation failed. Please install ffmpeg manually." |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +fi |
| 46 | + |
| 47 | +# Check if the directory exists |
| 48 | +if [ ! -d "$DIRECTORY" ]; then |
| 49 | + echo "Directory $DIRECTORY does not exist." |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +# Check if there are any .m4v files in the directory |
| 54 | +shopt -s nullglob |
| 55 | +m4v_files=("$DIRECTORY"/*.m4v) |
| 56 | +if [ ${#m4v_files[@]} -eq 0 ]; then |
| 57 | + echo "No .m4v files found in $DIRECTORY." |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
8 | 61 | # Iterate over all .m4v files in the directory
|
9 |
| -for FILE in "$DIRECTORY"/*.m4v; do |
| 62 | +for FILE in "${m4v_files[@]}"; do |
10 | 63 | # Extract the filename without extension
|
11 | 64 | BASENAME=$(basename "$FILE" .m4v)
|
12 | 65 |
|
13 | 66 | # Convert the .m4v file to .mp4 using ffmpeg
|
| 67 | + echo "Converting $FILE to $DIRECTORY/${BASENAME}.mp4..." |
14 | 68 | ffmpeg -i "$FILE" -c copy "$DIRECTORY/${BASENAME}.mp4"
|
15 | 69 |
|
16 | 70 | # Optional: remove the original .m4v file if conversion is successful
|
17 | 71 | if [ $? -eq 0 ]; then
|
18 |
| - rm "$FILE" |
| 72 | + echo "Conversion successful, removing $FILE..." |
| 73 | + rm -v "$FILE" |
| 74 | + else |
| 75 | + echo "Conversion failed for $FILE." |
19 | 76 | fi
|
20 | 77 | done
|
21 | 78 |
|
|
0 commit comments