I mentioned earlier how the script could be used with the ipod shuffle. It has since occurred to me that 99.9% of people that use the shuffle on linux would use the iPod shuffle Database Builder script.
I also dislike waiting for prompts, so i have included a variable (useall) that allows the script to automatically use all free space, rather than prompt you to enter a number.
Most significant is that this script removes any leading non-alphabetic characters from the filenames. This is so that the ipod shuffle db builder can group files together better. For example:
Code:
01.mp3
02 - 2802.3478.mp3
02. elvis - jailhouse rock.mp3
03 - gnr - don't cry.mp3
07_-_talking heads_-_girlfriend is better.mp3
09.aphex twin - digeridoo.mp3
12.vbr.mp3
15, elvis, in the ghetto.mp3
aphex twin - come to daddy - 05 - bucephalus.mp3
becomes
Code:
01.mp3
02 - 2802.3478.mp3
aphex twin - come to daddy - 05 - bucephalus.mp3
aphex twin - digeridoo.mp3
elvis - jailhouse rock.mp3
elvis, in the ghetto.mp3
gnr - don't cry.mp3
talking heads_-_girlfriend is better.mp3
vbr.mp3
obviously it will benefit you more if you have a consistent mp3 naming scheme
installation and usage:
1) Save the iPod shuffle Database Builder script to the root of the shuffle.
2) Save this script also to the root of the shuffle and make executable (as mentioned in the first post)
3) Edit the script so the variables such as rebuild_db to point to different locations if you wish. I make use of gnome's habit of hiding files with a period at the start, so I have ".rebuild_db.py" and ".randomcopy" at the pod root. In order to run the script, I keep a launcher in the ipod root with the command bash /media/ipod/.randomcopy
4) Make whatever changes you wish to the other variables so the script operates the way you want it, for example commenting out useall if you like
5) run it, select the folders you want to search
and wait for the script to find and copy your files.
Once done, the script will pass you the results of the ipod shuffle database builder.
6) Unmount and Enjoy!
Code:
#!/bin/bash
# randomcopy 0.6b
# a general-purpose script for copying random files
# modified specifically for the ipod shuffle and the ipod shuffle database builder script
bInMB=1048576
window_title="Randomcopy 0.6b"
window_icon="/usr/share/icons/gnome/16x16/devices/gnome-dev-ipod.png"
tgtPath="/media/ipod/music" # where you wish to copy your music files to. if it doesn't exist the script will create it
dbPath="/media/ipod/iPod_Control" # where the itunes db is kept
rebuild_db="/media/ipod/.rebuild_db.py" # where the ipod shuffle database builder script is kept: this should always be on the root of the ipod
redundancy=$(($bInMB/2)) # 1/2 MB is about enough space for the ipod shuffle database, overall
searchPattern=".*\.\(aac\|aa\|mp3\)" # file extensions to copy
minFS=$(($bInMB/2)) # smallest filesize to copy, in bytes
maxFS=$(($bInMB*40)) # largest filesize to copy, in bytes
useall=yes # comment-out this variable if you wish to be prompted for how many MB of files to copy, otherwise it will automatically use whatever space is left
######################################################################
if ! [ -e "$tgtPath" ] ; then mkdir "$tgtPath" ; fi
dbFS=`du "$dbPath" -b -s | grep -o '^[0-9]*'`
if [ $redundancy -gt $dbFS ]
then redundancy=$(($redundancy-$dbFS))
else redundancy=$dbFS
fi
tgtBFree=$(((`stat -f -c %f "$tgtPath"`*`stat -f -c %s "$tgtPath"`)-$redundancy))
O=$IFS IFS=$'\n' srcPaths=(`zenity --file-selection --directory --multiple --separator=$'\n' --title="Select source directories" --window-icon="$window_icon"`) IFS=$O
(
echo 10
for srcPath in "${srcPaths[@]}" ; do
srcB=$(($srcB+$(echo `find "$srcPath" -type f -size +$(($minFS-1))c -size -+$(($maxFS+1))c -iregex $searchPattern -print0 | xargs -0 stat -c %s+ ; echo 0` | bc)))
done
echo 100
if [ $useall ]
then
if [ $srcB -lt $tgtBFree ]
then remSrcB=$srcB
else remSrcB=$tgtBFree
fi
else
if [ $srcB -lt $tgtBFree ]
then dialog_entry_text=$(($srcB/$bInMB))
else dialog_entry_text=$(($tgtBFree/$bInMB))
fi
remSrcB=$(($bInMB*`zenity --entry --text="$(($srcB/$bInMB))MB of files $(($minFS/$bInMB))-$(($maxFS/$bInMB))MB found. $(($tgtBFree/$bInMB))MB free on device of /$(echo "$tgtPath" | sed 's/^.*\///'). How much to copy?" --entry-text="$dialog_entry_text" --title="$window_title" --window-icon="$window_icon"`))
fi
initSrcB=$remSrcB
if [ $srcB -gt 0 ] ; then # startof if not 0 Bytes found on quick scan
(
echo 10
for srcPath in "${srcPaths[@]}" ; do
O=$IFS IFS=$'\n' tempSrcF=( `find "$srcPath" -type f -size +$(($minFS-1))c -size -+$(($maxFS+1))c -iregex $searchPattern` ) IFS=$O
remSrcF=( "${remSrcF[@]}" "${tempSrcF[@]}" )
done
initSrcF="${#remSrcF[@]}"
echo 100
(
while [ ${#remSrcF[@]} -gt 0 ] && [ $remSrcB -ge $minFS ] ; do
random=$(($RANDOM % $initSrcF))
if [ -n "${remSrcF[$random]}" ] ; then # if element is not empty
tgtFN_np=$(echo "${remSrcF[$random]}" | sed 's/^.*\///') # strips leading path
if [[ $tgtFN_np =~ '[a-zA-Z].*\.[^.]*$' ]] ; then tgtFN_np=$(echo "$tgtFN_np" | sed 's/^[^a-zA-Z]*//') ; fi # strip all nonalpha char before last extn
srcFS=$((`stat -c %s "${remSrcF[$random]}"`))
if [ $srcFS -le $remSrcB ] && [ $((`expr length "$(echo "$tgtPath" | sed 's/^.*\///')/$tgtFN_np"`)) -le 255 ] && ! [ -e "$tgtPath/$tgtFN_np" ] ; then # first condtn checks for space to copy, second condtn checks path not too long for ipod db rebuilder (255), third condition prevents overwrite
cp -i --reply=no "${remSrcF[$random]}" "$tgtPath/$tgtFN_np"
remSrcB=$(($remSrcB-$srcFS)) # post-copy!
srcCpB=$(($srcCpB+$srcFS))
fi
unset remSrcF[$random] # make element empty
echo $((100*($initSrcB-$remSrcB)/$initSrcB))
fi
done
echo 100
python "$rebuild_db" -l | zenity --text-info --title="iPod shuffle Database Builder" --window-icon="$window_icon"
) | zenity --progress --auto-close --percentage=0 --text="copying $(($initSrcB/$bInMB))MB to $(echo "$tgtPath" | sed 's/^.*\///').." --title="$window_title" --window-icon="$window_icon"
) | zenity --progress --pulsate --auto-close --percentage=0 --text="deepscanning selected folders.." --title="$window_title" --window-icon="$window_icon"
fi
) | zenity --progress --pulsate --auto-close --percentage=0 --text="quickscanning selected folders.." --title="$window_title" --window-icon="$window_icon"
exit
again if there are any problems or changes I should look into please post about them
Bookmarks