Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 4470355

Browse files
committed
Refactor converter.sh script to be more robust
1 parent 39ceaed commit 4470355

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

bin/converter.sh

+60-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,78 @@
11
#!/bin/bash
22

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.
44

55
# Directory containing the .m4v files
66
DIRECTORY="./input"
77

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+
861
# Iterate over all .m4v files in the directory
9-
for FILE in "$DIRECTORY"/*.m4v; do
62+
for FILE in "${m4v_files[@]}"; do
1063
# Extract the filename without extension
1164
BASENAME=$(basename "$FILE" .m4v)
1265

1366
# Convert the .m4v file to .mp4 using ffmpeg
67+
echo "Converting $FILE to $DIRECTORY/${BASENAME}.mp4..."
1468
ffmpeg -i "$FILE" -c copy "$DIRECTORY/${BASENAME}.mp4"
1569

1670
# Optional: remove the original .m4v file if conversion is successful
1771
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."
1976
fi
2077
done
2178

0 commit comments

Comments
 (0)