Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
a "shuffle" for you iPod shuffle (or any portable
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
LGW
n00b
n00b


Joined: 02 Oct 2002
Posts: 60

PostPosted: Tue Feb 15, 2005 1:52 am    Post subject: a "shuffle" for you iPod shuffle (or any portable Reply with quote

OK, after I got my iPod shuffle and getting gnupod-0.87-RC1 to writing files to it, I wanted a simple solution to dig through my mp3 collection, select <512MB of MP3's and move them to the iPod using gnupod-addsong.pl

here's my bash script solution (works in one directory tree only, yet):

* you have to execute the script in your MP3 root - or modify the find command to look at the right position
* you should check the MAXSIZE, it should match your players size (or the size of data you want to store)
* you should change the "copy command" to something that fits your needs. in my case it would be gnupod-addsong.pl $FILENAME

I hope anyone can make use of it - or improve it. I'd like to add some checks, like, only select songs from genre's by reading the ID3 tags - but that might be a job for perl... :)

Code:

#!/bin/bash

# maximum size of files to move to your device
MAXSIZE=512000000

echo "reading files..."

# how to look for data. You can also try different find's here... :)
data=$(find -name \*.mp3 )

# remember to change the "copy file command" below, so the files are really
# copied somewhere usefull
#********************************************************************************

# split the file names
OIFS=$IFS; IFS=$'\n'
script_contents=( $data )
IFS=$OIFS

# shuffle the file names
for element in $(seq 0 $((${#script_contents[*]}-1)))
  do
  RAND=$((RANDOM%${#script_contents[*]}))
  DATA=${script_contents[$element]}
  script_contents[$element]=${script_contents[$RAND]}
  script_contents[$RAND]=$DATA
done

# loop, count
TOTAL=0
COUNT=0
for element in $(seq 0 $((${#script_contents[@]}-1)))
  do
 
  FILENAME=${script_contents[$element]}
  FILESIZE=$(stat -c %s "$FILENAME")
 
  echo "filename: " $FILENAME
  echo "filesize: " $FILESIZE
 
    TOTAL=$((TOTAL + FILESIZE))
    if [ $((TOTAL)) -lt $((MAXSIZE)) ]; then   

   # copy file command -
   # insert your copy command here
   echo "$FILENAME"

    else
   break
    fi
    COUNT=$((COUNT + 1))
done

#some more information
echo "added $COUNT files :)"
echo "added" $((TOTAL-FILESIZE)) "bytes"
Back to top
View user's profile Send private message
LGW
n00b
n00b


Joined: 02 Oct 2002
Posts: 60

PostPosted: Tue Feb 15, 2005 2:22 am    Post subject: Reply with quote

little update: as gnupod_addsong.pl has a little bit of overhead that makes things take even longer, I added some code that calls gnupod_addsong for a whole bunch of files at ones.

Code:

#!/bin/bash

# maximum size of files to move to your device
MAXSIZE=500000000

echo "reading files..."

# how to look for data. You can also try different find's here... :)
data=$(find -name \*.mp3 )

# remember to change the "copy file command" below, so the files are really
# copied somewhere usefull
#********************************************************************************

# split the file names
OIFS=$IFS; IFS=$'\n'
script_contents=( $data )
IFS=$OIFS

# shuffle the file names
for element in $(seq 0 $((${#script_contents[*]}-1)))
  do
  RAND=$((RANDOM%${#script_contents[*]}))
  DATA=${script_contents[$element]}
  script_contents[$element]=${script_contents[$RAND]}
  script_contents[$RAND]=$DATA
done

# loop, count
TOTAL=0
COUNT=0
for element in $(seq 0 $((${#script_contents[@]}-1)))
  do
 
  FILENAME=${script_contents[$element]}
  FILESIZE=$(stat -c %s "$FILENAME")
 
  echo "filename: " $FILENAME
  echo "filesize: " $FILESIZE
 
    TOTAL=$((TOTAL + FILESIZE))
    if [ $((TOTAL)) -lt $((MAXSIZE)) ]; then   
   FILELIST=$(echo -e "$FILELIST\n$FILENAME")
    else
   break
    fi

    if [ $((element%20)) == 19 ]; then
   #echo "$FILELIST"
   # copy file command -
   # insert your copy command here
   #gnupod_addsong.pl $FILELIST
   OIFS=$IFS; IFS=$'\n'
       gnupod_addsong.pl $FILELIST
   IFS=$OIFS
   FILELIST=
    fi
   
    COUNT=$((COUNT + 1))
done


OIFS=$IFS; IFS=$'\n'
    gnupod_addsong.pl $FILELIST
IFS=$OIFS


#some more information
echo "added $COUNT files :)"
echo "added" $((TOTAL-FILESIZE)) "bytes"
Back to top
View user's profile Send private message
LGW
n00b
n00b


Joined: 02 Oct 2002
Posts: 60

PostPosted: Tue Feb 15, 2005 4:57 pm    Post subject: first version in python :) Reply with quote

I switched to python because I want to be able to do some nifty mp3info tricks, and that will be easier in a proper language.

Here you go:
Code:

#! /usr/bin/env python

#pathname
pathname= "/mnt/mp3"

#ipod mount directory
# you have to edit the function addfilestodevice to use this :)
ipod_mount= ""
#ipod_mount= '/mnt/ipod'


# maximum size in byte
maxsize= 60*1024*1024

import os
import os.path
import re
import random

def addfilestodevice(filenames):
   # replace this with a proper system call for your player
   # i.e. "copy file to output dir"
   print "moving",len(filenames),"titles to the ipod, be patient"
   args=[]
   args.append('gnupod_addsong.pl')
   # add this if you did not set IPOD_MOUNTPOINT in the environment
   if ipod_mount != "":
      args.append('-m')
      args.append(ipod_mount)
   args.extend(filenames)
   os.popen2(args)
   os.wait()

def myvisit(list, dirname, names ):
   for i, v in enumerate(names):
      mymatch=re.match('.*\.mp3',v)
      if mymatch :
         data.append(dirname+"/"+v)

# list of files
data= []

# list of selected files
outlist= []

# number of added files
count= 0

# size of already added files
size= 0

print "reading file list"

# OK, get the list of files
os.path.walk(pathname,myvisit,data)

print "shuffle file list"
# now, shuffle them
random.seed()
random.shuffle(data)

print "select files"

for i, v in enumerate(data):
   filedata=os.stat(v)
   if (size+filedata.st_size < maxsize ):
      outlist.append(v)
      size+=filedata.st_size
      
out=[]
for i,v in enumerate(outlist):
   out.append(v)
   if (i%20==19):   
      addfilestodevice(out)
      out=[]
      
if (len(out)):
   addfilestodevice(out)

print "selected",len(outlist),"files"
print "size: ",size
Back to top
View user's profile Send private message
LGW
n00b
n00b


Joined: 02 Oct 2002
Posts: 60

PostPosted: Wed Feb 16, 2005 1:06 am    Post subject: Reply with quote

it's me again. I won't put the whole script in here again, because it's kind of senseless.

So, here's the link to the current script versions:

http://www.dark-reality.de/shuffler/

version 0.0.1 comes with initial mp3 tag support. It's now possible to filter the files by genres (i.e. select only "Metal" and "Ethno"...

You need id3-py but it's in portage :)
Back to top
View user's profile Send private message
Wi1d
Apprentice
Apprentice


Joined: 15 Mar 2004
Posts: 282
Location: USA, Iowa

PostPosted: Wed Feb 16, 2005 11:00 pm    Post subject: Reply with quote

Thanks for sharing your work. Nice script(s).
Back to top
View user's profile Send private message
LGW
n00b
n00b


Joined: 02 Oct 2002
Posts: 60

PostPosted: Wed Feb 16, 2005 11:12 pm    Post subject: Reply with quote

would have been pretty stupid to code for a few hours and only use it myself ;)

patches are welcome :D
Back to top
View user's profile Send private message
Dark Ninja
Tux's lil' helper
Tux's lil' helper


Joined: 31 Jan 2005
Posts: 127

PostPosted: Sun Feb 20, 2005 10:42 pm    Post subject: Reply with quote

You said you got it to work with 0.87-RC1? But, if I go here...

http://blinkenlights.ch/gnupod-dist/stable/

...I don't see it. You didn't mean 0.97, did you?


Thanks for the help, otherwise.
Back to top
View user's profile Send private message
LGW
n00b
n00b


Joined: 02 Oct 2002
Posts: 60

PostPosted: Mon Feb 21, 2005 12:55 am    Post subject: Reply with quote

sorry, both are wrong. It's 0.98-RC1 and you can find it here:

http://blinkenlights.ch/gnupod-dist/testing/gnupod-0.98rc1.tgz
Back to top
View user's profile Send private message
einar
n00b
n00b


Joined: 25 Feb 2005
Posts: 2

PostPosted: Fri Feb 25, 2005 9:50 pm    Post subject: Reply with quote

Can you elaborate on how you got the 0.98-RC1 to work?

So far I've managed to run the "make install", after having to do a few minor hacks to the gnupod_install.pl file. But now, when I try to run:
"gnupod_INIT.pl -m /mnt/ipod"
I get an error saying "Can't locate Unicode/String.pm in @INC ..." where it lists up a path of loads of folders. I did find the file (String.pm) in a folder which is not included in the path. What am I missing?

P.S. I'm not very familiar with perl or it's environment.
Back to top
View user's profile Send private message
einar
n00b
n00b


Joined: 25 Feb 2005
Posts: 2

PostPosted: Fri Feb 25, 2005 11:57 pm    Post subject: Reply with quote

Oh nevermind. I got it to work after emerging a few perl libraries.

Silly me :)
Back to top
View user's profile Send private message
lacipac
n00b
n00b


Joined: 30 Jan 2005
Posts: 5
Location: Budapest/Hungary/Europe

PostPosted: Sat Jul 09, 2005 8:30 pm    Post subject: Reply with quote

Hi,
now i can't reach the server for newer versions, so i downloaded the first one written in python (inserted here).
I'd like to ask why do you use so many enamerate()'s, when it makes no sense. For example:
Code:
for i, v in enumerate(data):
   filedata=os.stat(v)
   if (size+filedata.st_size < maxsize ):
      outlist.append(v)
      size+=filedata.st_size


It could be implied with:
Code:
for v in data:
   filedata=os.stat(v)
   if (size+filedata.st_size < maxsize ):
      outlist.append(v)
      size+=filedata.st_size


, because as far as I see You used i only once in the for cycles.

And one more thing I'd like to note is I think it's better to use mymatch=re.match('.*\.mp3$',v) instead of mymatch=re.match('.*\.mp3',v), because the last one includes foo.mp3.txt also.

on the other hand thanks for it, if you didn't written it I should write one like this, so you saved time for me :)

thanks again,
laca
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum