Log in

View Full Version : Automatic batch x264 encoding?


cafevincent
29th September 2011, 15:24
I noticed that the main bottleneck for me encoding videos with ripbot264 is that it's a lot of work to add the jobs one by one. Is anyone aware of a way to crop & encode all .m2ts videos (and their soundtracks) in a folder automatically using a set of predefined preferences? Does something like that exist? I wouldn't mind a batch script either, in fact I might be able to put together something like that myself if I wasn't such a newbie to encoding tools. Suggestions?

smok3
29th September 2011, 20:13
on windows i basically do it like this:

a. generate avisynth template and butcher that until it looks good
b. apply that template to all the selected files (my center app is total commander with all bunch of bat files that are either patched to buttons or to meni)
c. select all generated avs scripts and send them to x264 (again a batch loop script)
gui snap http://imagebin.org/176753

i can dig out a minimal working example, if that sounds like what you need.

----------

on osx i'am currently trying to get similar workflow http://forum.doom9.org/showthread.php?p=1528488 (without avisynth...)

MeteorRain
30th September 2011, 19:29
i'd rather choose to use makefile to build everything in one script with automatically target file generating and can fully avoid overwriting files that have already built.

and is almost compatiable both under windows and linux-like system

smok3
30th September 2011, 19:35
MeteorRain, can you explain the targeted workflow and especially its flexiblity/stability?

cafevincent
1st October 2011, 09:01
I'm looking for a one click per folder tree solution so I started to build a script. So far I found a way to demux all the ts/m2ts files (one by one), encode the video and remux the streams to mkv but how do I get some kind of autocropping from command line?

smok3
1st October 2011, 13:22
some sort of mplayer script comes to mind (autocroping),
http://lists.mplayerhq.hu/pipermail/mplayer-users/2005-November/056636.html
(sh at the bottom)

cafevincent
1st October 2011, 18:37
I have a bare script that works except I'm having trouble muxing the streams. MKVmerge only puts in the video track no matter what I try.

cafevincent
1st October 2011, 19:29
This is it. It's highly experimental so don't run it. The last line is my problem and the way it appears here seems to be way off. I have no idea how to put in all the audio and subtitle tracks. I also welcome comments/suggestions on the rest of the code.

if "%1"=="/go" goto work
if exist "%1" if not exist "%1\*" exit
if not exist "%1" exit

echo. >%temp%\joblist.cmd
for /r "%1" %%x in (*.m2ts) do echo call convert /go "%%x" >>%temp%\joblist.cmd
for /r "%1" %%x in (*.ts) do echo call convert /go "%%x" >>%temp%\joblist.cmd
%temp%\joblist
exit

:work
set source=%2
set source=%source:~1,-1%
for /r "%programfiles%" %%x in (tsdemux.exe) do "%%x" -o "%temp%" "%source%"
if "%source:~-4%"=="m2ts" set file=%source:~0,-5%
if "%source:~-4%"==".ts" set file=%source:~0,-3%

for /r "%programfiles%" %%x in (x264_x64.exe) do "%%x" --level 4.1 --aud --nal-hrd vbr --vbv-bufsize 25000 --vbv-maxrate 25000 --filter -1,-1 --ref 3 --bframes 3 --b-adapt 2 --b-pyramid none --subme 10 --aq-mode 1 --trellis 2 --partitions all --me tesa -o "%temp%\%file%.mkv" "%temp\%file%*.264"

for /r "%programfiles%" %%x in (mkvmerge.exe) do "%%x" -o "d:\videos\%name%.mkv" "%temp%\%name%.track*.*"

Asmodian
2nd October 2011, 05:26
This is the way I would do it, I used three batch files because of the way variables work in for loops (evaluated once at the start).
I have tested this but not on a full directory tree.

I assume tsdemux, x264, and mkvmerge are in the path, just add the full locations if not.

I also assume you only have m2ts or ts files in the tree to start (no mp4, mkv etc.). I use .mp4 for the output from x264 so that even if you set the output location inside the tree it doesn't go recursive. Due to the oddity that tsdemux accepts -o "DIRECTORY" as a file name but -o DIRECTORY as a directory you cannot have spaces in any of the directory names.

Main .cmd file, run this one with the top of the directory tree you want to compress as the command line parameter:
@if not exist %1\* goto end

for /r %1 %%a in (*.m2ts *.ts) do "tsdemux.exe" -o %%~dpa "%%a"

for /r %1 %%a in (*.h264 *.vc1) do "x264.exe" --preset ultrafast --crf 26 -o "%%~dpna.mp4" "%%a"

for /r %1 %%a in (*.mp4) do call mkvmerge.cmd "%%~dpa" "%%a" "%%~na.*.ac3" "%%~na.*.sup"

:end

This is mkvmerge.cmd
@SET FILELIST=
for /r %1 %%a in (%3 %4 %5 %6 %7 %8 %9) do call setfilelist.cmd %%a
"mkvmerge.exe" -o "D:\videos\%~n2.mkv" %2 %FILELIST%
@SET FILELIST=

This is setfilelist.cmd
@SET FILELIST=%FILELIST% %1

If you want other file types muxed in at the last stage just add them to the end of the last line of the main .cmd the same way ac3 and sup are ("%%~na.*.[EXTENSION]"). Because batch only allows nine command line parameters you can only set seven extensions. It is fine if there aren't any files with a set extension.

mkvmerge.cmd outputs all final files to D:\videos\.

Obviously change the x264 settings.

cafevincent
2nd October 2011, 16:01
Nice, thank you! I am trying it now. I also just realized it needs at least as much free space as the size of the tree, nearly 600gb and I have 13gb. I can't figure out a fix until I sober up.

Asmodian
2nd October 2011, 22:41
Yeah, it will take a lot of free space.

It will need even more than the size of the tree; the x264 compressed versions as well. I thought about adding auto delete of intermediate files but figured auto-delete can backfire way to easily.

It could be reordered to demux, compress, remux one file at a time but it would be a more complex set of batch files.

J_Darnley
3rd October 2011, 00:46
Just to point this out, you can do more than one command in a for loop, even with indentation:
FOR %A IN (SET) DO (
command 1
command 2
)

smok3
3rd October 2011, 08:00
Yeah, it will take a lot of free space.

It will need even more than the size of the tree; the x264 compressed versions as well. I thought about adding auto delete of intermediate files but figured auto-delete can backfire way to easily.

It could be reordered to demux, compress, remux one file at a time but it would be a more complex set of batch files.

why? just make two bat files, one is action that expects one set of files as input, the other is loop, example:

loop - ffmpeg_mux_loop.bat (expects filelist with newline termination, which is exactly what total commander provides)
@ECHO OFF
for /f "delims=+++++" %%i in (%1) DO t:\path\ffmpeg_mux.bat "%%i"

action - ffmpeg_mux.bat (expects one file as input, which is nice also just to check it from command line)
@echo off
:: check if the file extension is avi otherwise quit
:: %~x1 Expands %1 to a file extension.

echo %~x1
if /i %~x1 NEQ .avi goto :eof

:: check if there is a file with wav extension and same path/name

if exist %~dp1%~n1.wav (echo wav found) ELSE goto :eof

:: get the full path of the avi file without extension
echo full path without ext is %~dp1%~n1

:: if the muxed file allready exists goto eof

if exist %~dp1%~n1_mux.avi goto :eof

:: ffmpeg mux
@echo on
ffmpeg -i %1 -i %~dp1%~n1.wav -acodec copy -vcodec copy %~dp1%~n1_mux.avi

so you can basically write a very smart loop and reuse it for whatever project later and a decently simple action.

p.s. for bat collectors : http://imagebin.org/177228

Asmodian
3rd October 2011, 09:18
Interesting, thanks.

I already figured out a single file solution, this one also muxes and deletes as it goes so it will need a lot less free space. It leaves the final .mkv in the directory tree. That should be easy to modify but it is safer in case some files have the same name.

I actually tested this with a real directory tree and a few m2ts with 3x video and 2x audio tracks. It compresses all the video tracks and muxes everything into one mkv.

EDIT: Updated, errors with quotes fixed - tested with spaces in directory & file names. :o
@ECHO OFF
if not exist %1 goto eof
SET FILELIST=

for /r %1 %%g in ("*.m2ts" "*.ts") do (
cd "%%~pg"
call :tsdemux "%%g"
call :mkvmerge "%%~pg" "%%g" "%%~ng.track*.mkv" "%%~ng.track*.ac3" "%%~ng.track*.dts" "%%~ng.track*.sup"
)
GOTO eof

