Files
bash-scripts/m4a_to_mp3.sh
2026-01-18 23:43:38 +07:00

59 lines
1.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
SOURCE_DIR="${1:-.}"
if ! command -v ffmpeg &> /dev/null; then
echo "Ошибка: ffmpeg не установлен." >&2
exit 1
fi
# Временный файл для логирования неудач
temp_log=$(mktemp)
# Счётчики
total_files=0
success_count=0
# Считаем общее количество файлов
total_files=$(find "$SOURCE_DIR" -type f -iname "*.m4a" | wc -l)
if [[ $total_files -eq 0 ]]; then
echo "Не найдено файлов .m4a в $SOURCE_DIR"
exit 0
fi
echo "Найдено $total_files файлов для обработки..."
# Используем -print0 и read -d ''
find "$SOURCE_DIR" -type f -iname "*.m4a" -print0 | \
while IFS= read -r -d '' file; do
if [[ -f "$file" ]]; then
dir=$(dirname "$file")
basename=$(basename "$file" .m4a)
mp3_file="$dir/$basename.mp3"
echo "🔄 Конвертация: $file$mp3_file"
if ffmpeg -nostdin -loglevel error -y -i "$file" -codec:a libmp3lame -qscale:a 1 -map_metadata 0 -id3v2_version 3 "$mp3_file"; then
echo "✅ Успешно: $mp3_file"
rm "$file"
else
echo "❌ Ошибка при конвертации: $file" >&2
echo "$file" >> "$temp_log"
fi
fi
done
# Читаем лог и считаем неудачи
if [[ -s "$temp_log" ]]; then
failed_count=$(wc -l < "$temp_log")
echo "⚠️ Не удалось обработать $failed_count файлов:"
cat "$temp_log"
else
echo "✅ Все файлы успешно обработаны."
fi
# Удаляем временный лог
rm -f "$temp_log"
echo "Готово!"