-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata_edit.sh
executable file
·164 lines (143 loc) · 5.12 KB
/
metadata_edit.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
# AUTHOR: Canberk Demir
# DATE: 22/01/25 13.25
# TODO: Make Sure that spaces do not break the metadata
# Version: 0.1
# Ensure ffmpeg is installed
if ! command -v ffmpeg &> /dev/null; then
echo "ffmpeg is not installed. Please install it before running this script."
exit 1
fi
CURRENT_DIR=$PWD
# Verify that the folder exists
if [ ! -d "$CURRENT_DIR" ]; then
echo "The specified folder does not exist: $FOLDER"
exit 1
fi
# functionalized for later use
metadata(){
# Ask for metadata variables
echo -n "Enter new Artist (leave blank to keep current): "
read ARTIST
echo -n "Enter new Album (leave blank to keep current): "
read ALBUM
echo -n "Enter new Genre (leave blank to keep current): "
read GENRE
echo -n "Enter new Year (leave blank to keep current): "
read YEAR
# Process each mp3 file in the folder
for FILE in "$CURRENT_DIR"/*.mp3; do
# Skip if no mp3 files are found
if [ ! -e "$FILE" ]; then
echo "No MP3 files found in $CURRENT_DIR."
exit 1
fi
echo "Processing file: $(basename -s .mp3 "$FILE")"
# Create a temporary output file name
OUTPUT_FILE="${FILE%.mp3}_updated.mp3"
# Get the Title variable as the filename
TITLE="$(basename -s .mp3 "$FILE")"
# Build ffmpeg metadata options
METADATA_OPTIONS=()
[ -n "$TITLE" ] && METADATA_OPTIONS+=(-metadata title="$TITLE")
[ -n "$ARTIST" ] && METADATA_OPTIONS+=(-metadata artist="$ARTIST")
[ -n "$ALBUM" ] && METADATA_OPTIONS+=(-metadata album="$ALBUM")
[ -n "$GENRE" ] && METADATA_OPTIONS+=(-metadata genre="$GENRE")
[ -n "$YEAR" ] && METADATA_OPTIONS+=(-metadata date="$YEAR")
# Run ffmpeg to update metadata
ffmpeg -i "$FILE" -y -codec copy "${METADATA_OPTIONS[@]}" "$OUTPUT_FILE"
if [ $? -eq 0 ]; then
echo "Metadata updated successfully: $OUTPUT_FILE"
# Optionally replace the original file with the updated one
mv "$OUTPUT_FILE" "$FILE"
else
echo "Failed to update metadata for: $FILE"
# Clean up the temporary file if the operation failed
[ -f "$OUTPUT_FILE" ] && rm "$OUTPUT_FILE"
fi
echo "---------------------------------------------"
done
echo "Metadata update process completed."
}
# functionalized for later use
albumcover(){
# Look for a jpeg or png file (supports only those two)
for FILE in "$CURRENT_DIR"/*; do
case "$FILE" in
*.jpg|*.jpeg)
echo "Found an image file!"
echo $(basename "$FILE")
COVER_FILENAME="$(basename "$FILE")"
;;
*.png)
echo "Found an image file!"
echo $(basename "$FILE")
COVER_FILENAME="$(basename "$FILE")"
;;
esac
done
# Process each mp3 file in the folder
for FILE in "$CURRENT_DIR"/*.mp3; do
# Skip if no mp3 files are found
if [ ! -e "$FILE" ]; then
echo "No MP3 files found in $CURRENT_DIR."
exit 1
fi
echo "Processing file: $(basename -s .mp3 "$FILE")"
# Create a temporary output file name
OUTPUT_FILE="${FILE%.mp3}_updated.mp3"
# Run ffmpeg to update image
FILE=$(realpath "$FILE")
ffmpeg -i "$FILE" -i "$COVER_FILENAME" -c copy -map 0 -map 1 "$OUTPUT_FILE"
#Check whether the ffmpeg command returned exit code 0
if [ $? -eq 0 ]; then
echo "Image updated successfully: $OUTPUT_FILE"
# Optionally replace the original file with the updated one
mv "$OUTPUT_FILE" "$FILE"
else
echo "Failed to update metadata for: $FILE"
# Clean up the temporary file if the operation failed
[ -f "$OUTPUT_FILE" ] && rm "$OUTPUT_FILE"
fi
echo "---------------------------------------------"
done
echo "Image update process completed."
}
# Command Line menu for the script will work according to the positional arguments
case $1 in
album)
echo "Running album cover changer..."
albumcover
echo "Thank you for using metadata_edit!"
exit 0
;;
metadata)
echo "Running metadata changer..."
metadata
echo "Thank you for using metadata_edit!"
exit 0
;;
help)
cat << EOF
Hi! This script can do two functions:
-Album cover art changer
-Metadata changer for albums
Usage:
The script should be in the same folder as the mp3 and image files
Call function "album" for album cover art changes
Call function "metadata" for metadata changes
Call "quit" to exit the script
Call "help" to see this text again
NOTES:
-Cover art changer function only supports png and jpeg files
-Metadata changer picks the titles from file names. To function properly, file names should be renamed accordingly before execution
EOF
;;
quit)
echo "Thank you for using metadata_edit!"
exit 0
;;
*)
echo "See help (as a posargu) for a list of commands and explanations"
;;
esac