Build a video encoder “drop box”

Isn’t Windows Movie Maker amazing? All that video editing fun for free. Unfortunately, it only accepts a handful of file formats (on Windows XP at least), none of which are those used by most video cameras or internet video downloaders. This causes a problem in school as people need to convert the files before they can use them in Movie Maker, and that takes time, effort, and training. Yes, training. Really.

Wouldn’t it be great if there was a folder on the network that people could just drop video files in, and then go to another folder to pick up the WMV version of the same file? Just like that? That would, undoubtedly, be the greatest creation of all time.

I’m sure a pre-made solution already exists. Indeed – Windows Media Encoder goes some way towards it by doing the conversion for you. Sadly, WME lacks support for all sorts of file types, is slow, and doesn’t seem to monitor folders. DropFolders, which works in conjunction with Handbrake, looks like it does exactly what we need, except it won’t export WMV files.

The solution? A combination of the command-line video encoding tool ffmpeg and a free utility called Watch 4 Folder.

On the server, I created two folders: “To Be Converted” and “Converted”. I put the ffmpeg exe on the root of c:\ (although anywhere in your path would work), and created this batch file:

for %%a in ("E:\Path\To Be Converted\*.*") do (
move /Y "%%a" "E:\Path\Converted\%%~na%%~xa"
C:\ffmpeg.exe -i "E:\Path\Converted\%%~na%%~xa" -b 1500kb -vcodec wmv2 -acodec wmav2 -y "E:\Path\Converted\%%~na.wmv"
del /F "E:\Path\%%~na%%~xa"
)

“E:\Path\” being the location of the To Be… and Converted folders, of course. This script basically lists all the files in the drop box folder, and one by one moves them, encodes them to WMV, then deletes the original.

If you want, you can just leave it like this, manually running the batch file on the server each time files are dropped into the To Be Converted folder. Of course, to be automated, you need to add another bit to the process…

Install Watch 4 Folder on the server, and tell it to watch the drop box folder. The “event to monitor” option you want is “File Create”, and the Action is “Execute a program or batch file” – point this at the batch file created above. Click the Start Monitoring button, and that’s it – videos go in, WMVs come out.

There are, however, obvious flaws here. Firstly, if you dump a load of big files in the folder at once, they may start converting before they’ve finished copying. Watch 4 Folder waits until the file has finished copying before triggering the batch file, but the batch file converts all the files once triggered for one. Also, files with the same name but different extensions will cause problems when converting as they’ll overwrite. In addition, things go “a bit wrong” when you put folders in the drop box 🙂 These are things I can live with for now, though.

One partial solution would be to ignore Watch 4 Folder completely, and set the batch file up as a scheduled task on the server to run, say, every 20 minutes. This won’t completely get rid of the problems (and conversions won’t start almost instantly), but it may fit your needs better.

UPDATE 22/09/2010:

Seems most of the problems listed above don’t actually occur in practice. Half-copied files don’t get moved as they’re still open, so they don’t get broken during conversion. So actually, my solution is more ace than it appeared to be at first.

I also forgot to mention that you can, of course, change the file format and bitrate you convert to by passing different parameters to ffmpeg in the third line of the batch file.

UPDATE 01/08/2013:

The time came this week to upgrade the server this solution was running on. We moved over to Server 2008 R2, which is 64 bit, so I decided to run the newest 64 bit version of ffmpeg to compliment it (and it certainly makes conversions quicker too!). I just had a slight issue – ffmpeg has changed its syntax in the intervening three years.

The -b switch now needs you to specify -b:a or -b:v (for audio or video respectively), and 1500kb now needs to be 1500k (no b). To wit, this line was changed:

C:\ffmpeg.exe -i "E:\Path\Converted\%%~na%%~xa" -b:v 1500k -vcodec wmv2 -acodec wmav2 -y "E:\Path\Converted\%%~na.wmv"

Leave a Reply

%d bloggers like this: