process multiple files from one command line??

I’m currently on vacation (in Phuket Thailand (Europe is my nominal place of residence)), and I am searching for a quick and dirty solution to process many hundreds of short video files from my Canon HF S10 camcorder (1920x1080 resolution @ 25 Mb/sec bit rate) to a lower resolution and lower bit rate. I need to do that so I can pass the videos to friends/relatives whose PCs can not handle the high resolution nor high bit rate (my Intel Core i7 PC at home has no problem - but not everyone has a PC to those specs).

For individual files, I find the following will take a sample file “00017.mts” and convert it from 1920x1088 to 1280x720 and also reduce the bit rate from 25MB/sec to 8MB/sec:

ffmpeg -y -i 00017.mts -f avi -vcodec mpeg4 -b 8000000 -acodec ac3 -ab 128000 -s 1280x720 17.avi

The above command works GREAT !! But the problem is I have many many hundreds (thousands actually) of video clips/files and I don’t want to do that manually for every file.

So I created the following simple loop:

for i in *.mts; do ffmpeg -y -i "$i" -f avi -vcodec mpeg4 -b 8000000 -acodec ac3 -ab 128000 -s 1280x720 $i.avi; done

which processes every file in the directory. :slight_smile:

But the problem now is for an input file “00017.mts” the output is “00017**.mts**.avi”.

Is there a way to get rid of the “.mts” in the output file name?

If the shell is bash:

${i/%mts/avi}

will give you the name in $i with the trailing mts changed to avi. The % specifies trailing, without it the first match is used, so you will get a surprise with a name like high-mts.mts.

Oh, and enclose $i and that expression in double quotes so that possible spaces in the filename won’t wreck your script.

xvidenc/h264enc/divxenc can do directory batch encodings (choose as input type “dir”) and since you’re already an xvidenc & h264enc user, I recommend you try it. In batch mode, it will encode all files from the directory you give it with the settings you choose applied to all files :wink:

or just “${i%.*}.avi”

:wink:

Thanks ! Works like magic !! :slight_smile:

There are no spaces in the raw file names produced by my camcorder, but its still good to know.

Thanks !!

My Canon HF S10 camcorder is brand new, and at its top resolution/bit rate, it produces 1920x1080 @ 25MB/sec. I acquired the camcorder only a couple of weeks before my leaving on vacation, and I had a couple of business trips also during those two weeks prior to leaving on vacation. This meant I was not able to test its output files with much software.

I did try the raw .mts files with the h264enc and xvidenc scripts, and for some reason they can not handle the format for encoding. Possibly a Packman packaged mplayer/mencoder limitation? I can only “play” the files with mplayer if I specify the frame rate. ie I can get both audio and video with:

mplayer 00017.mts -fps 25

but

mplayer 00017.mts

does not play any video (only audio plays if fps is not specified).

Some of the h264enc errors look like this … (using: “h264enc -2p -p uhq” ) after the first pass:

Flushing video frames.
Writing index...
Writing header...
ODML: vprp aspect is 16:9.

Video stream:      nan kbit/s  (-2147483648 B/s)  size: 0 bytes  0.000 secs  215 frames

Audio stream:  139.609 kbit/s  (17451 B/s)  size: 8935 bytes  0.512 secs
x264 [info]: final ratefactor: 14.09

… and after the second pass:

Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
==========================================================================

Skipping frame!
VDec: vo config request - 1920 x 1080 (preferred colorspace: Planar YV12)
VDec: using Planar I420 as output csp (no 1)
Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
SwScaler: reducing / aligning filtersize 31 -> 16
SwScaler: reducing / aligning filtersize 31 -> 16
SwScaler: reducing / aligning filtersize 31 -> 14
SwScaler: reducing / aligning filtersize 31 -> 14
[swscaler @ 0x886caa0]Bicubic spline scaler, from yuv420p to yuv420p using MMX2
[swscaler @ 0x886caa0]using n-tap MMX scaler for horizontal luminance scaling
[swscaler @ 0x886caa0]using n-tap MMX scaler for horizontal chrominance scaling
[swscaler @ 0x886caa0]using n-tap MMX scaler for vertical scaling (YV12 like)
[swscaler @ 0x886caa0]1920x1080 -> 1280x720
x264 [info]: using SAR=1/1
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.1 Cache64
x264 [error]: empty stats file
x264_encoder_open failed.
FATAL: Cannot initialize video driver.

Exiting...
-> MEncoder has exited with a non-zero value!
-> Exiting in function: mencoder_exit()

h264enc DOES work with files of lower resolution/bit rate.

My apologies for not mentioning this to you earlier, … But with my upcoming vacation, business trips, and all sorts of things to do on the home/family front, I did not want to “impose” on you by bring it up as I was not clear on my available time to support iterative testing.

Fortunately the packman packaged ffmpeg CAN handle the format.

I probably need to post a sample raw .mts file (from my Camcorder) on the web, for you to download and check out (in case you are curious). It would be neat if your scripts could handle the raw Canon HF S10 video format, but again, I do not want to impose …

yes, samples would be good. I suspect that either your CAM is producing broken/incorrect file or there’s something with mencoder/mplayer going on… you can still force fps with mencoder and see if it’ll encode it then… but samples will be good for me so I can test with them :slight_smile:

To finish this thread (apologies for not posting the code I eventually ended up using) - for a mass conversion to 1280x720 at a reasonably high bit rate, I use the following:

for i in *.mts; do ffmpeg -y -i "$i" -f avi -vcodec mpeg4 -b 8000000 -acodec ac3 -ab 128000 -s 1280x720 2009_06_21_${i/%mts/avi}; done

Typically I run that from a terminal in the directory where the mts files are kept. I typically store both the output avi and the original mts files. … where I change the “2009_06_21_” for each directory … I keep the mts files in directories associated with each day that I take the video.

Just a further update to this thread.

After a company function, in order to share the videos I took, I converted all of my videos to 3 different formats: DVD, SVCD, and VCD compatible. The command lines to do that were all very similar:

  • *for i in .mts; do ffmpeg -y -i “$i” -target pal-dvd 2009_10_08dvd_${i/%mts/mpg}; done
    #dvd
  • *for i in .mts; do ffmpeg -y -i “$i” -target pal-svcd 2009_10_08svcd_${i/%mts/mpg}; done
    #svcd
  • *for i in .mts; do ffmpeg -y -i “$i” -target pal-vcd 2009_10_08vcd_${i/%mts/mpg}; done
    #vcd

The #dvd/svcd/vcd at the end is a comment and should not be included

Just another thankyou to microchip and ken_yap for your hints in this thread.

I was able to use that technique to launch a neat one line job to stabilize many dozens of my home video clips using VirtualDub under wine, with the deshaker plugin. The command line I used is in this thead: openSUSE Forums - View Single Post - KDENlive from Packman?
I would have posted it here, but did not want to be accused of “double posting”.

… Now a weakness of all Linux NLE is they do not have stabilization software. Hence being able to stabilize one’s home videos with a legally free windoze app running under wine is the next best thing. And indeed VirtualDub with deshaker plugin, after some tuning/head-scratching, now works well for the limited set of codecs I have set it up for.

My Intel Core i7 920 has never worked so hard, as my % CPU is running around 300% as it is very busy stabilizing many video clips.