Autofill iPod Shuffle with a certain amount of megabytes of music
I have a first generation iPod Shuffle (yes, I know it's old) with 512 megabytes of space (feel free to laugh). I also have arch linux installed on my computer, with a music folder containing about 30 folders full of music. This totals to about 2 gigabytes of music.
What I want to do is: create a script that moves no more than 512 megabytes (it can be slightly under this amount, but NOT over) of random songs from my music folder (and it's subsequent artist/album folders) to a separate folder. I will then use gtkpod to move those random songs in the new folder to my iPod Shuffle. The problem is, I have no clue how to do this. Help is appreciated, either in suggesting a linux command that might do this or writing out a whole script. Either way, I would be VERY satisfied ;) |
Dunno why, but I found this an interesting challenge. So much so that I actually went and wrote a whole script for it. You'll have to modify it as necessary for your needs. It's fully commented so it should be rather self-explanatory.
Code:
#!/bin/bash Edit: Hold on, just discovered a fairly big mistake. It won't properly handle duplicate entries. I'll post the revised version when I'm done. Edit2: I've updated the script to be able to handle duplicates. At least I hope. I'd appreciate having any mistakes pointed out. Edit3: Fixed a couple more things. It was still outputting duplicates, until I quoted the variables in the test. Now it seems to be working ok, but it's hard to test it when the input is random. Edit4: Corrected another bug. See my follow-up post below for details. |
I modified it for my specific folders, AND IT WORKED!
Thank you very much. This happens to be (obviously) exactly what I was looking for! You have inspired me to try to figure out how to do this in python (the only programming language I have at least an intermediate grasp on), and I will post the results of that whenever I figure it out. |
Glad to help out. I rather enjoy working out scripting challenges like this. I really just knocked it out fairly quickly though. I'm sure it could be improved with some thought.
I don't know much about python myself, but I'm sure the basic pattern of the script should be translatable. Looking forward to seeing what you come up with. |
Eyballing my script again, I just noticed another small bug. This line needs to be changed:
Code:
#get total number of files Code:
tnum=${#filelist[@]} I've edited the original post to reflect this fix. |
I decided to make one more modification to the script, and this time I'll post the whole thing again.
I got to thinking about how there often seems to be 4-5MB of wasted space at the end of the cycle, so I've added a routine to try to fill that space in. It's really just a test that forces it to iterate a few more times before giving up. If the space is large enough then it might just find one last track to squeeze into it. In my tests it seems to reduce the wasted space to about 2MB or so usually. You can adjust how many times you want it to keep trying. Code:
#!/bin/bash |
Thank you very much for the script and all of the nice modifications. I unfortunately gave up on the python script, as it was just basically going to use "os.system(command)" to run all of the commands in your script. This would make it fairly pointless...
|
All times are GMT -5. The time now is 01:28 PM. |