Skip to content

Instantly share code, notes, and snippets.

@patwooky
Last active September 26, 2019 19:43
Show Gist options
  • Save patwooky/042282358b03b1508741cd5e362028ef to your computer and use it in GitHub Desktop.
Save patwooky/042282358b03b1508741cd5e362028ef to your computer and use it in GitHub Desktop.
This script creates an mp4 movie from the source movie file. The input movie can be of any format readable by ffmpeg. if DJV viewer is present, it will launch the viewer after the mp4 is created
@echo off
REM ------------------------------------------------------------
REM Written by Patrick Woo patrickwoo@yahoo.com
REM date: 20190927
REM version v002_01
REM This script creates an mp4 movie from the source movie file
REM The input movie can be of any format readable by ffmpeg
REM if DJV viewer is present, it will launch the viewer after the mp4 is created
REM if spaces or non alphanumeric characters are present in the full path,
REM the script will likely fail
REM usage (command line):
REM make_mp4 input_movie.mov
REM or
REM make_mp4 input_movie1.mov input_movie2.avi input_movie3.flv
REM usave (GUI):
REM just drag and drop file(s) onto this script
REM changelog
REM this version tries to allows conversion of multiple files in the arguments list
REM ------------------------------------------------------------
REM echo full file path is %~f1
REM echo %~d1
REM echo %~dp1
REM echo %~n1
REM echo %~nx1
REM echo %~x1
REM setlocal
:: specify location of executables
REM specify your ffmpeg executable here
REM set "ffmp=R:\Pipeline\_bin\ffmpeg.exe"
set "ffmp=H:\patrick\Dropbox\proj\2019\vhq\vhq_pipeline\dos_shell\ffmpeg_bin\ffmpeg.exe"
if exist %ffmp% (
echo ffmp exists at %ffmp%
) else (
echo ffmp does not exist at %ffmp%
echo Terminating
goto MY_END
)
REM specify your djv executable path here
REM set "djvpath=R:\Pipeline\App_VHQ\djv_win64\bin\djv_view.exe"
set djvpath=D:\"Program Files"\DJV\bin\djv_view.exe
if exist %djvpath% (
echo djvpath exists at %djvpath%
) else (
echo djvpath does not exist at %djvpath%
echo Continuing anyway for mp4 conversion
)
REM count the number of args
set argC=0
for %%x in (%*) do set /A argC+=1
echo.
REM check for no arguments
if %argC%==0 goto NO_ARGS
:: set opmode
:: opmode will determine if this is a single file or multi file operation
:: 0 is a single-file operation, 1 is a multi-file operation
set opmode=0
REM if there are more than 1 argument, set opmode to 1
echo %argC% arguments were passed into the script: %*
if %argC% GTR 1 set opmode=1
echo opmode is %opmode%
set convertCounter=0
set overall_error_counter=0
if %opmode%==0 (
REM only 1 file to process, running the conversion subroutine once
REM then try to play the movie
call :sub_ffmp_convert %~f1
:: an error level will be returned, 0 indicates success and 1 indicates otherwise
echo errorlevel is %errorlevel%
) else (
REM multiple files to process, iterating the conversion process
REM and no playing of files after completing the set
REM create a variable to count errors across all iterations.
for %%G in (%*) DO (
REM echo %%A
call :sub_ffmp_convert %%G
REM echo back in iteration control, errorlevel is %errorlevel%
)
echo finally, overall_error_counter is %overall_error_counter%
echo.
echo All files processed
REM if %overall_error_counter%==0 (
REM echo Errors occured in %overall_error_counter% conversions
REM ) else (
REM echo No errors occured in %argC% conversions
REM )
)
goto MY_END
:sub_ffmp_convert
echo sub_ffmp_convert called with arg %~f1
echo.
echo overall_error_counter is %overall_error_counter%
echo --==/ Converting file %convertCounter% \==--
REM echo %~f1
if not exist "%~f1" (
REM if opmode is 0 (single file mode), operation will terminate
REM in multi-file mode, this will skip to the next file
if %opmode%==0 (
REM in single file mode
echo Input file does not exist. Terminating.
goto MY_END
) else (
REM in multi-file mode
echo Input file does not exist. Skipping to the next.
REM exiting the subroutine with error code 1
set /A convertCounter+=1
set /A overall_error_counter+=1
exit /b %overall_error_counter%
)
)
echo Input file exists.
echo.
REM constructs the filename to be created
set outputmp4=%~dp1%~n1.mp4
REM echo %outputmp4%
REM constructs the ffmpeg command to be executed
REM -vf "pad= " will deal with even numbered height pixels and add 1 to pixel count if it
REM cannot be divided by 2
set ffcmd=%ffmp% -i "%~f1" -vf "pad=width=ceil(iw/2)*2:height=ceil(ih/2)*2" -hide_banner %outputmp4%
REM run the command
REM echo %ffcmd%
%ffcmd%
echo.
if %errorlevel%==1 (
REM unsuccessful completion
echo Command completed with error code %errorlevel%.
echo.
if %opmode%==0 (
REM only attempt to play in a single file convert mode
if exist %outputmp4% (
echo %outputmp4% exists - playing movie anyway
echo.
)
)
REM exit with an error code 1
) else (
REM successful completion
echo Conversion completed successfully
echo.
echo mp4 created at:
echo %outputmp4%
echo.
)
if %opmode%==0 (
REM only attempt to play in a single file convert mode
if exist %outputmp4% (
echo.
if exist %djvpath% (
echo launching movie with DJV:
%djvpath% %outputmp4%
) else (
echo.
echo DJV not found on %djvpath%
echo Terminating.
)
)
)
REM increment the convert counter
set /A convertCounter+=1
REM echo errorlevel is %errorlevel%, overall_error_counter is %overall_error_counter%
set /A overall_error_counter+=%errorlevel%
echo overall_error_counter is now %overall_error_counter%
exit /b %overall_error_counter%
:: end sub_ffmp_convert
:NO_ARGS
echo.
echo No arguments supplied. Terminating.
goto MY_END
:MY_END
REM clear variables
set ffmp=
set ffcmd=
set outputmp4=
set djvpath=
set opmode=
set argC=
set convertCounter=
set overall_error_counter=
REM endlocal
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment