Search code examples
.netvideovideo-processing

How to convert to m4v


by script or programmatically ? I have a bunch of files that need to be converted wondering if there's a better way than go through the prompts one at a time

OS windows Vista/XP


Solution

  • Assuming that you have a (command-line capable) program already that can convert to m4v and you just want to be able to automate the process, here's a batch file that you could modify to loop through all the files in a directory and its subdirectories and invoke your conversion program. As written, it uses Handbrake to convert a DVD in .iso, .img, or extracted as VIDEO_TS format to mp4 for consumption by an XBox 360. It should be fairly easy to change.

    Just save it as encode.bat or something.

    @echo off
    
    rem Encode DVD for XBOX360 using Handbrake
    rem Anything in %ENCODED_DIR% will not be encoded because everything in there
    rem is assumed to have been encoded already.
    rem
    rem Encodes VIDEO_TS, iso, and img .. renames img to iso
    
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    SET FILE_TYPES=VIDEO_TS *.iso *.img
    SET ENCODED_DIR=[ENCODED]
    SET CONVERT_PROG=[HANDBRAKE]\HandBrakeCLI.exe
    SET CONVERT_ARG_INPUT=-i
    SET CONVERT_ARG_OUTPUT=-o
    SET CONVERT_ARG_SETTINGS=--longest --preset="Xbox 360" --native-language=eng --subtitle-scan
    
    IF NOT EXIST "%ENCODED_DIR%" mkdir "%ENCODED_DIR%"
    
    FOR /F "usebackq delims==" %%i IN (`dir %FILE_TYPES% /s /d /b ^| find /V "%ENCODED_DIR%"`) DO (
        rem trim the trailing slash and we have our output name minus the extension
        SET INPUT_FILENAME=%%i
        SET OUTPUT_FILENAME=%%~pi
        SET BASE_NAME=!OUTPUT_FILENAME:~0,-1!
        SET OUTPUT_FILENAME=!BASE_NAME!.mp4
    
        rem rename .img to .iso so Handbrake recognizes it as a proper input format
        IF /I "%%~xi"==".img" (
            SET INPUT_FILENAME=%%~pi%%~ni.iso
            ren "%%i" "%%~ni.iso"
        )
    
        start "Converting" /BELOWNORMAL /WAIT "%CONVERT_PROG%" %CONVERT_ARG_INPUT% "!INPUT_FILENAME!" %CONVERT_ARG_OUTPUT% "!OUTPUT_FILENAME!" %CONVERT_ARG_SETTINGS% 
    
        echo ERRORLEVEL AFTER CONVERT %ERRORLEVEL% >> last_errorlevel.txt 
    )
    
    ENDLOCAL
    

    So you'll need to modify these variables:

    SET FILE_TYPES=VIDEO_TS *.iso *.img
    SET ENCODED_DIR=[ENCODED]
    SET CONVERT_PROG=[HANDBRAKE]\HandBrakeCLI.exe
    SET CONVERT_ARG_INPUT=-i
    SET CONVERT_ARG_OUTPUT=-o
    SET CONVERT_ARG_SETTINGS=--longest --preset="Xbox 360" --native-language=eng --subtitle-
    

    FILE_TYPES is what you want to use as input format for your converter program. ENCODED_DIR is a directory that you want to skip (you could use it to store files you've already encoded or store them elsewhere). CONVERT_PROG is the directory of your converter. In the example, I have it in a subdirectory called [HANDBRAKE], and it's called HandBrakeCLI.exe. ***CONVERT_ARG_**** are the settings you use to invoke your converter program.

    Just put what you want to convert in a subdirectory off of the script. For example, without changing the script you would rely on this directory structure:

    encode_stuff\encode.bat
    encode_stuff\[ENCODED]\<stuff to skip>
    encode_stuff\[HANDBRAKE]\HandBrakeCLI.exe
    encode_stuff\dvd1\VIDEO_TS\<movie junk>
    encode_stuff\dvd2\my_dvd2.iso
    

    Then when you run the script, it'll create dvd1.mp4 and my_dvd2.mp4.

    So based on this description on how it works, hopefully you can figure out how to modify it even if you don't know too much about cmd shell programming using the batch language. If this answer doesn't help, you should update the original question to include what format you're going from, what playback device you're targeting with m4v, and whether you already have a program that can do the conversion.