Page 1 of 1

fill your mp3 player with a random mix

Posted: Fri Mar 14, 2014 9:20 pm
by pidsley
I have an ancient mp3 player that only holds 512M. This is plenty long enough for my work when set it on shuffle play, but I like to remix the player once a week or so. I have a directory full of rips from somafm (using streamripper), and a few songs I always like to have in the mix. This script includes those I like to keep, and then builds a random list from the rips until I have enough to fill the player. I like arrays, so I used one again here. The script is easily modified, and you may or may not find it useful, but here it is anyway. Use any ideas you like, leave what you don't.

Code: Select all

#!/bin/bash

# copy music from src to dest
# stop when dest is full enough for mp3 player

SRC="/media/shared/somafm"		# where to get the songs
DEST="/media/shared/music-list"		# where to put the random mix
KEEP="/media/shared/somafm/keep/*.mp3"	# songs you always want in the mix	
MAX_SIZE=450000				# how big is the mix?

rm $DEST/*  # delete the old mix
cp $KEEP $DEST	# copy songs to keep

alist=( $(shuf -e $SRC/*.mp3) ) # BAD IDEA if file names contain spaces

n=0
for song in ${alist[*]}; do
	let n+=1
	cp "$song" $DEST
	size=$(du $DEST | awk '{print $1}')
	echo $n $size $song
	[[ $size -gt  $MAX_SIZE ]] && break # stop when it's big enough
done
WARNING! Using "shuf" (or "ls") to fill an array like this is a BAD IDEA if any of the files in the music directory contain spaces. You will see a bunch of failed copies as the script tries to copy files that do not exist, and you will hear me asking why you have file names with spaces in them.

Re: fill your mp3 player

Posted: Fri Mar 14, 2014 9:28 pm
by GekkoP
Well, believe you me, I find it very useful. I got an mp3 reader I use in my car (with one of these toys), and I'll use this to update it. Thanks for sharing this script.

Re: fill your mp3 player

Posted: Fri Mar 14, 2014 9:38 pm
by wuxmedia
really nice pids, might adapt it for a mp3 CD player... Love the fill until full Idea!

Re: fill your mp3 player with a random mix

Posted: Fri Mar 14, 2014 9:44 pm
by pidsley
You're both very welcome! GekkoP, I have a similar adapter for my old car. Wux, it should be easy to modify the script to fill mp3 CDs. I added the script to my github: https://github.com/pidsley/codemangler/ ... mp3-player

Re: fill your mp3 player with a random mix

Posted: Fri Mar 14, 2014 10:20 pm
by wuxmedia
cheers Pids, will play when less inebriated 8)
I had tape converters, but they were pretty hit and miss, I treated myself to a cheap Sony CD with front aux in.

Re: fill your mp3 player with a random mix

Posted: Sat Mar 15, 2014 1:54 am
by ChefIronBelly
I like it and will use it thanks.

Re: fill your mp3 player with a random mix

Posted: Sun Mar 16, 2014 10:03 am
by kexolino
Damn, I have an oooold 512 MB mp3 player too somewhere, one of those that you can pull apart and it turns into a USB stick. If I can find it, I'm definitely using this script.

Re: fill your mp3 player with a random mix

Posted: Mon Mar 17, 2014 10:50 am
by rhowaldt
cool idea pids, thanks for sharing.

Re: fill your mp3 player with a random mix

Posted: Tue Mar 18, 2014 5:06 pm
by machinebacon
nice, thanks for sharing. And if it is not the old mp3 player, it can be the (smart)phone :)

Re: fill your mp3 player with a random mix

Posted: Tue Mar 18, 2014 9:13 pm
by ivanovnegro
Just awesome. I have an old mp3 player I still use and it has 2 GB. Let me try that script the next time I travel.

Re: fill your mp3 player with a random mix

Posted: Wed Mar 26, 2014 2:18 pm
by johnraff
Pidsley, this was a great idea! One of those why didn't I think of that? moments.
I have an old 500MB Ipod shuffle I use in the car for which this will be perfect.

I had to hack at your code a bit, though, to fit my purposes.
My music is a big rambling directory tree full of stuff from all kinds of sources with all kinds of naming schemes, and yes many of the filenames have spaces. ..

So, I brought in "find" to get files out of the darkest corners, and to cope with those spaces used 'find stuff -print0 | xargs -0 shuf -e' to get the shuffled list, then the bash builtin "mapfile" (thanks to Dr. Xaos for that tip) to put that in an array. Spaces in filenames are OK, but names containing linebreaks will still mess it up. Added a bit of quoting too.

I don't know if your "du" is different but I need the -c option to get the total space used, combined with a filter in awk: du -c "$DEST" | awk '/total/{print $1}'

Otherwise your script is exactly the same and in a few seconds went through 15GB to give me a directory with 450MB of nicely random music. (with spaces in some filenames)
fwiw:

Code: Select all

#!/bin/bash

# copy music from src to dest
# stop when dest is full enough for mp3 player

SRC="$HOME/music"      # where to get the random mix
DEST="$HOME/for_stick"      # where to put the random mix
KEEP=("$HOME/music/keep"/*)   # songs you always want in the mix   
MAX_SIZE=450000            # how big is the mix?

rm $DEST/*.mp3  # delete the old mix
cp "${KEEP[@]}" "$DEST"   # music i always want in the mix

mapfile -t alist < <(find -L "$SRC" -type f -name '*.mp3' -print0 | xargs -0 shuf -e)
n=0
for song in "${alist[@]}"; do
   let n+=1
   cp "$song" "$DEST"
   size=$(du -c "$DEST" | awk '/total/{print $1}')
   echo $n $size $song
   [[ $size -gt  $MAX_SIZE ]] && break # stop when it's big enough
done
terminal:

Code: Select all

john@waldorf-bv:~$ bash scripts/stickfiller.sh 
1 22320 /home/john/music/Cape Verde - Facebook/Cordas Do Sol - Ehh Boi.mp3
2 80620 /home/john/music/Paris DJs/Paris_DJs_Soundsystem-Celebrating_Joe_Henry.mp3
3 83984 /home/john/music/Anarchy/Thai/Various Thai Rock/ypb02.mp3
4 88204 /home/john/music/CDs/Various (World - 2006) Putumayo Presents Acoustic Africa (va)/07 - Laye Sow Mauritania (Senegal).mp3
5 91604 /home/john/music/singles/Vietnam/HueHuongEnsemble-BaZangBaZeOy.mp3
6 93380 /home/john/music/babeblogue/Baba Zula/06 - San 2 Zortlatmasi.mp3
7 96180 /home/john/music/DCS - Aao Nach Lao/[DP]_Bhangra_Music.mp3
8 100368 /home/john/music/Anarchy/Thai/kikakumono/sure4.mp3
9 118640 /home/john/music/HighLife Haven/One/04 - Ogeli sili obi.mp3
10 121636 /home/john/music/Anarchy/Thai/early_lukthung/p_h_p.mp3
11 128728 /home/john/music/campursari/KecikKecikcampursari.net.mp3
12 136740 /home/john/music/CDs/Bhangra Artists (1991) Bhangra Hit Parade Vol 1 (va)/01 - Track 1.mp3
13 138568 /home/john/music/Anarchy/Thai/early_morlam/chawee.mp3
14 148640 /home/john/music/CDs/Gnawa Diffusion 1999 Bab El Oued Kingston/03 - Kabariou.mp3
15 152956 /home/john/music/Anarchy/Thai/Morlam and Lukthung Greats/jntr14.mp3
16 157768 /home/john/music/babeblogue/Beginner's Guide To Bollywood (3CD)/CD 1 - Vintage Bollywood/14 - Love Story - Kya Gazab Karte Ho Jee.mp3
17 166188 /home/john/music/CDs/Salif Keita - Moffou/03 - Track 3.mp3
18 170388 /home/john/music/Monsoon Country/Cambodia/play_KhmerLer/NekProssJomNab.mp3
19 180452 /home/john/music/The Kinks (2001) BBC Sessions 1964 - 1977/CD2/06 - Money Talks.mp3
20 185504 /home/john/music/Orchestra Baobab - Made In Dakar [2007]/Orchestra Baobab - Aline.mp3
21 193996 /home/john/music/Bekkas Majid - African Gnaoua Blues/02Majid_Bekkas_-_Mrahba.mp3
22 197532 /home/john/music/Anarchy/Thai/Phleng Pua Chiwit/hope01.mp3
23 202268 /home/john/music/lastfm/lastripper/tinar/Geoffrey Oryema/Exile/Makambo.mp3
24 209700 /home/john/music/lastfm/lastripper/tinar/Toumani Diabate's Symmetric Orchestra/Boulevard De L'independance/Mali Sadio.mp3
25 213992 /home/john/music/lastfm/lastripper/tinar/Ali Farka Toure & Toumani Diabate/In the Heart of the Moon/Ai Ga Bani.mp3
26 218896 /home/john/music/CDs/Les Négresses Vertes (1995) Zig Zague/08 - Enfer Et Paradis.mp3
27 222656 /home/john/music/lastfm/lastripper/tinar/Vieux Farka Touré/Vieux Farka Touré/Ma Hine Cocore.mp3
28 229948 /home/john/music/CDs/Orchestra Baobab (2002) Specialist in all styles/04 - Jiin ma jiin ma.mp3
29 238888 /home/john/music/CDs/Salif Keita - Moffou/04 - Track 4.mp3
30 242740 /home/john/music/Anarchy/Thai/Phleng Pua Chiwit/kaimod.mp3
31 245448 /home/john/music/Mambo Kings/07 Melao De Cana (Moo La Lah).mp3
32 251400 /home/john/music/African Head Charge - Songs of Praise/10 My God.mp3
33 253964 /home/john/music/Bally Sagoo - Rising From The East/02._Ek_Ladki.mp3
34 257660 /home/john/music/Monsoon Country/Cambodia/play_KhmerLer/PjerBotChomreang.mp3
35 260740 /home/john/music/CDs/Old Thai-Old Thai pop/old_thai-old_thai_pop-09-track_9.mp3
36 267492 /home/john/music/Khaled & Cheb Mami - 100 Percent Arabica/Khaled & Cheb Mami - 100 Percent Arabica 09 - Laaroubi.mp3
37 274208 /home/john/music/lastfm/lastripper/tinar/Ballake Sissoko/Kora Music From Mali/Kaira.mp3
38 285012 /home/john/music/CDs/Najma - Pukar/Najma-Pukar-06-Track6.mp3
39 303412 /home/john/music/CDs/S. Balachander and others (va)/The_Virtuoso_of_the_Veena_etc.-03-Sufi_musicians-Prelude_to_a_ceremony.mp3
40 307012 /home/john/music/lastfm/lastripper/capev/Elizio/Carpe diem/Hands up (feat. Big Tom).mp3
41 311400 /home/john/music/DCS - Aao Nach Lao/[DP]_Mar_Suteya.mp3
42 323156 /home/john/music/CDs/Moksie Patoe (1998) Greetings From Surinam/05 - Babylon.mp3
43 327048 /home/john/music/Jorge Ben unknown/03 O Amante Vigilante Africano.mp3
44 357248 /home/john/music/singles/Kevin_Ayers_Concert4-5-06.mp3
45 363948 /home/john/music/Khaled & Cheb Mami - 100 Percent Arabica/Khaled & Cheb Mami - 100 Percent Arabica 16 - Les Ombres De La Cite (Heavy Rap).mp3
46 367704 /home/john/music/lastfm/lastripper/tinar/Tarika/Bibiango/Demony.mp3
47 371232 /home/john/music/Feats Don't Fail Me Now/04 Down the Road.mp3
48 377340 /home/john/music/Khaled & Cheb Mami - 100 Percent Arabica/Khaled & Cheb Mami - 100 Percent Arabica 05 - Douha Alia.mp3
49 381080 /home/john/music/CDs/Cape_Verde/12_-_Djurumani_-_Galo_Bedjo.mp3
50 386644 /home/john/music/CDs/Nasida Ria - Keadilan/Nasida_Ria-Keadilan-06-Banyak_Dosa_Bertaubatlah.mp3
51 389900 /home/john/music/Lennon Legend- The Very Best of John Lennon/11 Stand by Me.mp3
52 397628 /home/john/music/Donovan (1966) Sunshine Superman [2005 Remastered]/13 - Superlungs (First Version).mp3
53 401864 /home/john/music/HighLife Haven/One/01 - Builders Brigade Band - apolonia.mp3
54 406996 /home/john/music/Cape Verde - Facebook/Ildo Lobo-MANU.mp3
55 411628 /home/john/music/lastfm/lastripper/tinar/Bonga/O'Melhor De Bonga/Mona Ki Ngi Xica.mp3
56 415784 /home/john/music/Tinariwen_Aman_Aman/pista.mp3
57 431908 /home/john/music/HighLife Haven/One/4 - Ebere egbulam.mp3
58 437372 /home/john/music/CDs/Various (1994) viêt-nam - poésies et chants/04 - yên tuoc tranh ngôn (conversation between the swallow and the sparrow.mp3
59 448844 /home/john/music/CDs/Abdel Ali Slimani (1995) Mraya/09 - Ana Guellile Dub.mp3
60 452220 /home/john/music/Pakistani-Gurus Trilogy - Aghaz/shq.mp3
...I'm definitely going to use this. Thank you!

Re: fill your mp3 player with a random mix

Posted: Wed Mar 26, 2014 2:50 pm
by rhowaldt
^ spaces in filenames on Linux still? you are such a heretic John! use my fclean-script (it is out there, somewhere) to clean up those damn spaces, and make all the shit lowercase in the process. much nicer ;)

Re: fill your mp3 player with a random mix

Posted: Wed Mar 26, 2014 4:36 pm
by wuxmedia
^ I'll try that out, I have 124GB of mp3s to filter the spaces out of. O-o

Re: fill your mp3 player with a random mix

Posted: Wed Mar 26, 2014 4:41 pm
by GekkoP
^ oh yes, I shall (MUST) do the same.

Re: fill your mp3 player with a random mix

Posted: Thu Mar 27, 2014 2:44 am
by johnraff
rhowaldt wrote:^ spaces in filenames on Linux still? you are such a heretic John! use my fclean-script (it is out there, somewhere) to clean up those damn spaces, and make all the shit lowercase in the process. much nicer ;)
Yeh, spaces and odd characters (except /) are all allowed in Unix so I feel we should just cope with them instead of getting puritanical about it. I don't go out of my way to put spaces in the names of files I make myself, and also dislike upper-case, but in albums of music the directory name is often displayed by the player somewhere so there's nothing wrong with a directory called "Feats don't fail me now". Then, in stuff that comes from elsewhere names could be any way you can think of, and I'd just as soon spend a few minutes making sure my script can deal with it than go through all those names sanitizing them.

Re: fill your mp3 player with a random mix

Posted: Thu Mar 27, 2014 9:00 am
by rhowaldt
^ i must say, i am quite a purist when it comes to filenames. plus, remember my id3kill script? i don't jive with the whole 'the player will display the name from the tag' - NO. filename.must.be.proper.

but that is just me :)

Re: fill your mp3 player with a random mix

Posted: Mon Apr 21, 2014 6:12 am
by johnraff
Really enjoying the random mixes. Hearing music I didn't even know was there, and other stuff sounds quite new taken out of its usual context. Image

Re: fill your mp3 player with a random mix

Posted: Sun Oct 26, 2014 3:46 am
by machinebacon
warning: Unix filenames usually don't contain slashes (though pathnames do).
Pidsley's script is working on a correctly set up UNIXoid system, so any removal of spaces and slashes is usually only needed on a system that is owned and used by a dabbler.

Re: fill your mp3 player with a random mix

Posted: Thu Oct 30, 2014 4:20 am
by johnraff
...sigh...

What if the files came from elsewhere originally, like out of an rar archive? Are we obliged to sanitize them before putting in our music directory?

Solid systems shouldn't break over something like spaces or linebreaks. (Of course on Unix forward slashes in filenames are not allowed.)

Re: fill your mp3 player with a random mix

Posted: Thu Oct 30, 2014 5:55 am
by DebianJoe

Code: Select all

kxt snail
I found myself dealing with spaces so often when working with random sources as to make an option for my killxtools to automagically fix them, because they're disgusting.
johnraff wrote:Are we obliged to sanitize them before putting in our music directory?
As opposed to writing a script, which is essentially JUST a shortcut way to remove a few steps from something that we would do often, that must always include checks for edge cases? Sure, sanitize file names.

I can almost assure you that Pidsley does not give a fuck if anyone else ever uses his script, and that he probably doesn't have spaces in his music file names. Thus, if you do, then do whatever you see necessary to fix it and roll on.

If this script was intended for redistribution or production, it might be different, but this is just a 'Hey guys, check out this method that I use to do something useful' script, from what I can tell. I would even go so far as to say that the entirety of the BBQ is just neat proof of concepts over creative ideas, and so essentially, dealing with all user cases stifles creativity in creation. Let the end user modify anything as THEY need to serve their purpose if they're inspired by a concept that is presented. They should learn to fish, rather than being handed a perfectly cooked fish (so to speak).