:tsdemux
"tsdemux.exe" %1
for /r "%~dp1" %%h in ("%~n1.track*.h264" "%~n1.track*.vc1") do ( call :x264 "%%h" )
GOTO eof

:x264
"x264.exe" --preset ultrafast --crf 26 -o "%~n1.mkv" %1
del %1
GOTO eof

:mkvmerge
for /r %1 %%i in (%3 %4 %5 %6 %7 %8 %9) do ( call :setfilelist "%%i" )
"mkvmerge.exe" -o "%~n2.mkv" %FILELIST%
del "%~n2.track*.*"
SET FILELIST=
GOTO eof

:setfilelist
SET FILELIST=%FILELIST% %1
:eof

cafevincent
3rd October 2011, 11:17
That seems to work well but I did find a problem: tsdemux doesn't demux subtitle streams.

edit: using switch -u (demux unknown streams) fixes the issue. Also tsdemux names the h264 streams as .264 not .h264 just so you know.

edit2: damn this thing is sweet! thank you all!

cafevincent
3rd October 2011, 13:04
Is it possible to automatically exclude audio tracks with certain language like russian?

edit: I'm also looking for a way to encode all audio tracks into AAC.

edit2: ..and autocropping.

cafevincent
3rd October 2011, 15:26
some sort of mplayer script comes to mind (autocroping),
http://lists.mplayerhq.hu/pipermail/mplayer-users/2005-November/056636.html
(sh at the bottom)

I'm confused, what is a mplayer script?

nm
3rd October 2011, 16:30
I'm confused, what is a mplayer script?

