#1 Le 12/01/2011, à 21:18
- thurston
Script "Music mp3s high Q(avec id3 Tag)" > "mp3s Low Q nomad"
Bonjour,
J'utilise énormément un script assez basique ET basé sur le TAG id3 du mp3 qui sert à transformer une foultitude de mp3s en les
- regroupant de facon logique ~MP3CAR \ Artist \ year-Album \
- gérant les noms un peu long en tronquant automatiquement (compatibilité avec tout lecteur mp3)
- proposant une qualité adaptée à une lecture dans une voiture ou autre
- gérant les problèmes de codage non valide dans les noms...
- me normalisant le son entre chaque morceau
Enfin bref, quand je veux me faire une clé usb de mes MP3s bien codés 320kbts et avec TAG id3tag (oblige), en réduisant la taille et en renommant tout ca en auto,
1. je créé un repertoire disons... ~/music
2. je mets en vrac tous les mp3s qui m'intéresse (AVEC LEUR Tag ID3 valide
3. je copie le script dans le même repertoire ~/music/mp32SDCard (c'est comme ca que je l'appelle, ca pourrait etre mp32USB au choix)
4. dans un terminal je me mets dans le répertoire ~/music, et j'y lance ./mp32SDCard -n
thurston@sursonpc:~/music$ ./mp32SDcard -n
5. on attend un peu, en fonction du CPU, et on récupère dans le répertoire MP3CAR les mp3s low quality, bien rangés, bien nommés et tout et tout...
ca peut donner des idées, ou aider, au cas où...(je sais que pas mal de truc existent déjà, je partage simplement)
A+
Thurston
EDIT: je modifie le code en indiquant l'historique au fur et à mesure de mon boulot dessus...
#!/bin/bash
#########################################
# Script Version: 03
# Revision date: 2011-01-21
#
# Log
# *********************************************
# 01 : Add tests on ID3 if not entered,
# comment was reworked to get a 16 digit display optimization
# 02 : id3v2 replaced by mid3v2 (tag issue: genre)
# 03 : /char bug in tag, creating multi non expected dir levels is now fixed (tag issue: genre)
# Control of argument presence
#******************************************************************************************
if [ "$1" = "-h" ];
then
clear
echo " this script is compressing and re-ordeding MP3s files in the script directory"
echo " to be adequate with car audio environment:"
echo " - level of compression => car listening for higher ratio / more music"
echo " - directory arrangement suitable with small display (you can specify lenght"
echo " directory custom: /Artist - year - Album/ => Artist/Year-Album/"
echo " file name custom: Artist - year - Album - Title => Track-Tittle"
echo ""
echo " Normalization process (at the end of recompressing)"
echo "usage: $0 -n"
exit 1
fi
# Control of function being already installed
#******************************************************************************************
#test -x /usr/bin/id3v2 || echo "installation of id3v2 packet needed:";sudo apt-get install id3v2 -y
test -x /usr/bin/mp3gain || sudo apt-get install mp3gain -y
#test -x /usr/bin/id3v2 || sudo apt-get install id3v2 -y
test -x /usr/bin/mid3v2 || sudo apt-get install mid3v2 -y
test -x /usr/bin/lame || sudo apt-get install lame -y
test -x /usr/bin/convmv || sudo apt-get install convmv -y
#******************************************************************************************
function short_rename()
{
# $1: sentence to be shortly renamed
# $2: nb first
# $3: nb second
IFS=$' ' # put in ligne variable each line of the file thanks to \n balise
nb_longest=0
for word in $1
do
nb=`echo ${#word}`
if [ $nb -ge $nb_longest ];
then
longest_in_2nd=$longest
longest="$word"
nb_longest=$nb
fi
done
first=`echo "$longest_in_2nd" | sed -r 's/(^.| .)/\U&/g' | cut -c-$2`
if [ "$first" = "" ]
then
total_length=$(( $2 + $3 ))
result=`echo "$longest" | sed -r 's/(^.| .)/\U&/g' | cut -c-$total_length`
else
second=`echo "$longest" | sed -r 's/(^.| .)/\U&/g' | cut -c-$3`
result=$first$second
fi
echo "$result"
}
#******************************************************************************************
function extract_id3()
{
# Recupere infos id3
mid3v2 "$1" > /tmp/tmp.$$
artist=$(grep TPE1 /tmp/tmp.$$ | awk -F '=' '{ print $2 }')
album=$(grep TALB /tmp/tmp.$$ | awk -F '=' '{ print $2 }')
genre=$(grep TCON /tmp/tmp.$$ | awk -F '=' '{ print $2 }')
title=$(grep TIT2 /tmp/tmp.$$ | awk -F '=' '{ print $2 }')
track=$(grep TRCK /tmp/tmp.$$ | awk -F '=' '{printf "%02d",$2}')
#track=$(awk '/^TRCK/{printf "%02d",$NF}' /tmp/tmp.$$) # this use of awk gives 01 instead of 1
year=$(grep TDRC /tmp/tmp.$$ | awk -F '=' '{ print $2 }')
if [ "$year" = "" ];
then
year=$(grep TRDA /tmp/tmp.$$ | awk -F '=' '{ print $2 }')
if [ "$year" = "" ];
then
year=$(grep TYER /tmp/tmp.$$ | awk -F '=' '{ print $2 }')
fi
fi
#correction of id3 info missing & correction of special char like / which creates a real mess when creating dirs
if [ "$artist" = "" ]; then artist="noartist"; fi
artist=`echo "$artist" | sed 'y/àâçéèêëîïôöûùü\/\\~\\\\\|\\#\\*\\:\\"\\?\\>\\<\\.\\(\\)\ÂÀÇÉÈÊËÎÏÔÖÙÛÜ/aaceeeeiioouuu--------------AACEEEEIIOOUUU/'`
if [ "$album" = "" ]; then album="noalbum"; fi
album=`echo "$album" | sed 'y/àâçéèêëîïôöûùü\/\\~\\\\\|\\#\\*\\:\\"\\?\\>\\<\\.\\(\\)\ÂÀÇÉÈÊËÎÏÔÖÙÛÜ/aaceeeeiioouuu--------------AACEEEEIIOOUUU/'`
if [ "$title" = "" ]; then title="notitle"; fi
title=`echo "$title" | sed 'y/àâçéèêëîïôöûùü\/\\~\\\\\|\\#\\*\\:\\"\\?\\>\\<\\.\\(\\)\ÂÀÇÉÈÊËÎÏÔÖÙÛÜ/aaceeeeiioouuu--------------AACEEEEIIOOUUU/'`
if [ "$track" = "" ]; then track="xx"; fi
if [ "$year" = "" ]; then year="xxxx"; fi
#comment="$artist - $year - $album - $title"
#comment="`echo "$artist" | sed -r 's/^(.).* (.)/\U\1.\2/' | sed -r 's/(^.| .)/\U&/g' | cut -c-5`-`echo "$title" | sed -r 's/(^.| .)/\U&/g' | sed 's/ //g' | cut -c-10`" #specific comment to be used for car environnement with 16 char on display:5-10, with 5 char being optimized for artist name...
comment=`short_rename "$artist" 2 4`.`short_rename "$title" 3 6`
#echo "$comment"
}
#******************************************************************************************
function safetag()
{
# $1: tag to be safely renamed
if [ "$1" = "" ];
then $1="none"
fi
}
#******************************************************************************************
function insert_id3()
{
mid3v2 -a "$artist"\
-y "$year"\
-A "$album"\
-t "$title"\
-g "$genre"\
-T "$track"\
-c "$comment"\
"$1"
}
#**********************************************************************************************
function Normalization()
{
# scan all mp3 file in the current directory and apply a normalization
# mp3gain -r -k -d 89.0 ./*.mp3
find "$DEST_DIR" -name \*.mp3 -exec mp3gain -r -k {} \; && find . -name \*.MP3 -exec mp3gain -r -k {} \; # use of standard 89dB level for normalization
}
#**********************************************************************************************
function Process_Duration()
{
# $1= Start date
# $2= Stop date
# $3 = Backup Report File if any
# date +%s gives duration between 1-01-1970 in seconds. Then easy for duration calculation
difference=$(( $2-$1 ))
hour=$(( difference/3600 ))
min=$(( (( difference/60 ))-$(( he*60 )) ))
sec=$(( (( difference ))-$(( hour*3600 + min*60 )) ))
#echo "Process Duration = $hour hour $min minutes $sec seconds" >> "$3"
echo "Process Duration = $hour hour $min minutes $sec seconds"
}
#**********************************************************************************************
function Process_Info()
{
# gives different process duration collecting start and stop(s) dates
echo "MP3 re-compress operation duration:"
Process_Duration $start_date $stop_date1
echo " "
echo "MP3 normalization duration (if proceed):"
Process_Duration $stop_date1 $stop_date2
echo ""
}
#******************************************************************************************
function SafeRename ()
{
# $1=file to be safely rename, avoiding codage not valid tag
#new_name=`echo "$1" | sed 'y/àâçéèêëîïôöûùüÂÀÇÉÈÊËÎÏÔÖÙÛÜ/aaceeeeiioouuuAACEEEEIIOOUUU/'`;
#if [ "$new_name" != "$1" ]
#then
#mv "$1" "$new_name";
#fi
convmv -r --notest -f iso-8859-1 -t utf8 "$1"
}
#******************************************************************************************
function re_encode()
{
lame -V 6 -q 0 --vbr-new -h "$1" "$2"
# High quality: HiFi, home or quiet listening
# -V0 (~245 kbps), -V1 (~225 kbps), -V2 (~190 kbps) or -V3 (~175 kbps)
# Portable: background noise and low bitrate requirement, small sizes
# -V4 (~165 kbps), -V5 (~130 kbps) or -V6 (~115 kbps) are recommended
# Very low bitrate, small sizes: eg. for voice, radio, mono encoding etc.
# For very low bitrates, up to 100kbps, ABR is most often the best solution
}
#******************************************************************************************
#************************* Main Program ***************************************************
#******************************************************************************************
exceptions="AlbumArtSmall.jpg|Folder.jpg|Thumbs.db|.zip$|\.$"
DEST_DIR=~/MP3CAR
SCAN_DIR=./
length=32 #number of char to be kept while renaming the processed mp3
if [ ! -e "$DEST_DIR" ];
then
mkdir "$DEST_DIR"
#else
#rm -rf "$DEST_DIR"
#mkdir "$DEST_DIR"
fi
# Backup Start Time
start_date=$(date +%s)
find "$SCAN_DIR" -type f -name '*.mp3' | while read fichier
do
extract_id3 "$fichier"
DEST_ALBUM="$DEST_DIR/`cut -c-$length <<< "$genre"`/`cut -c-$length <<< "$artist"`/$year-`cut -c-$length <<< "$album"`"
DEST_FILE=`cut -c-$length <<< "$track-$title"`
if [ ! -e "$DEST_ALBUM" ];
then
mkdir -p "$DEST_ALBUM"
fi
# name definition (unique)
output="$DEST_ALBUM/$DEST_FILE.mp3"
i=1
while [ -e "$output" ]
do
output="$DEST_ALBUM/$DEST_FILE$i.mp3"
((i+=1))
done
# echo "$output"
re_encode "$fichier" "$output"
insert_id3 "$output"
done
#Process with bad coding name (all dir and files affected)
SafeRename "$DEST_DIR"
# End of recompress process
stop_date1=$(date +%s)
stop_date2=$stop_date1 #in case no Normalization required => 0sec
# Normalization if required
if [ "$1" = "-n" ];
then
Normalization
# Backup End Time
stop_date2=$(date +%s)
fi
# gives status
Process_Info
# commands not used
#find "$SCAN_DIR" -mindepth 1 -type d | sort | while read DIRECTORY # in case scan current directory find . -type f...
Dernière modification par thurston (Le 21/01/2011, à 21:25)
Hors ligne