Saturday, August 30, 2014

Setting default audio & subtitle in MKV files using a batch file.

Matroska (mkv) files are video files that can contain multiple tracks, allowing for multiple subtitles and audios, and one can set a default audio & subtitle for those.

mkvtoolnix is a collection of tools (mkvmerge, mkvmerge gui [mmg], mkvinfo, mkvextract, and mkvpropedit) for matroska (mkv) files.

The batch file below allows one to view the default flag for the tracks in a mkv file and then toggle the default flag on/off. Copy the text block below and save it to a batch file, say MKV.BAT and put that bat file somewhere in your path (e.g. C:\WINDOWS).

@echo off
if '%1==' goto ?
::change code page to utf8 (mkvinfo always writes utf8 when redirected)
set track=
set mkvtoolnix=%ProgramFiles%\mkvtoolnix
%windir%\system32\chcp 65001>nul
"%mkvtoolnix%\mkvinfo.exe" %1>%temp%\mkvinfo.txt
if '%2==' goto info

set flag=unknown
for /f "tokens=3-9,11*" %%a in (%temp%\mkvinfo.txt) do call :flag %2 %%a %%b %%c %%d %%e %%f %%g %%h %%i
echo Track %2 (%name% %type%) default flag is %flag%
if %flag%==1 goto unset
echo Now setting flag to 1
"%mkvtoolnix%\mkvpropedit.exe" %1 --edit track:%2 --set flag-default=1
"%mkvtoolnix%\mkvinfo.exe" %1>%temp%\mkvinfo.txt
goto info

:unset
echo Now setting flag to 0
"%mkvtoolnix%\mkvpropedit.exe" %1 --edit track:%2 --set flag-default=0
"%mkvtoolnix%\mkvinfo.exe" %1>%temp%\mkvinfo.txt

:info
for /f "tokens=3-9,11*" %%a in (%temp%\mkvinfo.txt) do call :filter %%a %%b %%c %%d %%e %%f %%g %%h %%i
if not "%track%"=="" echo %track%
goto :eof

:filter
if '%2==' goto :eof
for %%v in (Content A Pixel Display (size: Lacing CodecPrivate size head Timecode Muxing Writing Segment Sampling Channels version: read maximum type type: Duration: Date: MinCache: Video Audio Algorithm: Max Order: Scope: Type: Interlaced: Enabled: Forced) do if %1==%%v goto :eof
for %%v in (EbmlVoid Chapters EditionEntry EditionFlagHidden: EditionFlagDefault: EditionUID: ChapterAtom ChapterUID: ChapterTimeStart: ChapterFlagHidden: ChapterFlagEnabled: ChapterDisplay ChapterString: ChapterLanguage: Cluster) do if %1==%%v goto :eof
for %%v in (UID: duration: decode) do if %2==%%v goto :eof
if %2==number: goto :track
if "%track%"=="" goto :eof
if %2==type: (set track=%track% %3) & goto :eof
if %2==ID: (set track=%track%, %3) & goto :eof
set track=%track%, %*
goto :eof

:track
if not "%track%"=="" echo %track%
if %3 LSS 10 (set track=Track %3: ) else set track=Track %3:
goto :eof

:flag
if '%3==' goto :eof
if %3==number: if '%1=='%4 (set track=1) else set track=
if '%track%==' goto :eof
if %2==Name: set name=%3
if %2==Default if %3==flag: set flag=%4
if %2==Track if %3==type: set type=%4
goto :eof

:?
echo MKV mkvfile [track number]
echo.
echo If track number not specified, the tracks will be listed
echo.
echo If track number specified, the default flag will be toggled for that track.

You will need to set the path to mkvtoolnix appropriately (edit the line set mkvtoolnix=...) to point to the mkvtoolnix folder.

Remember if you set a default flag on for one audio track, remember to unset the default flag for any other audio track that had the default flag on.

Monday, July 14, 2014

Compiling swftools on Windows

Compiling swftools on Windows...

1. Download swftools-master.zip

2. Download mingw-get-setup.exe from www.mingw.org, run it and install base, msys & c++ to C:\MINGW

3. Download from http://gnuwin32.sourceforge.net/packages.html and install to C:\MINGW\GNUWIN32:
bison-2.4.1-setup.exe
flex-2.5.4a-1.exe
freetype-2.3.5-1-setup.exe
gettext-0.14.4.exe
giflib-4.1.4-1.exe
jpeg-6b-4.exe
libgw32c-0.4.exe
libiconv-1.9.2-1.exe
libpng-1.2.37-setup.exe
pdflib-lite-6.0.2.exe
t1lib-5.1.0.exe
yasm-1.1.0-win32.exe
zlib-1.2.3.exe

4. Download and compile lame

5. Download  fontconfig-2.2.2-20040412.zip,   fontconfig-dev-2.2.2-20040412.zip
    and extract to c:\mingw\fontconfig

6. Create C:\MinGW\msys\1.0\etc\fstab and put the following lines in:
c:\mingw /mingw
c:\mingw\gnuwin32 /gw32
c:\mingw\fontconfig /fc

7. Extract swftools to C:\MinGW\msys\1.0\home\

8. Run msys
cd ..
cd swftools
.\configure
make

8. If gcc complains about ..
- #define boolean int  in config.h -- just comment out that line
- not found -- copy lib\pdf\aconf.h to lib\pdf\xpdf directory and edit the line 
   #include "../../config.h" in xpdf\aconf.h into #include "../../../config.h"
- "cmyk.h" not found -- edit GfxState.cc and change "cmyk.h" to "../cmyk.h"
extern "C" int unlink(char *filename); definition conflict in stream.cc -- comment out that line

9. To compile gfx2gfx (exampel usage: gfx2gfx infile.swf -o outfile.pdf)
- edit swftools\src\gfx2gfx.c and insert the line       out->setparameter(out, "maxdpi", "0");
   below the line           gfxdevice_pdf_init(out);
- edit swftools\src\makefile and append gfx2gfx$(E) to the end of the line
   programs = $(install_programs) swfbytes$(E) ttftool$(E)
- run make again

note: pdf2pdf also accepts swf input and sends to pdf output

References: 
http://lists.gnu.org/archive/html/swftools-common/2005-02/msg00023.html
http://sudofixit.blogspot.com/2014/02/guide-convert-book-style-swfs-to-pdfs.html
http://go.kblog.us/2009/08/mingw-to-compile-lame-for-windows.html

Other:
If you want to build pdflib lite, and get
1) DWORD errors: add to pc_util.c
    typedef unsigned int DWORD;
2) png dereferencing errors: change #include "png.h" in p_image.h to
   #include "..\png\png.h"
(this error is caused by gcc reading a different (newer) version of libpng that may be installed elsewhere)
and similarly search and replace #include "tiffio.h" to #include "..\tiff\tiffio.h"
and #include "zlib.h" to #include "..\flate\zlib.h"