In this case it's a Bash (http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) (+Awk) script that uses MPlayer's (http://www.mplayerhq.hu/) cropdetect filter to calculate suitable cropping values automatically. Might be implementable as a cmd.exe batch script, but I'd use a more powerful scripting language instead. Cygwin includes bash and awk.

cafevincent
3rd October 2011, 16:38
In this case it's a Bash (http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) (+Awk) script that uses MPlayer's (http://www.mplayerhq.hu/) cropdetect filter to calculate suitable cropping values automatically. Might be implementable as a cmd.exe batch script, but I'd use a more powerful scripting language instead. Cygwin includes bash and awk.

Either way it's beyond my skill level to implement :confused:

cafevincent
3rd October 2011, 19:40
What about AAC, is there a free way to use that format? I would love a single command line tool that can convert different bluray audio formats to stereo AAC or other efficient format.

cafevincent
3rd October 2011, 23:40
more problems surfacing: mkvmerge is complaining about .sub files not being supported. And all produced .mkv files have black thumbnails. I use the DivXTechPreviewMKVOnWin7R3_0 for thumbs because it captures an image from about 10% into the video so they actually mean something. So why are they blank?

x264 settings:
"x264.exe" --level 4.1 --aud --nal-hrd vbr --vbv-bufsize 25000 --vbv-maxrate 25000 --filter -1,-1 --ref 3 --bframes 3 --b-adapt 2 --b-pyramid none --subme 10 --aq-mode 1 --trellis 2 --partitions all --me tesa --crf 20 -o "%~n1.mkv" %1

Asmodian
4th October 2011, 07:57
For audio compression and sup to sub conversion, it also won't overwrite anything so you can rerun it on a partially finished directory tree (untested but looks good). ;)

@ECHO OFF
if not exist %1 goto eof
SET FILELIST=

for /r %1 %%g in ("*.m2ts" "*.ts") do (
cd "%%~pg"
call :tsdemux "%%g"
call :mkvmerge "%%~pg" "%%g" "%%~ng.track*.mkv" "%%~ng.track*.m4a" "%%~ng.track*.idx"
)
GOTO eof

:tsdemux
"tsdemux.exe" -u %1
for /r "%~dp1" %%h in ("%~n1.track*.264" "%~n1.track*.vc1") do ( call :x264 "%%h" )
for /r "%~dp1" %%j in ("%~n1.track*.ac3" "%~n1.track*.dts" "%~n1.track*.wav") do ( call :eac3to "%%j" )
for /r "%~dp1" %%k in ("%~n1.track*.sup") do ( call :bdsup2sub "%%k" )
GOTO eof

:x264
if exist "%~n1.mkv" goto eof
"x264.exe" --preset ultrafast --crf 26 -o "%~n1.mkv" %1
del %1
GOTO eof

:eac3to
if exist "%~n1.m4a" goto eof
eac3to.exe %1 "%~n1.m4a"
del %1
GOTO eof

:bdsup2sub
if exist "%~n1.idx" goto eof
java -jar "BDSup2Sub.jar" %1 "%~n1.idx" /lang:en
GOTO eof

:mkvmerge
if exist "%~n2.mkv" goto eof
for /r %1 %%i in (%3 %4 %5 %6 %7 %8 %9) do ( call :setfilelist "%%i" )
"mkvmerge.exe" -o "%~n2.mkv" %FILELIST%
del "%~n2.track*.*"
SET FILELIST=
GOTO eof

:setfilelist
SET FILELIST=%FILELIST% %1
:eof

Saddly, as nm said, autocrop is more work and would need another tool. I am not sure how to do it in batch.

No idea about thumbnails, sry

smok3
4th October 2011, 12:18
for thumbs you may want to check
http://moviethumbnail.sourceforge.net/

something like
::echo off

title %1
echo.
echo %~n1%~x1 1st try
t:\utility\movie_thumbs\mtn\mtn.exe -i -c 1 -r 1 -t -P -o %~x1.jpg %1
:: next line assumes that width > height
t:\utility\movie_thumbs\nconvert\nconvert.exe -out jpeg -ratio -resize 0 100 -canvas 100 100 center %1.jpg


if not exist %1.jpg (
echo %~n1%~x1 2nd try
t:\utility\movie_thumbs\mtn\mtn.exe -i -c 1 -r 1 -t -P -Z -o %~x1.jpg %1
:: next line assumes that width > height
t:\utility\movie_thumbs\nconvert\nconvert.exe -out jpeg -ratio -resize 0 100 -canvas 100 100 center %1.jpg
)


if not exist %1.jpg (
echo %1 > %~f1.error
)

cafevincent
4th October 2011, 13:14
for thumbs you may want to check
http://moviethumbnail.sourceforge.net/

something like
::echo off

title %1
echo.
echo %~n1%~x1 1st try
t:\utility\movie_thumbs\mtn\mtn.exe -i -c 1 -r 1 -t -P -o %~x1.jpg %1
:: next line assumes that width > height
t:\utility\movie_thumbs\nconvert\nconvert.exe -out jpeg -ratio -resize 0 100 -canvas 100 100 center %1.jpg


if not exist %1.jpg (
echo %~n1%~x1 2nd try
t:\utility\movie_thumbs\mtn\mtn.exe -i -c 1 -r 1 -t -P -Z -o %~x1.jpg %1
:: next line assumes that width > height
t:\utility\movie_thumbs\nconvert\nconvert.exe -out jpeg -ratio -resize 0 100 -canvas 100 100 center %1.jpg
)


if not exist %1.jpg (
echo %1 > %~f1.error
)

I'm not sure if I want to pursue this method. Some thoughts: I doubt that say ripbot264 uses this method and its encodes have working thumbs. And by that I mean the thumbs are correctly generated from the video. I like generated thumbs because they are future proof (resolution) and the thumb is not always generated from the same frame making them a bit more interactive/less fixed and open for future thumb innovations like animation. That being said I suspect there is a problem in the creation process here.

cafevincent
4th October 2011, 13:17
For audio compression and sup to sub conversion, it also won't overwrite anything so you can rerun it on a partially finished directory tree (untested but looks good). ;)

@ECHO OFF
if not exist %1 goto eof
SET FILELIST=

for /r %1 %%g in ("*.m2ts" "*.ts") do (
cd "%%~pg"
call :tsdemux "%%g"
call :mkvmerge "%%~pg" "%%g" "%%~ng.track*.mkv" "%%~ng.track*.aac" "%%~ng.track*.idx"
)
GOTO eof

:tsdemux
"tsdemux.exe" -u %1
for /r "%~dp1" %%h in ("%~n1.track*.264" "%~n1.track*.vc1") do ( call :x264 "%%h" )
for /r "%~dp1" %%j in ("%~n1.track*.ac3" "%~n1.track*.dts" "%~n1.track*.wav") do ( call :eac3to "%%j" )
for /r "%~dp1" %%k in ("%~n1.track*.sup") do ( call :bdsup2sub "%%k" )
GOTO eof

:x264
if exist "%~n1.mkv" goto eof
"x264.exe" --preset ultrafast --crf 26 -o "%~n1.mkv" %1
del %1
GOTO eof

:eac3to
if exist "%~n1.aac" goto eof
eac3to.exe %1 "%~n1.aac"
del %1
GOTO eof

:bdsup2sub
if exist "%~n1.idx" goto eof
java -jar "BDSup2Sub.jar" %1 "%~n1.idx" /lang:en
GOTO eof

:mkvmerge
if exist "%~n2.mkv" goto eof
for /r %1 %%i in (%3 %4 %5 %6 %7 %8 %9) do ( call :setfilelist "%%i" )
"mkvmerge.exe" -o "%~n2.mkv" %FILELIST%
del "%~n2.track*.*"
SET FILELIST=
GOTO eof

:setfilelist
SET FILELIST=%FILELIST% %1
:eof

Saddly, as nm said, autocrop is more work and would need another tool. I am not sure how to do it in batch.

No idea about thumbnails, sry

I see there is no quality options for the AAC conversion. Does eac3to decide the bitrate/channels?

smok3
4th October 2011, 13:17
have no clue how ripbot works, this is what i use on my web server with slightly modified script (bash). It takes frame from the middle of the movie approximately.

cafevincent
4th October 2011, 13:25
have no clue how ripbot works, this is what i use on my web server with slightly modified script (bash). It takes frame from the middle of the movie approximately.

Just to make sure, does your method incorporate the thumb into the mkv? What is the maximum supported resolution?

smok3
4th October 2011, 15:12
Just to make sure, does your method incorporate the thumb into the mkv? What is the maximum supported resolution?

nope, no mkv, just makes jpegs & whatever you set it (its 100x100px in this case)

sneaker_ger
4th October 2011, 16:22
The blank thumbs are most likely because of DivX not supporting header removal compression. Disable it for all video and audio tracks for increased compatibility.

cafevincent
4th October 2011, 23:00
The blank thumbs are most likely because of DivX not supporting header removal compression. Disable it for all video and audio tracks for increased compatibility.

Can you elaborate on that? Is that a setting I need to find in mkvmerge? I'm having difficulty finding any option about headers.

cafevincent
5th October 2011, 01:59
I found out that using mkvmerge 4.1.1 produces video compatible with the DivX mkv thumbnail thingy.

cafevincent
5th October 2011, 03:55
For audio compression and sup to sub conversion, it also won't overwrite anything so you can rerun it on a partially finished directory tree (untested but looks good). ;)

@ECHO OFF
if not exist %1 goto eof
SET FILELIST=

for /r %1 %%g in ("*.m2ts" "*.ts") do (
cd "%%~pg"
call :tsdemux "%%g"
call :mkvmerge "%%~pg" "%%g" "%%~ng.track*.mkv" "%%~ng.track*.aac" "%%~ng.track*.idx"
)
GOTO eof

:tsdemux
"tsdemux.exe" -u %1
for /r "%~dp1" %%h in ("%~n1.track*.264" "%~n1.track*.vc1") do ( call :x264 "%%h" )
for /r "%~dp1" %%j in ("%~n1.track*.ac3" "%~n1.track*.dts" "%~n1.track*.wav") do ( call :eac3to "%%j" )
for /r "%~dp1" %%k in ("%~n1.track*.sup") do ( call :bdsup2sub "%%k" )
GOTO eof

:x264
if exist "%~n1.mkv" goto eof
"x264.exe" --preset ultrafast --crf 26 -o "%~n1.mkv" %1
del %1
GOTO eof

:eac3to
if exist "%~n1.aac" goto eof
eac3to.exe %1 "%~n1.aac"
del %1
GOTO eof

:bdsup2sub
if exist "%~n1.idx" goto eof
java -jar "BDSup2Sub.jar" %1 "%~n1.idx" /lang:en
GOTO eof

:mkvmerge
if exist "%~n2.mkv" goto eof
for /r %1 %%i in (%3 %4 %5 %6 %7 %8 %9) do ( call :setfilelist "%%i" )
"mkvmerge.exe" -o "%~n2.mkv" %FILELIST%
del "%~n2.track*.*"
SET FILELIST=
GOTO eof

:setfilelist
SET FILELIST=%FILELIST% %1
:eof

Saddly, as nm said, autocrop is more work and would need another tool. I am not sure how to do it in batch.

No idea about thumbnails, sry

I've been testing your latest revision:

eac3to seems to be producing files with extension ".aac.m4a". this caused a mess but I seem to have fixed it.

dts-hd tracks are not being demuxed (not sure about normal dts)

BDSup2Sub.jar is refusing the sup streams, claiming its not supported. so still no subs.

Files produced have chapters, any idea how to leave them out? No chapters is best for skipping videos when played from a playlist.

Thumbnails do indeed work using mkvmerge 4.1.1

@ECHO OFF
if not exist %1 goto eof
SET FILELIST=

for /r %1 %%g in ("*.m2ts" "*.ts") do (
cd "%%~pg"
call :tsdemux "%%g"
call :mkvmerge "%%~pg" "%%g" "%%~ng.track*.mkv" "%%~ng.track*.m4a" "%%~ng.track*.idx"
)
GOTO eof

:tsdemux
"tsdemux.exe" -u %1
for /r "%~dp1" %%h in ("%~n1.track*.264" "%~n1.track*.vc1" "%~n1.track*.m2v") do ( call :x264 "%%h" )
for /r "%~dp1" %%j in ("%~n1.track*.ac3" "%~n1.track*.dts" "%~n1.track*.pcm" "%~n1.track*.wav") do ( call :eac3to "%%j" )
for /r "%~dp1" %%k in ("%~n1.track*.sup") do ( call :bdsup2sub "%%k" )
GOTO eof

:x264
if exist "%~n1.mkv" goto eof
"x264.exe" --preset ultrafast --crf 26 -o "%~n1.mkv" %1
del %1
GOTO eof

:eac3to
if exist "%~n1.m4a" goto eof
"c:\program files (x86)\eac3to\eac3to.exe" %1 "%~n1.aac"
del %1
GOTO eof

:bdsup2sub
if exist "%~n1.idx" goto eof
"java.exe" -jar "%windir%\BDSup2Sub.jar" %1 "%~n1.idx" /lang:en
GOTO eof

:mkvmerge
if exist "%~n2.mkv" goto eof
for /r %1 %%i in (%3 %4 %5 %6 %7 %8 %9) do ( call :setfilelist "%%i" )
"mkvmerge.exe" -o "%~n2.mkv" %FILELIST%
del "%~n2.track*.*"
SET FILELIST=
GOTO eof

:setfilelist
SET FILELIST=%FILELIST% %1
:eof

Asmodian
5th October 2011, 21:20
I'll need samples to figure any of your specifics out, I don't have any samples with dtshd or subs etc.

Do you mean the dtshd isn't demuxed at all or just not recompressed?

I edited that script to fix the .aac.m4a issue, you should change the eac3to output extention to .m4a.

Feel free to look at the docs for mkvmerge, eac3to, and bdsup2sub. There are a lot of options for them that could fix your issues (yes eac3to has lots of quality and channel settings).

I would stick with the newest mkvtoolnix and just turn off header removal compression.

GL :)

MeteorRain
6th October 2011, 01:16
MeteorRain, can you explain the targeted workflow and especially its flexiblity/stability?

you have gnu make or m$ make to manage all your encoding in a rule file -- makefile.

you can make filename as a variable like $(videoname) and make extension as $(ext). when do your encoding, you can use
nmake /E videoname=blahblah /E ext=mkv
or
export videoname=blahblah
export ext=mkv
make to execute them in your batch file or shell script.

inside the makefile, we define the building rule. say we'd like to re-encode a x264+flac mkv to a x264+aac mp4, we may write as: (nmake style, gnu style may vary)
#prefix can be some tag or some group name etc
prefix=SomePrefix

all: "$(prefix) $(videoname).mp4"

"$(prefix) $(videoname).mp4": "$(videoname).audio.mp4" "$(videoname).video.mp4"
mp4box -add "$(videoname).video.mp4" -add "$(videoname).audio.mp4" "$(prefix) $(videoname).mp4"

"$(videoname).video.mp4": "$(videoname).$(ext)"
x264 --demuxer lavf --other-params -o "$(videoname).video.mp4" "$(videoname).$(ext)"

"$(videoname).audio.mp4": "$(videoname).$(ext)"
mkvextract tracks "$(videoname).$(ext)" 2:"$(videoname).audio.mp4"

the most exciting thing is you never lost your several-hours work when there's something wrong with muxing and you happens to re-execute your script. makefile check your modification time and never overwrite if it's newer than source file. also you'll never missing anything you want to re-encode if you change some source like an avs script.
calling is also as simple as write a loop around the make call.

does that make sense?

MeteorRain
6th October 2011, 01:22
for different type of source, try the super cow power'd ffmpeg tool to extract or transform the audio part.

for avs script being involved, create sub directory for a file, move or hard-link file into sub-dir with a constant name, say blahblah.mkv -> blahblah/video.mkv, and copy your avs code into there, and happy encoding.

cafevincent
6th October 2011, 21:52
I'll need samples to figure any of your specifics out, I don't have any samples with dtshd or subs etc.

Do you mean the dtshd isn't demuxed at all or just not recompressed?

I edited that script to fix the .aac.m4a issue, you should change the eac3to output extention to .m4a.

Feel free to look at the docs for mkvmerge, eac3to, and bdsup2sub. There are a lot of options for them that could fix your issues (yes eac3to has lots of quality and channel settings).

I would stick with the newest mkvtoolnix and just turn off header removal compression.

GL :)

Did you read my private message?

Asmodian
7th October 2011, 18:49
I did, I'll see if I can get it working tonight or tomorrow. I have had a personal project distracting me. ;)

Asmodian
9th October 2011, 02:29
Well it looks like those are not really subtitle files but instead they are the dtsma tracks you were expecting, switching to eac3to to do the demux gave the expected dtsma files.

If I mux just the video + sup file I get a working mkv with audio but no subtitles. lol

Here is a new batch that works for your 00002.m2ts sample, of course encoding the 3 audio tracks into aac and muxing them all might be silly but you can remove all but dtsma or pcm for the audio encoding if you want. I also turned off "header removal compression" for the video track (so switch back to the new mkvtoolnix :p ).

I left in the subtitle handling in case some files really do have sups in them.

@if not exist %1 goto eof
@SET FILELIST=

@for /r %1 %%g in ("*.m2ts" "*.ts") do @( @cd "%%~pg"
if exist "%%~ng.mkv" (@call :mkvexist "%%~ng.mkv" "%%g"
@GOTO eof)
@call :demux "%%g"
@call :mkvmerge "%%~pg" "%%g" "%%~ng - *.mkv" "%%~ng - *.m4a" "%%~ng - *.idx" )
@GOTO eof

:demux
"eac3to.exe" %1 -demux
@for /r "%~dp1" %%h in ("%~n1 - *.h264" "%~n1 - *.vc1") do @( @call :x264 "%%h" )
@for /r "%~dp1" %%j in ("%~n1 - *.ac3" "%~n1 - *.dts" "%~n1 - *.pcm" "%~n1 - *.dtsma") do @( @call :eac3to "%%j" )
@for /r "%~dp1" %%k in ("%~n1 - *.sup") do @( @call :bdsup2sub "%%k" )
@GOTO eof

