Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: ffmpeg bash script question

  1. #1
    Join Date
    Oct 2007
    Beans
    4

    ffmpeg bash script question

    I'm in the early stages of writing a script that loops through all files of the same extension in a directory and converts them to mp4 using ffmpeg. For some reason the loop seems to continue to execute while ffmpeg is converting, causing the first file in the loop to be converted, but none of the other files. I would think that the script would wait with ffmpeg is running, when the first file is done converting it loops to the second file and so on. Any suggestions on what I could change to accomplish this? Thanks!

    Code:
    #!/bin/bash
    
    ext=$1
    enc=$2
    
    if [ $enc = 'xvid' ]
    then
            ls *.$ext | while read file
            do
                    ffmpeg -i "$file" -f mp4 -vcodec xvid -maxrate 1000k -b 700k -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac -ab 192k -s 320x240 -aspect 4:3 "$file".mp4
            done
    else
            echo "Invalid extension"
    fi

  2. #2
    Join Date
    Jun 2007
    Beans
    6

    Re: ffmpeg bash script question

    'What happens if you take the done statement out?

  3. #3
    Join Date
    Oct 2007
    Beans
    4

    Re: ffmpeg bash script question

    When that happens, I get this error:

    Code:
    line 12: syntax error near unexpected token `else'
    line 12: `else'

  4. #4
    Join Date
    May 2007
    Beans
    2,334

    Re: ffmpeg bash script question

    Hi,

    You need to wait for ffmpeg to finish before starting the next, so why not just add a do loop that waits until ffmpeg is finshed, something like:-


    Code:
    #!/bin/bash
    
    ext=$1
    enc=$2
    
    if [ $enc = 'xvid' ]
      then
        ls *.$ext | while read file
        do
          cmd=`ps -A| grep ffmpeg`
          while [  "$cmd" -ne "" ]; do
            sleep 10;
            echo waiting
            cmd=`ps -A| grep ffmpeg`
          done
          ffmpeg -i "$file" -f mp4 -vcodec xvid -maxrate 1000k -b 700k -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac -ab 192k -s 320x240 -aspect 4:3 "$file".mp4         
        done
    else
        echo "Invalid extension"
    fi
    The while loop just waits until ffmpeg ends.

    Regards
    Ian Dobson
    Walking on water and writing software to specification is easy if they're frozen.

    My corner of the internet http://www.planet-ian.com

  5. #5
    Join Date
    Oct 2007
    Beans
    4

    Re: ffmpeg bash script question

    Ian, you read my mind. I was just looking into how to check and see if ffmpeg is running. Let me give this a shot and see how it goes. Thanks!

  6. #6
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: ffmpeg bash script question

    The shell can list the files itself, no need for ls, so I would use a for loop instead of that while loop. And using pidof will make the code checking if ffmpeg is running a little bit shorter.
    Code:
        for file in *.$ext; do
            while pidof /usr/bin/ffmpeg; do sleep 10; done >/dev/null
            ffmpeg -i "$file" -f mp4 -vcodec xvid -maxrate 1000k -b 700k \
                   -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac \
                   -ab 192k -s 320x240 -aspect 4:3 "${file/%$ext/mp4}"
        done

  7. #7
    Join Date
    Feb 2008
    Location
    Watkins Glen, NY
    Beans
    42
    Distro
    Ubuntu Karmic Koala (testing)

    Re: ffmpeg bash script question

    Funny thing is i was looking for a Script to do just that! thank you guys for the help!

  8. #8
    Join Date
    Oct 2007
    Beans
    4

    Re: ffmpeg bash script question

    Sweet. The script seems to be doing what it should now. Not sure if it was changing the ls to a for statement or if adding the >dev/null statement. Either way, thanks for the help!

  9. #9
    Join Date
    Feb 2010
    Beans
    10

    Re: ffmpeg bash script question

    Actually, adding < /dev/null to the ffmpeg command should fix it, as it's probably the problem described on this page: http://mywiki.wooledge.org/BashFAQ/089

  10. #10
    Join Date
    May 2007
    Beans
    2,334

    Re: ffmpeg bash script question

    Quote Originally Posted by phaemon View Post
    Actually, adding < /dev/null to the ffmpeg command should fix it, as it's probably the problem described on this page: http://mywiki.wooledge.org/BashFAQ/089
    Hi,

    What's the point replaying to an almost 2 year old thread? Post count not high enough? or do you just want to premote "Greg's Wiki". And no you don't have to answer that one.

    Regards
    Ian Dobson
    Walking on water and writing software to specification is easy if they're frozen.

    My corner of the internet http://www.planet-ian.com

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •