|
| 1 | +#!/usr/bin/env zsh |
| 2 | + |
| 3 | +# Store the script name |
| 4 | +SCRIPT_NAME=$(basename "$0") |
| 5 | + |
| 6 | +# Function to display help text |
| 7 | +show_help() { |
| 8 | + echo "Usage: $SCRIPT_NAME [-h|--help] <file_or_directory>" |
| 9 | + echo |
| 10 | + echo "Convert a .ogg file or all .ogg files in the specified directory to .mp3 using ffmpeg." |
| 11 | + echo |
| 12 | + echo "Options:" |
| 13 | + echo " -h, --help Show this help message and exit" |
| 14 | +} |
| 15 | + |
| 16 | +# Function to convert a single .ogg file to .mp3 |
| 17 | +convert_file() { |
| 18 | + local file=$1 |
| 19 | + if [[ "$file" == *.ogg ]]; then |
| 20 | + ffmpeg -i "$file" "${file%.ogg}.mp3" |
| 21 | + echo "Conversion complete for file '$file'." |
| 22 | + else |
| 23 | + echo "Error: The file '$file' is not a .ogg file." |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +# Function to convert all .ogg files in a directory to .mp3 |
| 29 | +convert_directory() { |
| 30 | + local dir=$1 |
| 31 | + local ogg_files=("$dir"/*.ogg) |
| 32 | + |
| 33 | + if [[ ${#ogg_files[@]} -eq 0 ]]; then |
| 34 | + echo "Error: No .ogg files found in the directory '$dir'." |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + |
| 38 | + for f in "$dir"/*.ogg; do |
| 39 | + if [[ -f "$f" ]]; then |
| 40 | + convert_file "$f" |
| 41 | + fi |
| 42 | + done |
| 43 | + |
| 44 | + echo "Conversion complete for all .ogg files in the directory '$dir'." |
| 45 | +} |
| 46 | + |
| 47 | +# Check for help option |
| 48 | +if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| 49 | + show_help |
| 50 | + exit 0 |
| 51 | +fi |
| 52 | + |
| 53 | +# Check if the argument is provided |
| 54 | +if [[ -z "$1" ]]; then |
| 55 | + show_help |
| 56 | + echo |
| 57 | + echo "Error: No file or directory provided." |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +# Get the file or directory from the argument |
| 62 | +input=$1 |
| 63 | + |
| 64 | +# Check if ffmpeg is installed |
| 65 | +if ! command -v ffmpeg &>/dev/null; then |
| 66 | + echo "Error: ffmpeg is not installed. Please install ffmpeg to use this script." |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +# Check if the input is a directory or a file |
| 71 | +if [[ -d "$input" ]]; then |
| 72 | + convert_directory "$input" |
| 73 | +elif [[ -f "$input" ]]; then |
| 74 | + convert_file "$input" |
| 75 | +else |
| 76 | + echo "Error: The input '$input' is neither a file nor a directory." |
| 77 | + exit 1 |
| 78 | +fi |
0 commit comments