:x264
@if exist "%~n1.mkv" goto eof
"x264_x64.exe" --preset ultrafast --crf 26 -o "%~n1.mkv" %1
@del %1
@GOTO eof

:eac3to
@if exist "%~n1.m4a" goto eof
"eac3to.exe" %1 "%~n1.m4a"
@del %1
@GOTO eof

:bdsup2sub
@if exist "%~n1.idx" goto eof
java -jar "D:\Ripping\BDSup2Sub.jar" %1 "%~n1.idx" /lang:en
@GOTO eof

:mkvmerge
@for /r %1 %%i in (%3 %4 %5 %6 %7 %8 %9) do @( @call :setfilelist "%%i" )
"mkvmerge.exe" --compression 1:none -o "%~n2.mkv" %FILELIST%
@del "%~n2 - *.*"
@SET FILELIST=
@GOTO eof

:setfilelist
@SET FILELIST=%FILELIST% %1
@GOTO eof

:mkvexist
@ECHO %1 exists, skipping %2
:eof

cafevincent
9th October 2011, 06:46
The reason I need all tracks transformed to AAC is that I usually have plenty of dubbed tracks so I will have to manually post-process those with mmg to get rid of those tracks. I hope the language tag still exists in the AACs.

If I want to get rid of the chapters is that done during demuxing or encoding or remuxing so I can start figuring that out?

Asmodian
9th October 2011, 13:22
I don't actually see how chapters would be there but just add --no-chapters as an option for mkvmerge.exe:

@SET FILELIST=%FILELIST% --no-chapters %1

edit: maybe chapters are coming with the m4a audio tracks through eac3to?

cafevincent
9th October 2011, 15:13
I don't actually see how chapters would be there but just add --no-chapters as an option for mkvmerge.exe:

@SET FILELIST=%FILELIST% --no-chapters %1

edit: maybe chapters are coming with the m4a audio tracks through eac3to?

It appears the chapters are not really chapters. I mean mmg says there are chapters but mpc-hc doesn't see it that way, it does skip to the next video. These "ghost" chapters didn't go anywhere with the --no-chapters option (tried it days ago) so I assumed I was doing something wrong. Anyway this is the way I like it so no problem with chapters.

Are you aware of any ways to identify the audio languages from command line?

Asmodian
10th October 2011, 01:28
From what I can tell there isn't a language set anywhere, sorry. I would decide which track was the right one (dtsma, pcm, or ac3) and just use that one. Usually they don't switch the codec for the different types of streams, i.e. German is always the ac3 while English is always the dtsma, etc. This is, of course, only for that specific source.

Rumbah
10th October 2011, 12:03
I would recommend Staxrip to you, it's a gui that can do batch encoding.

You just have to create a template and then you can batch encode as many files as you like with this template (I did that to convert everything for my PSP before I wrote a dedicated script).


PS: There is the Nero aac encoder that is free and "batchable".

cafevincent
10th October 2011, 23:46
From what I can tell there isn't a language set anywhere, sorry. I would decide which track was the right one (dtsma, pcm, or ac3) and just use that one. Usually they don't switch the codec for the different types of streams, i.e. German is always the ac3 while English is always the dtsma, etc. This is, of course, only for that specific source.

Are you talking generally or about the ambra experience? The ambra experience doesn't have different languages. If that is the case then I suggest trying one of the lost episodes. The audio format method is too unreliable because I have seen too many exceptions to that rule.

cafevincent
13th October 2011, 09:18
I couldn't send you another private message so I put this here. Others: this is a mod of the script to go through existing mkv encodes and it's designed to encode the audio to aac and remove chapters. And convert mp4 to mkv while it's at it. Credit goes to Asmodian and this code quote is just my revision on this work in progress.

@if not exist %1 goto eof
@SET FILELIST=

@for /r %1 %%g in ("*.mkv" "*.mp4") do @( @cd "%%~pg"
if exist "%%~ng_aac.mkv" (@call :mkvexist "%%~ng.mkv" "%%g"
@GOTO eof)
@call :demux "%%g"
@del "%%~ng - *log.txt"
@call :mkvmerge "%%~pg" "%%g" "%%~ng - *.*")
@cd \
@GOTO eof

:demux
"c:\program files (x86)\eac3to\eac3to.exe" %1 -demux
@for /r "%~dp1" %%j in ("%~n1 - *.ac3" "%~n1 - *.dts" "%~n1 - *.pcm" "%~n1 - *.dtsma") do @( @call :eac3to "%%j" )
@GOTO eof

:eac3to
@if exist "%~n1.m4a" goto eof
"c:\program files (x86)\eac3to\eac3to.exe" %1 "%~n1.m4a"
@del %1
@GOTO eof

:mkvmerge
@for /r %1 %%i in (%3 %4 %5 %6 %7 %8 %9) do @( @call :setfilelist "%%i" )
"c:\program files (x86)\mkvtoolnix\mkvmerge.exe" --compression 1:none -o "%~n2_aac.mkv" %FILELIST%
@del "%~n2 - *.*"
@SET FILELIST=
@GOTO eof

:setfilelist
@SET FILELIST= %FILELIST% --no-chapters %1
@GOTO eof

:mkvexist
@ECHO %1 exists, skipping %2
@cd \
:eof

It seems eac3to doesn't support mp4. And there must be something wrong with the header of the resulting mkvs because thumbnail generation (via the divx mkv thumb thingy) for each video takes minutes and end up with a blank thumbnail. However otherwise the script is working, conversion and chapter removal works.

edit: I tried how it skips already converted files and it stops the whole process when it gets to just one in the loop.