Skip to content

Instantly share code, notes, and snippets.

@jamietre
Last active September 16, 2023 01:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jamietre/9776ec821ee2d410989ff89bddd6571b to your computer and use it in GitHub Desktop.
Save jamietre/9776ec821ee2d410989ff89bddd6571b to your computer and use it in GitHub Desktop.
Batch file for transcoding MVC 3D streams in an MKV container
@ECHO OFF
rem usage: transcodemvc input output [-r] [-s]
rem -r will re-use a previously extracted elementary stream from the temp file location
rem -s will swap eyes
set TEMP_DIR=c:\temp\recodemvc
set TEMP_FILE=%TEMP_DIR%\temp_mvc.264
set PROFILE=high
set SWAP=false
rem 1 = slowest/best, 7 = fastest/worst
set USAGE=3
set MIN_BITRATE=20000
set MAX_BITRATE=35000
rem parse args
set INFILE=%1
set OUTFILE=%2
SHIFT & SHIFT
SET REUSE=
:loop
IF "%1" neq "" (
IF "%1" == "-r" (
SET REUSE=1
SHIFT
GOTO :loop
) ELSE IF "%1" == "-s" (
SET SWAP=1
SHIFT
GOTO :loop
) ELSE (
SHIFT
GOTO :loop
)
)
if not exist %INFILE% (
echo "input file doesn't exist"
goto :finished
)
if exist %OUTFILE% (
echo "output file already exists; delete it first"
goto :finished
)
echo profile=%PROFILE%; USAGE=%USAGE%; MIN_BITRATE=%MIN_BITRATE%; MAX_BITRATE=%MAX_BITRATE%
if "%REUSE%" == "1" (
echo reusing cached elementary stream
goto :reuse
)
set TEMP_LEFT=%TEMP_FILE%.left.264
set TEMP_RIGHT=%TEMP_FILE%.right.264
if not exist %TEMP_DIR% mkdir "%TEMP_DIR%"
if exist %TEMP_FILE% del "%TEMP_FILE%" >nul 2>&1
if exist %TEMP_LEFT% del "%TEMP_LEFT%" >nul 2>&1
if exist %TEMP_RIGHT% del "%TEMP_RIGHT%" >nul 2>&1
if exist %TEMP_FILE%.created del "%TEMP_FILE%.created" >nul 2>&1
echo -- Extract elementary stream from mkvextract
echo.
mkvextract -f tracks %INFILE% 0:"%TEMP_FILE%" --fullraw || goto :error
echo . >%TEMP_FILE%.created
echo Temporary file created; you can use "-r" option to start from this point.
:reuse
if not exist %TEMP_FILE% (
echo There's no temporary file "%TEMP_FILE%"... did you use -R argument erroneously?
goto :finished
)
set ADDL_OPTS=
if "%SWAP%" == "1" (
set ADDL_OPTS=-swaplr
)
echo -- Begin transcoding
FRIMDecode64 -i:mvc "%TEMP_FILE%" -tab -o - | FRIMEncode64 -tab 2 -i - -o:mvc "%TEMP_FILE%.recoded" -w 1920 -h 1080 -sw -profile %PROFILE% -level 4.1 -vbr %MIN_BITRATE% %MAX_BITRATE% -cpbsize 3372 -l 6 -gop 24 4 0 O -maxdpb 4 -u %USAGE% %ADDL_OPTS%
if %errorlevel% neq 0 exit /b %errorlevel%
rem Original implementation: can't swap eyes, as far as I know
rem FrimTranscode64 -i:mvc "%TEMP_FILE%" -o:mvc "%TEMP_FILE%.recoded" -sw -profile %PROFILE% -level 4.1 -vbr 20000 35000 -cpbsize 3372 -l 6 -gop 24 4 0 O -maxdpb 4 -u %USAGE%
mkvmerge -o %OUTFILE% "%TEMP_FILE%.recoded" --no-video %INFILE% || goto :error
goto :finished
:error
echo Failed with error %errorlevel%
exit /b %errorlevel%
:finished
echo Success! Created "%OUTFILE%"
@jamietre
Copy link
Author

jamietre commented Jun 29, 2017

A simple script to transcode 3D MVC MKV video files for the purpose of shrinking them.

BD3D2MK3D is much easier and configurable, generally, but it can't output video as a frame-packed MVC format stream. If you have hardware that can play MVC files you probably would much rather have video in that format, since you can also play it as 2D-only (unlike SBS) and of course you get full HD.

This is a quick and dirty DOS batch file. Requires in your path:

To use:

transcodemvc <inputfile> <outputfile> [-r] [-s]

-r will reuse temporary files created previously; useful if something fails after the setup
-s will swap left & right eyes; use this to fix right-eye-first videos.

What does it do:

  • extract first track (main video) from input file
  • recode to MVC using specified h.264 options in the script
  • merge recoded video with all non-video tracks from original input file

With any luck you'll end up with the same video, recompressed/shrunk and possible with eyes swapped.

Caveats:

  • Works only on Windows
  • h.264 settings hardcoded (adjust to your taste)
  • If you have an Intel CPU might want to try removing -sw switch
  • Someone with more time than me should enhance this idea to do things like batch process directories, validate input with ffprobe, etc.
  • "Works for me"

@jamietre
Copy link
Author

jamietre commented Jul 9, 2017

Version 1.27 of FrimEncoder released yesterday (after 18 months!)

@jamietre
Copy link
Author

Updated script to use FRIMEncode and FRIMDecode instead of FRIMTranscode so we can use the -swaplr option

@kadrim
Copy link

kadrim commented Mar 13, 2021

great, works for me too :-)

@kadrim
Copy link

kadrim commented Mar 13, 2021

BTW: using this on a very old Samsung UE6100 (2012) in combination with a equally old Bluray 3D player from samsung. just have to remux the files to m2ts with

  1. h264
  2. mvc
  3. audio or whatever

the order for the streams seems to be important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment