I figured I'd post this script I wrote, the OSS community has helped me out many times over the years, I figured I should start giving back.

My brother owns a JVC Everio HDD camcorder that records in .MOD format. This is a type of MPEG2 stream that most DivX-enabled DVD players won't recognize. I created a bash script that uses ffmpeg to convert MOD files into the much more popular AVI format. I hope it helps someone!

To install, copy/paste the code below and with root permissions save it to /usr/local/bin/mod2avi. Change it to the proper owner ('chown root:root /usr/local/bin/mod2avi'), make it executable ('chmod 755 /usr/local/bin/mod2avi') and you're good to go, so long as you have ffmpeg installed already. Type mod2avi for usage information.

For AVI files that nearly match the original quality (and file size) of the MOD files, use 4500k/192 as your audio/video settings. If you plan on posting the videos on YouTube or e-mailing, try 500k/128 or lower values until you find a file size that works for you.

Just as an (insignificant) note: these sort of things should be installed to /usr/local/bin, rather than /usr/bin, according to the FHS. How much this matters is entirely dependant on your system and personal preference.
Regardless of where you put it, you must also chown root:root mod2avi, to make sure it has the correct ownership.

One thing you might want to add is a check to make sure that the target (foo.avi) file does not exist, and if it does, handle it well.
Also, you may wish to hardcode the path to ffmpeg, so that it can't be redirected to another utility. It doesn't matter with this script (in its current form), but it is a good habit to get into.

I think that concept will work, I know I'm missing one critical command though. Where I have 'next' above is likely not what I need, what I want to do is skip the conversion of the file that bash is currently working with, so logically (being a BASIC programmer at heart) I typed 'next'. At least I didn't type 'goto'.

Daenyth, would you or someone else be able to help me with this? I'm sure I could find the answer after doing some more reading, but I learn much quicker by example and hands-on experience.

I also have one other question is regarding the path to ffmpeg. Should I simply enter it as /usr/bin/ffmpeg in the script, or should I use some sort of 'find' command (probably 'which') that finds the proper executable?

I think that concept will work, I know I'm missing one critical command though. Where I have 'next' above is likely not what I need, what I want to do is skip the conversion of the file that bash is currently working with, so logically (being a BASIC programmer at heart) I typed 'next'. At least I didn't type 'goto'.

Using "-f" is perfectly correct here. Rather than "next", you would use "continue". I'll also tweak this

Daenyth, would you or someone else be able to help me with this? I'm sure I could find the answer after doing some more reading, but I learn much quicker by example and hands-on experience.

I'm the same way, hopefully this place helps you out. I would also recommend reading this. I always recommend it to new bash scripters. Some of it you won't need, but it is helpful. You can also google for "advanced bash scripting guide" if you're ready to learn more.

Quote:

I also have one other question is regarding the path to ffmpeg. Should I simply enter it as /usr/bin/ffmpeg in the script, or should I use some sort of 'find' command (probably 'which') that finds the proper executable?

Thanks!

The usual method is to hardcode the path (perhaps as a variable), and let the user modify the script as needed. For example:

I decided to jump in a bit deeper and give this script a few features. I'm actually planning on putting it on a SourceForge page when it's done so it's more accessible. I've noticed there are many people out there that could use this script so I figured I'd get it out there.

I've made the script more configurable and also more along the lines of a traditional conversion script. I'm sure it could use a lot of tweaking though, not to mention a few bug fixes before it's ready for production use. I should add, this is still my first bash script so try to go easy on me, hehehe.

One bug I've identified is the script doesn't handle filenames with spaces, at all. It errors out in the error-checking routine saying it can't read the file. This happens at line 79 and at line 222.

Other than that it seems to work well. I've tested all the options and they all work to my knowledge. I would love if someone could test the script in various instances and provide feedback on it. Don't worry if you don't have any MOD files, the script will work just as well with MPG files.

Here's the full text of the script:

Code:

#!/bin/bash# mod2avi - convert JVC Everio .mod files into XviD .avi files# This software is licensed under the GNU General Public License# For the full text of the GNU GPL, see: http://www.gnu.org/copyleft/gpl.html# No guarantees of any kind are associated with use of this software.# requirements: ffmpeg

mpegonly()# MPEG only function - if called it ignores the rest of the script and simply renames the MOD files to MPG{ if [ "$quietmode" = "0" ]; then echo "$(basename $0) $version $vdate" # Show script name, version and date echo "[] `date`" # Show current date echo "[] Renaming files to .mpg instead of converting..." fi

I decided to jump in a bit deeper and give this script a few features. I'm actually planning on putting it on a SourceForge page when it's done so it's more accessible. I've noticed there are many people out there that could use this script so I figured I'd get it out there.

I've made the script more configurable and also more along the lines of a traditional conversion script. I'm sure it could use a lot of tweaking though, not to mention a few bug fixes before it's ready for production use. I should add, this is still my first bash script so try to go easy on me, hehehe.

One bug I've identified is the script doesn't handle filenames with spaces, at all. It errors out in the error-checking routine saying it can't read the file. This happens at line 79 and at line 222.

Other than that it seems to work well. I've tested all the options and they all work to my knowledge. I would love if someone could test the script in various instances and provide feedback on it. Don't worry if you don't have any MOD files, the script will work just as well with MPG files.

Absolutely fantastic work! Keep it up, you're on to something really good here. Please don't take the following as criticism, because you're doing great!

With the length of the script growing, you might want to start using something other than <code>. Try http://pastebin.ca, as they also offer syntax highlighting. It will also allow easier downloads for people interested in using it, and we can use those to post diffs, so that any tweaks people want to show are easier to find.

One note here that I won't bother changing below: When you are using a literal string in a variable, use ' rather than ", to deny (accidental) variable interpolation. It's mostly a cosmetic/code style thing, but I find that it helps. YMMV

Quote:

Here's the full text of the script:

Code:

#!/bin/bash# mod2avi - convert JVC Everio .mod files into XviD .avi files# This software is licensed under the GNU General Public License# For the full text of the GNU GPL, see: http://www.gnu.org/copyleft/gpl.html# No guarantees of any kind are associated with use of this software.# requirements: ffmpeg

I like to use a quit() function or something similar to handle my exit codes; That was any temp-file cleanup is in one routine, and it decreases code duplication. It also allows you to trap signals (kill, ctrl+c, etc)

Thanks for the positive replies, Daenyth and DarthWavy. I think the script is nearly ready for production use, I can't find any bugs in it at the moment. I've created a SourceForge webpage and posted the current version (v0.5b) at the site: http://mod2avi.sourceforge.net

If anyone has a chance to test it, let me know how it turns out and if there are any bugs or feature suggestions. Thanks again for all the help and suggestions so far!

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum