ffmpeg script to convert all movies in a folder for PSP

Hi all,

I use ffmpeg to convert my movies to play them with my PSP.
I use the terminal version of ffmpeg so i put in the code:

]ffmpeg -i FILENAAM.avi -f psp -r 29.97 -b 512k -ar 24000 -ab 64k -s 320x240 M4V00001.MP4

FILENAAM is the part i replace with the title of the movie i want to see converted.

Now i was wondering if there is a possibility to make it so that i give a command so ffmpeg will start another conversion when the first one is done.
So it can convert one move after the other.
I would like this so i can put on the command and go to bed, or do some shopping and so the movies i want to convert are all converted after i get home again or wake up.

i found a script on the internet that needed a little adjustment but somewhere its wrong, but i cant find where.

Original script:

#!/bin/bash
echo "fakap mp3-to-flv converter http://staff.fakap.net/mp3toflv/"
echo "Copyright (c) mypapit 2007"
echo ""
if [ $# -eq 0 ]
then
    echo "Usage: flvto3gp [flv files] ..."
    exit
fi

while [ $# -ne 0 ]
do
        ffmpeg -i $1 -s 176x144 -vcodec h263 -r 28 -b 96k -ab 64 -acodec libfaac -ac 1 -ar 44100 "$1".3gp
    shift
done
echo "Finished fakaping with flv-to-3gp converter"
echo "\"fakap all those nonsense!\""
echo ""

Adjusted script:

#!/bin/bash
echo "avi-to-MP4 converter"
echo "Original: fakap mp3-to-flv converter http://staff.fakap.net/mp3toflv/"
echo "Copyright (c) mypapit 2007"
echo ""
if [ $# -eq 0 ]
then
    echo "Usage: avitomp4 [mp4 files] ..."
    exit
fi

while [ $# -ne 0 ]
do
        ffmpeg -i $1 -f psp -r 29.97 -b 512k -ar 24000 -ab 64k -s 320x240 "$1".MP4
    shift
done
echo "Finished fakaping with flv-to-3gp converter"
echo "\"fakap all those nonsense!\""
echo ""

who can tell me where i am wrong?

thx

Do you get an error when you run your modified script? If so, please post the error.

i do not get any errors, just terminal that opens and then it closes again before something happens.
so actually what happens is that i run the script, it opens a terminal for a fraction of a second and closes again, without giving any sort of output like error messages, converted files,...)

Are you running it by double clicking the script from your desktop or context-menu->run in terminal from the desktop? That is the only way it will open the terminal and then exit. If that is not the case, post an example on how are you running it.

To run this script. Open a terminal and then run it from the terminal by doing

./yourscript <list of files to convert>

off course that will work if you made it executable by doing

chmod +x <yourscript>

Thanks

with running it as you say, it seems to work.
only small problem i now have is that the output files have double extention (like moviename.avi.mp4)
how can i make it that the .avi part s left out in the filenames of the outputfiles and only have like moviename.mp4?

Add the below line after "do" in your script:

newext=$(echo "$1"|sed  's/\.avi//g')

and change your ffmpeg command to:

ffmpeg -i $1 -f psp -r 29.97 -b 512k -ar 24000 -ab 64k -s 320x240 "$newext".MP4

it works perfect, thx for the help.

Do you know a way that i do not have to type all the names of the movies i want to convert ( <list of files to convert> )
So when i run the script in a certain folder it takes all .avi files that are in that folder to convert them (would save me some work, total is about 200 movies to convert :stuck_out_tongue: )
Now i placed 10 movies in a folder and typed their names in the <list of files to convert> so it starts with those 10, but it would be nice not to have to type all the movie titles before converting.

Is there a way that when you execute the script that before it starts converting you first get the question to restart (r), shutdown (s), leave computer on (l)
so after you press r, s or l the conversion starts and then after all is converted the system shuts down or restart or does nothing?
For this i thing about:

[code]echo "When converting is done would you like to Restart (r), Shot down (s) or leave the computer on (l)"
if [r] restart,
if [s]shutdown
if [l] do nothing

but im not a good scripter, so i know the way i put it here will not work (but i dont know the good way)

Thank you dude2cool,

works as it should.
Converting my movies now

Only question left:
is there a way to make the script so that you dont have to type the names of the moviefiles (let the script take all .avi files automaticly in the folder where the .sh file is in.)

./yourscript *.avi

Or if you really, really want to do that inside the script for some reason, add this as the first line:

set -- /path/to/dir/*.avi

of course, why couldn't i think of that :wall::wall:

thx

now only one question remains, the restart, shutdown, do nothing question.

#!/bin/sh

RESTART=""

while true
do
        case "$RESTART" in
        [RrSslL])
                break;
                ;;
        *)      echo "(R)estart, (S)hut down, (L)eave computer on?"
                read RESTART
                ;;
        esac
done

echo "do stuff"
sleep 10

case "$RESTART" in
        [rR])   echo "Finished.  Rebooting."
                echo sudo /sbin/reboot
                ;;

        [sS])   echo "Finished.  Shutting down."
                echo sudo /sbin/poweroff
                ;;

        [lL])   echo "Finished.  Quitting."
                exit 0
                ;;

        *)      echo "How'd this get here?"
                ;;
esac

thank you for this.
I placed this in the existing script.
When i start the script indeed it asks me to restart, shutdown or Leave computer on.�
When i give in R S or L the script starts converting the movies as it needs to do
after all the converting is done i get following output:

Finished.  Shutting down.
sudo /sbin/poweroff
koen@Koen-PC ~/Downloads/test $

so it says its going to shutdown (or restart or just quit)
it echo's the command to shutdown or restart or exit
but it does not do the work, it comes back to commandprompt

---------- Post updated at 05:35 PM ---------- Previous update was at 05:01 PM ----------

To clarify previous post, this is the used script:

#!/bin/sh
RESTART=""

while true
do
        case "$RESTART" in
        [RrSslL])
                break;
                ;;
        *)      echo "(R)estart, (S)hut down, (L)eave computer on?"
                read RESTART
                ;;
        esac
done

echo "do stuff"
sleep 2


echo "CPSP 1.3"
echo "Converting movies (.avi) to .MP4 for PlayStation Portable."
echo ""
echo "Files must be named as M4V00001.avi (adjust 00001 to 00002, 00003, ... for multiple files."
echo ""
echo "Original: fakap mp3-to-flv converter http://staff.fakap.net/mp3toflv/"
echo "Special thanks to dude2cool and Corona688 @ http://www.unix.com/ for helping to put the script together"
if [ $# -eq 0 ]
then
    echo "Input files: *.avi"
    echo "Output files: *.MP4"
    
fi

while [ $# -ne 0 ]
do
newext=$(echo "$1"|sed  's/\.avi//g')
        ffmpeg -i $1 -f psp -r 29.97 -b 512k -ar 24000 -ab 64k -s 320x240 "$newext".MP4
    shift
done
echo ""
echo "CPSP has finished converting the files."
echo "Thank you for using CPSP 1.3"
echo "bye bye"
echo ""
case "$RESTART" in
        [rR])   echo "Finished.  Rebooting."
                echo sudo /sbin/reboot
                ;;

        [sS])   echo "Finished.  Shutting down."
                echo sudo /sbin/poweroff
                ;;

        [lL])   echo "Finished.  Quitting."
                exit 0
                ;;

        *)      echo "How'd this get here?"
                ;;
esac

This is the output i get:

koen@Koen-PC ~/Downloads/test $ ./CPSP.sh *.avi
(R)estart, (S)hut down, (L)eave computer on?
r
do stuff
CPSP 1.3
Converting movies (.avi) to .MP4 for PlayStation Portable.

Files must be named as M4V00001.avi (adjust 00001 to 00002, 00003, ... for multiple files.

Original: fakap mp3-to-flv converter http://staff.fakap.net/mp3toflv/
Special thanks to dude2cool and Corona688 @ http://www.unix.com/ for helping to put the script together

FFmpeg version 0.6.2-4:0.6.2-1ubuntu1, Copyright (c) 2000-2010 the Libav developers
  built on Mar 22 2011 15:35:22 with gcc 4.5.2
  ...
  ...
  Some info about ffmpeg (cutted to save some space)
  ...
  ...
  libavutil     50.15. 1 / 50.15. 1
  libavcodec    52.72. 2 / 52.72. 2
  libavformat   52.64. 2 / 52.64. 2
  libavdevice   52. 2. 0 / 52. 2. 0
  libavfilter    1.19. 0 /  1.19. 0
  libswscale     0.11. 0 /  0.11. 0
  libpostproc   51. 2. 0 / 51. 2. 0

Input #0, avi, from 'dmd-nda-sample.avi':
  Duration: 00:01:29.71, start: 0.000000, bitrate: 2630 kb/s
    Stream #0.0: Video: mpeg4, yuv420p, 624x352 [PAR 1:1 DAR 39:22], 23.98 tbr, 23.98 tbn, 23.98 tbc
    Stream #0.1: Audio: mp3, 48000 Hz, 2 channels, s16, 128 kb/s
Output #0, psp, to 'dmd-nda-sample.MP4':
  Metadata:
    encoder         : Lavf52.64.2
    Stream #0.0: Video: mpeg4, yuv420p, 320x240 [PAR 117:88 DAR 39:22], q=2-31, 512 kb/s, 2997 tbn, 29.97 tbc
    Stream #0.1: Audio: aac, 24000 Hz, 2 channels, s16, 64 kb/s
Stream mapping:
  Stream #0.0 -> #0.0
  Stream #0.1 -> #0.1
Press [q] to stop encoding
[mpeg4 @ 0x9ff9680]Invalid and inefficient vfw-avi packed B frames detected  
frame=    7 fps=  4 q=2.0 size=      18kB time=0.23 bitrate= 630.2kbits/s dup=1 frame=   17 fps=  8 q=2.0 size=      50kB time=0.57 bitrate= 721.1kbits/s dup=3 frame=   27 fps= 10 q=2.0 size=      72kB time=0.90 bitrate= 653.2kbits/s dup=5 frame=   41 fps= 13 q=2.0 size=      96kB time=1.37 bitrate= 574.1kbits/s dup=8 frame=   54 fps= 14 q=2.0 size=     139kB time=1.80 bitrate= 632.1kbits/s dup=11frame=   76 fps= 18 q=2.0 size=     208kB time=2.54 bitrate= 670.5kbits/s dup=15frame=   86 fps= 18 q=2.0 size=     247kB time=2.87 bitrate= 703.8kbits/s dup=17frame=   96 fps= 18 q=2.5 size=     277kB 
...
...
...
A bunch of converting steps
...
...
...
74 fps= 14 q=14.0 size=    6398kB time=89.22 bitrate= 587.5kbits/s dup=frame= 2689 fps= 14 q=14.9 Lsize=    6478kB time=89.72 bitrate= 591.4kbits/s dup=538 drop=0    
video:5778kB audio:660kB global headers:0kB muxing overhead 0.617032%

CPSP has finished converting the files.
Thank you for using CPSP 1.3
bye bye

Finished.  Rebooting.
sudo /sbin/reboot
koen@Koen-PC ~/Downloads/test $ 

You need to read the code given to you, not just copy it. Look at this line, and see what it dies:

echo sudo /sbin/reboot

...well, I didn't want to reboot my computer after all.

stupid me...
remove the echo's before you try it (me and scripting,...)

Now it gives the command as it should be.

problem is that it asks for the sudo password after the converting (when i supposed to be AFK)
Is there a way to let the script ask the sudo password after the selection to shutdown or restart so you get output like

koen@Koen-PC ~ $ cd Downloads/test
koen@Koen-PC ~/Downloads/test $ ./CPSP.sh *.avi
(R)estart, (S)hut down, (L)eave computer on?
r
Enter sudo password to shutdown or restart after the conversion completes      <== when i choose R or S
password:
do stuff

(fat text is new part, non fat is what i am trying to explain)

i am 100% sure it would be:

echo"Enter sudo password to shutdown or restart after the conversion completes"

but then it must ask for the password and execute the restart/shutdown after the converting is finished

i know i ask alot, but like i said, me and scripting...

i found a way with editing sudoers file with visudo, so it don't need to give the password, but i hope there is a way to have it like described above (asking password after selecting reboot/shutdown and then first run the conversion and after converting doing the shutdown or reboot)

You can't save a password for later that way. sudo, as well as su, ssh, sftp, scp, and most other sane authentication systems, only accept passwords from a terminal, not from a script, as a security feature. There's other options, though.

You could run the entire script under a different user. Not as root -- that's almost never a good idea -- just a different user that sudo won't require a password to /sbin/poweroff with. So you type a password to get into the script and it can do what it needs to from there.

#!/bin/bash

if [ "$USER" != "scriptuser" ]
then
        # Re-load the script under the appropriate user
        exec sudo -u "scruptuser" "$0" "$@"
fi

...

Or you could run a little loop in sudo that waits for /proc/<shell's pid> to stop existing before running a /sbin/poweroff or /sbin/reboot respectively.

if i understand this correct , you mean let the sudo command check if the converting is done like every 5 minutes so it remembers the password when you entter it at the beginning of the script? or do i misunderstand you?

Yes, you understand me. Not that it runs sudo every 5 minutes -- you run sudo once, and the command it runs checks every few minutes...

and even after hours of converting (lots of movies) it still "remembered" the password

that sounds like the solution i am looking for.
gonna try to let it work, ill get back with the feedback (and propably asking for help :stuck_out_tongue: )