Okay, so I finally have my batch file about 90% of where I want it to be, but until I learn how to read media stream information on the command line, it's going to have to do.
I like watching 720p television episodes through my PS3 on my 50" Kuro plasma. However, most all of them are uploaded in an mkv container. For the last couple of weeks, just since I got my connection back and upgraded to watching the 720p episodes over the regular ones, I have been downloading and converting them to m2ts format and renaming them manually. The conversion allows me to serve them using PS3 Media Server without any transcoding and the renaming bit, well, that's just a quirk of mine. I dislike the long file names with all of the extra info.
I got tired of doing it all the long way and one of the purposes of having a computer is to make life easier, isn't it? So, I delved into writing batch files to automate everything. I had done a little bit before, but nothing on the scale that I ended up using; this was definitely a learning experience. What I came up with probably isn't the best way to do it, but it works for my setup. I was having problems with altbinz passing the download directory and collection name, so I ended up just setting the directory in a variable, since that never changes, and writing a routine to read the name of the file, since the collection name doesn't always match the name of the file.
Currently, I think the only issue may be with the creation of the meta file used by tsMuxeR for the conversion. I'm using a generic file, written each time for each different file downloaded. It won't always work, since some files may be encoded using DTS and I don't really want it to take that and convert it to AC3, but as I mentioned before, I don't yet know how to read the encoding information from the command line to tailor the meta file, so it will have to do for now.
If anybody has any suggestions, I am always looking to improve my coding skills. I tried to put enough remarks in the file to explain what each section is doing, but I'll answer questions, also.
Oh yeah, my thanks to boredazfcuk for pointing out that Bulk Rename Utility has a command line version. I've been using the GUI for a long time and love it, but didn't know about BRC.
Update: Okay, so I found a pre-compiled command-line version of MediaInfo for Windows and have figured out how to read the information the I need out of it (well, most of it, anyhow). So, I have updated my batch file. Reading the encoding information comes in handy to determine the frame rate. Not all of the files I am trying to convert have the same frame rate and, for some reason, leaving it blank and having tsMuxeR read it from the stream didn't always work either. So, the batch file now determines the frame rate using MediaInfo and writes it to the meta file (as well as the audio encoding scheme, DTS or AC3). I also discovered that not all of the video files have the video stream as track 1 and the audio as track 2; sometimes they are reversed. That led to the other major change - if the first attempt fails, it swaps the track numbers in the meta file and running a second attempt at conversion.
I think that covers most everything that I updated. I am incredibly satisfied with the file the way it works now. I have tested it with a good dozen files with varying frame rates and mixed track numbers and it has yet to choke on one. There is nothing else I can think to do to improve it, so I think I'm done for now.
And while I didn't get any comments or questions after posting it the first time, those are still welcome.
Update 2: Turns out I left out a rather important line of code, the one that runs tsMuxeR the second time if the first attempt fails. Should work now.
Final update (hopefully): After much research, I finally figured out how to parse a text file and read the information passed from mediainfo. The program now reads the frame rate and video and audio tracks from the file and sets variables for use in the meta file for muxing. I've also discovered that the PS3 will not pass a DTS stream unless it is from an optical disc, so I also added routines to check for a DTS audio track, demux it, convert it to AC3 format and then mux the video and audio into a new file. I've been using the new batch file for a couple of days now and it hasn't choked on any of the mkv files I've thrown at it, so I think it's done (finally).
Hope that someone else out there can make use of it, but I have to say, learning how to do all of this has really helped my batch writing skills (I've learned a lot that I did not know before).
@echo off
setlocal ENABLEEXTENSIONS
rem this batch file converts a downloaded file from an mkv container to m2ts
rem it uses mediainfo command line interface to read video and audio track numbers
rem tsmuxer to demux dts tracks and remux files to m2ts format and
rem eac3to to convert dts tracks to ac3 format
rem set the download directory and change to it
set dldir=C:\temp
cd %dldir%
rem if the file did not unrar correctly, the next command will quit the program
if not exist %dldir%\*.mkv goto :eof
rem the next set of commands finds the name of the file that was downloaded
rem (there should only be one mkv file in the directory)
rem Bulk Rename line that follows will remove spaces from the target filename
"C:\Program Files\brc_64\brc64.exe" /DIR:%dldir% /PATTERN:"*.mkv" /CHANGECASE:L /REPLACECI:" ":. /TRIM /QUIET /EXECUTE
for /f "delims=" %%A in ('forfiles /m *.mkv /c "cmd /c echo @fname"') do set filename=%%A
set filename=%filename:"=%
rem set the remaining variables
set muxdir=C:\Program Files (x86)\tsMuxeR
set eacdir=C:\Program Files (x86)\eac3to
set metafile=%dldir%\%filename%.meta
set media=%dldir%\%filename%.mediainfo.txt
set newmedia=%dldir%\%filename%.media.new.txt
rem need to determine which track is video and which is audio, since they are not
rem always consistent. if the settings are not correct, tsmuxer will fail
c:\mediainfo.cli\mediainfo --Inform %filename%.mkv > %media%
find /i "id " %media% > %newmedia%
rem mediainfo always writes the video track information first, so the first ID will
rem always be that one. setting a counter will let us skip over the video lines to
rem find the audio track number. not interested in anything after that.
set line=1
for /f "tokens=1,2 delims=:" %%G in (%newmedia%) do (
set /a line+=1
if "%%G"=="ID " (
set videotrack=%%H
goto :findaudio)
)
:findaudio
set videotrack=%videotrack: =%
set /a line+=1
for /f "tokens=1,2 delims=: skip=%line%" %%G in (%newmedia%) do (
set /a line+=1
if "%%G"=="ID " (
set audiotrack=%%H
goto :setframes)
)
:setframes
set audiotrack=%audiotrack: =%
for /f "tokens=1,2 delims=:" %%G in (%media%) do (
if "%%G"=="Frame rate " set frames=%%H
)
set frames=%frames:fps=%
set frames=%frames: =%
:checkdts
findstr /I dts %media%
if not errorlevel 1 (goto :dts) else (goto :writemeta)
:dts
rem write meta file demuxing dts track
echo MUXOPT --no-pcr-on-video-pid --new-audio-pes --demux --vbv-len=500 > %metafile%
echo A_DTS, "%dldir%\%filename%.mkv", track=%audiotrack% >> %metafile%
rem demux audio
"%muxdir%\tsMuxeR.exe" "%metafile%" %dldir%
rem get name of dts file
for /f "delims=" %%A in ('forfiles /m *.dts /c "cmd /c echo @fname"') do set dtsfile=%%A
set dtsfile=%dtsfile:"=%
rem check for demuxing error
if not exist %dtsfile%.dts goto :failed
for /f %%A in ("%dldir%\%dtsfile%.dts") do set size=%%~zA
if %size%==0 goto :failed
rem convert dts to ac3
"%eacdir%\eac3to.exe" %dtsfile%.dts %dtsfile%.ac3 -640
rem write new meta file for m2ts container
echo MUXOPT --no-pcr-on-video-pid --new-audio-pes --vbv-len=500 > %metafile%
echo V_MPEG4/ISO/AVC, "%dldir%\%filename%.mkv", insertSEI, contSPS, fps=%frames%, track=%videotrack% >> %metafile%
echo A_AC3, "%dldir%\%dtsfile%.ac3" >> %metafile%
goto :mux
:writemeta
echo MUXOPT --no-pcr-on-video-pid --new-audio-pes --vbv-len=500 > %metafile%
echo V_MPEG4/ISO/AVC, %dldir%\%filename%.mkv, insertSEI, contSPS, fps=%frames%, track=%videotrack% >> %metafile%
echo A_AC3, %dldir%\%filename%.mkv, track=%audiotrack% >> %metafile%
:mux
"%muxdir%\tsMuxeR.exe" %metafile% %dldir%\%filename%.m2ts
rem if conversion was successful, delete downloaded file and clean up
rem otherwise, note the failure, clean up and quit
if not exist %filename%.m2ts goto :failed
for /f %%A IN ("%filename%.m2ts") do set size=%%~zA
if %size%==0 goto :failed
rem remove downloaded file, leaving converted file
del %dldir%\%filename%.mkv
goto :end
:failed
rem delete the failed conversion file and rename the downloaded file to note failure
del %dldir%\%filename%.m2ts
ren %dldir%\%filename%.mkv %filename%.conversion.failed.mkv
:end
rem rename new file to standard format that I prefer
"C:\Program Files\brc_64\brc64.exe" /DIR:%dldir% /PATTERN:"*.m2ts *.avi" /CHANGECASE:L /REPLACECI:s01e:1 /REPLACECI:_:. /REPLACECI:s02e:2 /REPLACECI:s03e:3 /REPLACECI:s04e:4 /REPLACECI:S05E:5 /REPLACECI:s06e:6 /REPLACECI:s07e:7 /REPLACECI:s08e:8 /REPLACECI:s09e:9 /REPLACECI:s10e:10 /REMOVEWORDS:".720p .dvdrip .hddvd .internal .repack .x264 .ws .pdtv .dsr .xvid .hdtv .dts .us .bluray .2010 -hv -runner aaf- -reptile -saints -sfm -2hd -fqm -sys -bajskorv -lol -bia -dimension -orenji -ctu -sitv -immerse -aaf -momentum -fov -ositv -gothic -cinefile" /TRIM /QUIET /EXECUTE
rem move the converted file to a new directory and clean up other files
rem if the conversion fails for some reason, this will still move the file into
rem the new directory, instead of deleting it
move %dldir%\*.m2ts %dldir%\done
move %dldir%\*.mkv %dldir%\done
move %dldir%\*.avi %dldir%\done
del %metafile%
del %media%
del %newmedia%
del %dtsfile%.dts
del %dtsfile%.ac3
del %dtsfile%*.txt
del *.idx
del %dldir%\*.nfo
del %dldir%\*.srr
del %dldir%\*.srs
del %dldir%\*.sfv
del %dldir%\*.nfo
endlocal