Results 1 to 7 of 7

Thread: simple bash scripting

  1. #1
    Join Date
    Jan 2005
    Location
    Baltimore, MD, USA
    Beans
    865
    Distro
    Ubuntu Development Release

    simple bash scripting

    Hey all. I can program in C++ and Java, so methods and conditionals and loops come naturally to me, so this is mainly a question of syntax.

    I have a whole bunch of files in a directory, they are ".avi" files. I want to run ffmpeg on each one and convert them to an ".mpg" file. I already have the ffmpeg all set up, but only to take one file at a time. My bash script looks like this so far:

    Code:
    for file do
      echo $file
    #  newname=???
      echo $newname
    done
    all I need to do is take file, which will be something like "movie.avi" and set newname to "movie.mpg" so that I can pass the two variables into my ffmpeg command.

    I think I need sed or awk or grep or something but I dont know.
    Knowledge is half the battle.
    The other half is violence!

  2. #2
    Join Date
    Oct 2004
    Location
    Pennsylvania
    Beans
    1,698

    Re: simple bash scripting

    Quote Originally Posted by Paerez View Post
    Hey all. I can program in C++ and Java, so methods and conditionals and loops come naturally to me, so this is mainly a question of syntax.

    I have a whole bunch of files in a directory, they are ".avi" files. I want to run ffmpeg on each one and convert them to an ".mpg" file. I already have the ffmpeg all set up, but only to take one file at a time. My bash script looks like this so far:

    Code:
    for file do
      echo $file
    #  newname=???
      echo $newname
    done
    all I need to do is take file, which will be something like "movie.avi" and set newname to "movie.mpg" so that I can pass the two variables into my ffmpeg command.

    I think I need sed or awk or grep or something but I dont know.
    Try:
    Code:
    for file in filelist do
       echo "$file"
       newfile = $(echo "$file" | sed 's/.avi$/.mpg$/')
       echo "$newfile"
    done
    Of course, in bash, it might be simpler to do:
    Code:
    for file in filelist do
       echo "$file"
       echo "${file%.avi}.mpg"
    done

  3. #3
    Join Date
    Jan 2005
    Location
    Baltimore, MD, USA
    Beans
    865
    Distro
    Ubuntu Development Release

    Re: simple bash scripting

    thanks a lot.

    I had tried the sed version, but misused the "s and 's so it was coming out weird. I ended up using the second version but modified it to:
    newname="${file%.avi}.mpg"
    Knowledge is half the battle.
    The other half is violence!

  4. #4
    Join Date
    May 2006
    Beans
    268

    Re: simple bash scripting

    Why don't you use Tovid. You only have to type *.avi.

  5. #5
    Join Date
    Jan 2005
    Location
    Baltimore, MD, USA
    Beans
    865
    Distro
    Ubuntu Development Release

    Re: simple bash scripting

    I am using the script for a variety of encodings, like ipod format. And I am comfortable with ffmpeg. And I wanted to learn some bash
    Knowledge is half the battle.
    The other half is violence!

  6. #6
    Join Date
    Apr 2006
    Location
    Austin, TX
    Beans
    2
    Distro
    Ubuntu

    Re: simple bash scripting

    Another potential way to do this if you want to not rely on bashisms is this:

    Code:
    for file in filelist; do
        echo $file
        echo "$(basename $file .avi)".mpeg
    done
    I was also going to show you how to do it with find and xargs, but xargs doesn't allow you to do the manipulations that I need to do with basename.

  7. #7
    Join Date
    Jan 2005
    Location
    Baltimore, MD, USA
    Beans
    865
    Distro
    Ubuntu Development Release

    Re: simple bash scripting

    the problem with basename is that it strips the directory, which I would like to keep. I actually changed it to:
    newname="${file%.???}.ipod.mp4"

    to change any file to an mp4 extension. It works great now. I can pipe output to my script to:
    find . -name '*.avi' | script.sh
    to make it work recursively, and put the new with the original. Sweet.
    Knowledge is half the battle.
    The other half is violence!

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
  •