LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to burn/write files in a cd-r/rw using bash scripting (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-burn-write-files-in-a-cd-r-rw-using-bash-scripting-550836/)

jaepi 05-02-2007 09:55 PM

how to burn/write files in a cd-r/rw using bash scripting
 
hello there again...i have created a script that will open a hdd then close...i want to add a script that would burn my, let's say, mp3 files from the /home/username directory to the media(cd-r/cd-rw)...sad ly, i don't know how to manipulate these mp3 files, transfer them to the burner, then burn the files...

here's my script as for now:

#!/bin/bash
clear

eject /dev/hdd
sleep 5
eject -t /dev/hdd


if [ "$?" -eq "0" ]; then
nautilus /media/cdrecorder1
sleep 5
eject /dev/hdd
sleep 5
eject -t /dev/hdd
else
exit 1
fi

when nautilus closes, it will pause for 5 secs then eject the medium...

help pls :D

slakmagik 05-02-2007 10:27 PM

The 'eject -T' command I told you about earlier will act as a toggle. Not sure what all the sleeping is about. You could have an interactive prompt, like 'press enter when ready' instead. And, if the script is to burn CDs, you have everything around that, but no *that*. :) Read the man page for cdrecord - it's pretty simple (if your task is simple) but I try to avoid optical media as much as possible. You might check out bashburn rather than reinvent the wheel, unless this is just a learning exercise.

jaepi 05-02-2007 10:46 PM

ohh..uhmmm...sleeping is the time given for the user to place his media on the drive...let's say, for 5 seconds..hehe...im currently viewing the man page for cdrecorder...lol...and again, too complicated for a noob like me, but im trying...this script is a learning exercise at the same time, for lazy person like me...a ready made script for burning audio and data files will be very useful :D

slakmagik 05-02-2007 11:13 PM

right - maybe something like 'read -t30 -n1 -ep "Hit any key when ready..."' - five seconds doesn't seem like much time - this way the script will let you go as fast or slow as you want up to a point, but still not hang the script forever. OTOH, I'm fond of completely non-interactive scripts, so I can see the pros of avoiding a prompt, too. The bashburn script is findable at either freshmeat or sourceforge (or both). Search this site for threads on cdrecord - there are several examples - basically 'cdrecord OPTIONS DEVICE FILE', IIRC.

jaykup 05-06-2007 12:42 AM

Here is a modified version of your script. Feel free to change/delete/add whatever you want.

Its a good way to do what you wanted, and a good thing to play around with.

I've tested everything but the actual cd-burning command.

It may look like a lot, but read through the comments and it may seem very simple.

To test it I commented out the sleep, eject, and just echoed the cdrecord commands

Code:

#!/bin/bash

# Check to see if you supplied an arg, if not
# display usage, then exit with an error
if [ -z "$1" ]
        then
                # Everything between the EOF's will be displayed on the screen
cat <<EOF
Usage: `basename $0` [path_to_mp3's]
EOF
                exit 1
fi

# Sets the directory to a user input
DIRECTORY="$1"

# This should be set incase it changes, you only have to change it here
CDROM="/dev/hdd"

# This gets all the current mp3's in the folder...
# We need this to know which wav's we are deleting.
# Incase there are wav's that were there for some other reason, it
# won't delete them.
ls *.[Mm][Pp]3 | sed 's|.[Mm][Pp]3|.wav|' > CLEANUPFILE

clear

# Open the CDROM, wait 5 seconds and close it
eject $CDROM
sleep 5
eject -t $CDROM

# IF the cdrom sucessfully closes, convert all mp3's to wav's
if [ "$?" -eq "0" ]
        then
                echo "Converting mp3's to wav's..."

                # Convert them into wav's
                cd $DIRECTORY
                TOTAL=`ls *.[Mm][Pp]3 | wc -l`

                # A very bad and crude way of checking if the songs will fit
                # onto the cdrom.  You should check each mp3's length, and
                # compare it to 80 minutes
                # This just takes the amount of mp3's and checks it against 20
                if [ "$TOTAL" -gt 20 ]
                        then
                                echo "there are 20 or more mp3's, they will not fit"
                                exit 1
                fi

                # The main loop for converting

                # Set which track we are converting
                CURRENT=1
                # Run the main loop on all mp3's
                for x in *.[Mm][Pp]3
                do
                        # Change the file name from .mp3 to .wav so "sox" can save it as a .wav
                        FILECHANGE=`echo "$x" | sed 's|.[Mm][Pp]3|.wav|'`
                        # Check if the file already exists, This may be bad if the script errors
                        # and you get a half complete wav file.
                        if [ -e "$FILECHANGE" ]
                                then       
                        echo "Skipping file $CURRENT of $TOTAL, .wav file exists"
                                else
                        echo "Converting file $CURRENT of $TOTAL"
                        # The actual converting program.  Sox should be on all major distros
                        sox "$x" "$FILECHANGE"
                        fi
                        # The counter
                        CURRENT=$(($CURRENT+1))
                done

                echo "Conversion Complete, now trying to burn..."

                # Burn the wav's onto the cd
                # You may want to add other options like speed, tao or dao, etc.
                cdrecord dev $CDROM "$DIRECTORY/*.wav"
                # If cdrecord completes sucessfully, then eject the drive
                if [ $? -eq 0 ]
                        then
                                echo "Burning successful.  Ejecting drive..."
                fi

                # Sleep, and cycle the cdrom
                sleep 5
                eject /dev/hdd
                sleep 5
                eject -t /dev/hdd
        else
                echo "Something went wrong with closing the CDROM"
                exit 1
fi

# remove only the .wav's that we created.
cat CLEANUPFILE | sed "s|'|'\\\''|" | sed "s|^|rm '|" | sed "s|$|'|" | bash

# Removes the CLEANUPFILE, I use a dump to perserve formatting.
rm CLEANUPFILE

exit 0


jaepi 05-06-2007 07:26 PM

Quote:

Originally Posted by jaykup
Here is a modified version of your script. Feel free to change/delete/add whatever you want.

Its a good way to do what you wanted, and a good thing to play around with.

I've tested everything but the actual cd-burning command.

It may look like a lot, but read through the comments and it may seem very simple.

To test it I commented out the sleep, eject, and just echoed the cdrecord commands

Code:

#!/bin/bash

# Check to see if you supplied an arg, if not
# display usage, then exit with an error
if [ -z "$1" ]
        then
                # Everything between the EOF's will be displayed on the screen
cat <<EOF
Usage: `basename $0` [path_to_mp3's]
EOF
                exit 1
fi

# Sets the directory to a user input
DIRECTORY="$1"

# This should be set incase it changes, you only have to change it here
CDROM="/dev/hdd"

# This gets all the current mp3's in the folder...
# We need this to know which wav's we are deleting.
# Incase there are wav's that were there for some other reason, it
# won't delete them.
ls *.[Mm][Pp]3 | sed 's|.[Mm][Pp]3|.wav|' > CLEANUPFILE

clear

# Open the CDROM, wait 5 seconds and close it
eject $CDROM
sleep 5
eject -t $CDROM

# IF the cdrom sucessfully closes, convert all mp3's to wav's
if [ "$?" -eq "0" ]
        then
                echo "Converting mp3's to wav's..."

                # Convert them into wav's
                cd $DIRECTORY
                TOTAL=`ls *.[Mm][Pp]3 | wc -l`

                # A very bad and crude way of checking if the songs will fit
                # onto the cdrom.  You should check each mp3's length, and
                # compare it to 80 minutes
                # This just takes the amount of mp3's and checks it against 20
                if [ "$TOTAL" -gt 20 ]
                        then
                                echo "there are 20 or more mp3's, they will not fit"
                                exit 1
                fi

                # The main loop for converting

                # Set which track we are converting
                CURRENT=1
                # Run the main loop on all mp3's
                for x in *.[Mm][Pp]3
                do
                        # Change the file name from .mp3 to .wav so "sox" can save it as a .wav
                        FILECHANGE=`echo "$x" | sed 's|.[Mm][Pp]3|.wav|'`
                        # Check if the file already exists, This may be bad if the script errors
                        # and you get a half complete wav file.
                        if [ -e "$FILECHANGE" ]
                                then       
                        echo "Skipping file $CURRENT of $TOTAL, .wav file exists"
                                else
                        echo "Converting file $CURRENT of $TOTAL"
                        # The actual converting program.  Sox should be on all major distros
                        sox "$x" "$FILECHANGE"
                        fi
                        # The counter
                        CURRENT=$(($CURRENT+1))
                done

                echo "Conversion Complete, now trying to burn..."

                # Burn the wav's onto the cd
                # You may want to add other options like speed, tao or dao, etc.
                cdrecord dev $CDROM "$DIRECTORY/*.wav"
                # If cdrecord completes sucessfully, then eject the drive
                if [ $? -eq 0 ]
                        then
                                echo "Burning successful.  Ejecting drive..."
                fi

                # Sleep, and cycle the cdrom
                sleep 5
                eject /dev/hdd
                sleep 5
                eject -t /dev/hdd
        else
                echo "Something went wrong with closing the CDROM"
                exit 1
fi

# remove only the .wav's that we created.
cat CLEANUPFILE | sed "s|'|'\\\''|" | sed "s|^|rm '|" | sed "s|$|'|" | bash

# Removes the CLEANUPFILE, I use a dump to perserve formatting.
rm CLEANUPFILE

exit 0


hey, thanks dude...haha...im just a noob, some of the commands here are new to me...oh well..i'll just figure it out...thanks a lot...

jaykup 05-06-2007 07:49 PM

:) Go ahead and read through the comments. And feel free to ask about any command you don't understand.

rkelsen 05-06-2007 07:50 PM

How about this: BashBurn


All times are GMT -5. The time now is 06:17 PM.