BashScripts.org
http://bashscripts.org/forum/

mp3 player random auto-filler
http://bashscripts.org/forum/viewtopic.php?f=8&t=379
Page 1 of 1

Author:  ^Albe^ [ Mon Jun 04, 2007 2:00 pm ]
Post subject:  mp3 player random auto-filler

hi to all
first i'd like to thank you for yours support in the chat #channel always great and really kind: in the past yours support help me for automate some simply but usefull process

second: the problem
i have a semi-newbie knowledge, but i'd like to write a script for: take some files (mp3 in this case) randomly from a folder (and its subfolders) and copy they in another one (i dunno if better to mirroring the directory structure or not.. doesn't matter) until the free space reach a desired value (% or size, approx obv: no prob for 10/20 megs difference, and specified in the script ): can u give me some suggestion for what kind of commands i can use for make this script?


let me know, regards :idea:
and sorry for the language :(

Author:  jeo [ Mon Jun 04, 2007 2:25 pm ]
Post subject: 

Hi ^Albe^, welcome to the forums!

Thanks for posting your question here :) Normally I can hack something together, but work is busy today... I've got some ideas that I'll post later

Author:  jeo [ Mon Jun 04, 2007 5:14 pm ]
Post subject: 

Okay, there are many ways for this script to go wrong (it may not handle spaces in filenames very well, etc...) but it should give you a good idea of some of the options that you have. I didn't have any mp3's to play with, so I made some random txt files to test.

Code:
#!/bin/bash

## let's set some variables:
## This is our SOURCE directory

DIR="mp3"


## This is our DESTINATION direction

DIRTOO="mp3too"


## A temporary file to keep our numbered list

TMPFILE="/tmp/randommp3"


## The max size of the resulting directory
## (give or take the size of one mp3)

THRESHOLD="100000"


## The amount of space CURRENTLY used
## in the destination directory

SPACE=$(du -s "$DIRTOO"|awk '{print $1}')


## The number of files that we're working with

FILENUM=$(ls "$DIR"| grep -c 'mp3')


## This generates our numbered list and stores
## it in the tmpfile

find "$DIR" -name '*.mp3'|cat -n > "$TMPFILE"


## this starts a loop which ends when our threshold
## ($THRESHOLD) is reached

until [ "$SPACE" -gt "$THRESHOLD" ]; do


  ## This generates our random number based on
  ## the number of files in the source dir.

  RN="$((RANDOM%${FILENUM}+1))"


  ## This cuts out the file name that corresponds to the
  ## random number

  MP3="$(grep -P "^\s*$RN\t" "$TMPFILE" | awk '{print $2}')"


  ## Here we do the actual copy

  cp -v "$MP3" "$DIRTOO"


  ## Here we check to see how much space we are using
  ## before we start the loop again

  SPACE="$(du -s "$DIRTOO"|awk '{print $1}')"


## End of the loop!

done



I hope this helps! Let us know if you have any questions. If you run into specific problems, we can figure out how to work around them.

Author:  jeo [ Mon Jun 04, 2007 5:16 pm ]
Post subject: 

I wonder if it would be easier to generate a playlist instead of copying the actual mp3s?...

Author:  ^Albe^ [ Tue Jun 05, 2007 1:36 pm ]
Post subject: 

really thx
the playlist is not a good choice beacuse the destination folder is a sd card (/media/Scheda_sd) used in another device
for the space problem i can rename all the files, no prob at all

using an sd memory, perhaps is better to control the freespace instead the folder dimension
and i have a little error during the execution : grep -P option not available (i think that is a good translation of the error)
using this:
Code:
#!/bin/bash

## let's set some variables:
## This is our SOURCE directory

DIR="/media/Hd_Backup/Mp3"


## This is our DESTINATION direction

DIRTOO="/media/Scheda_sd/mp3"


## A temporary file to keep our numbered list

TMPFILE="/tmp/randommp3"


## The max size of the resulting directory
## (give or take the size of one mp3)

THRESHOLD="100000"


## The amount of space CURRENTLY used
## in the destination directory

SPACE=$(du -s "$DIRTOO"|awk '{print $1}')


## The number of files that we're working with

FILENUM=$(ls "$DIR"| grep -c 'mp3')


## This generates our numbered list and stores
## it in the tmpfile

find "$DIR" -name '*.mp3'|cat -n > "$TMPFILE"


## this starts a loop which ends when our threshold
## ($THRESHOLD) is reached

until [ "$SPACE" -gt "$THRESHOLD" ]; do


  ## This generates our random number based on
  ## the number of files in the source dir.

  RN="$((RANDOM%${FILENUM}+1))"


  ## This cuts out the file name that corresponds to the
  ## random number

  MP3="$(grep -P "^\s*$RN\t" "$TMPFILE" | awk '{print $2}')"


  ## Here we do the actual copy

  cp -v "$MP3" "$DIRTOO"


  ## Here we check to see how much space we are using
  ## before we start the loop again

  SPACE="$(du -s "$DIRTOO"|awk '{print $1}')"


## End of the loop!

done


in the meantime, i study the script for try to understand it

Author:  jeo [ Thu Jun 07, 2007 10:15 pm ]
Post subject: 

Interesting... I always seem to use something that isn't universal :(

I was thinking of using awk instead of grep there. May look into it... For the freespace, you could use 'df' to check space of the whole disk.

Author:  ^Albe^ [ Sun Dec 09, 2007 6:35 am ]
Post subject: 

back to the problem after some months
i discovered that the grep -P is not available in the ubuntu distro's :?
so i founded a pcregrep packet that replace the grep -P:
a little renaming (with another batch script) of the space with the _
and:
all ok!!! super!!!
thanks!

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com