[Windows] Convert .MKV to .AVI and send to SickBeard

Come up with a useful post-processing script? Share it here!
Post Reply
MSB
Newbie
Newbie
Posts: 1
Joined: January 31st, 2012, 9:16 pm

[Windows] Convert .MKV to .AVI and send to SickBeard

Post by MSB »

Hello all,

I've put together a script to automatically convert any .mkv file to .avi so that I can use my XBOX 360 to watch the video. This is primarily for TV shows - so I have integrated calling SabToSickBeard from within the script once the file has been converted. I'm using XenonMKV to convert the file - which doesn't reencode the video, but rather it repackages high-definition H.264/x264 video and 5.1 audio contained in MKV files to an MP4 format that the Xbox 360 can play. (It also renames it to .avi so that XBox will recognize it to play.)

After much trial and error, I think I've got it about right, but I'm a novice with this stuff - so I'd love some feedback and suggestions on how I could make it better. (One thing that I've noticed the last few times the script ran, was that SAB didn't rename the folder from "_UNPACK_" when it was done. This caused the script to fail to find the correct directory and not process correctly. Is this something that I'm not doing correctly in the script, e.g. not giving SAB enough time to rename the folder?)

Code: Select all

@ECHO OFF
ECHO.
ECHO ------------------------------------------------------------------------------------------
ECHO  This postprocessing script runs after video is downloaded in SAB.
ECHO  It will look to see if the file is a MKV format and attempt to convert to AVI
ECHO  If the video is a TV show (Category="tv" in SAB), then it will run SabToSickBeard script
ECHO  Output Params: %1 %2 %3 %4 %5 %6 %7
ECHO ------------------------------------------------------------------------------------------
ECHO.
ECHO Begin SABPostProccessVideo...
ECHO.
SET SabToSickBeard=C:\SickBeard\autoProcessTV\sabToSickBeard.exe
SET XenonMKV=C:\XenonMKV\XenonMKV.exe

CD /d %1\..

ECHO Looking for MKV files. If any are found, will run XenonMKV to convert them to AVIs...
ECHO.
IF EXIST "%CD%\%3\*.mkv" (
ECHO MKVs Found. Running XenonMKV...
ECHO.
%XenonMKV% -inputfolder %1 -outputfolder %1) ELSE (
ECHO No MKV files found. Video was probably AVI already.
ECHO.
GOTO TRY_SAB_TO_SICKBEARD)

ECHO XenonMKV complete.
ECHO.
ECHO Checking for AVI files to see if conversion worked...
ECHO.

CD /d %1\..

IF EXIST "%CD%\%3\*.avi" (
IF EXIST "%CD%\%3\*.mkv" (
ECHO AVIs found, so conversion appears to have been sucessful.
ECHO.
ECHO Removing original MKVs...
ECHO.
DEL "%CD%\%3\*.mkv") ELSE (
ECHO AVIs found, so conversion appears to have been sucessful.
ECHO.
ECHO However no MKVs were found. So it's possible that the original MKVs were already removed or the conversion didn't really work.
ECHO.)) ELSE (
ECHO AVI files were not found so conversion probably failed.
ECHO.
ECHO Any original MKV files were not removed.
ECHO.)
GOTO TRY_SAB_TO_SICKBEARD

:TRY_SAB_TO_SICKBEARD
ECHO Looking to see if video is a TV Episode. If it is SabToSickBeard will run...
ECHO.
IF %5 == tv (
ECHO Video was a TV Show, running SabToSickBeard script...
ECHO.
%SabToSickBeard% %1 %2 %3 %4 %5 %6 %7 
ECHO End SABPostProcessVideo.
exit) ELSE (
ECHO Video is NOT a TV Show, so sabToSickBeard was not run.
ECHO End SABPostProcessVideo.
exit)

REM  Below are the attributes sent from SAB to the Postprocessing Script:
REM   %1 	The final directory of the job (full path)
REM   %2 	The original name of the NZB file
REM   %3 	Clean version of the job name (no path info and ".nzb" removed)
REM   %4 	Indexer's report number (if supported)
REM   %5 	User-defined category
REM   %6 	Group that the NZB was posted in e.g. alt.binaries.x
REM   %7 	Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+21
emosbek
Newbie
Newbie
Posts: 7
Joined: July 7th, 2012, 4:26 pm

Re: [Windows] Convert .MKV to .AVI and send to SickBeard

Post by emosbek »

Awesome! any chance this can be used for movies?
.MKV to .AVI and then move the converted file to a different folder?
bmupton
Newbie
Newbie
Posts: 9
Joined: November 9th, 2012, 7:14 pm

Re: [Windows] Convert .MKV to .AVI and send to SickBeard

Post by bmupton »

I've been using this script but I've modified it slightly.

XenonMKV, at least for me, fails if the folder contains spaces, so I added some bits to copy things to a "processing" folder and back when done. I'm not near my processing computer right now, but when I am again I'll post what I did.

This works fine as a post process script to run on downloads from CouchPotato as well...it just skips over the sabtosickbeard part since the category isn't right.

EDIT: My script

Code: Select all

@ECHO OFF
ECHO.
ECHO -------------------------------------------------------------
ECHO  This postprocessing script runs after video is
ECHO  downloaded in SAB. It will look to see if the
ECHO  file is a given format and attempt to convert to another
ECHO  If the video is a TV show (Set the category below),
ECHO  then it will run SabToSickBeard script as well.
ECHO  Incoming parameters from SAB to this script:
ECHO  Final Directory:
ECHO   %1
ECHO  Original NZB:
ECHO   %2
ECHO  Clean Job Name:
ECHO   %3
ECHO  Indexer Report:
ECHO   %4
ECHO  SAB Category:
ECHO   %5
ECHO  Group:
ECHO   %6
ECHO  postprocess status (0 OK, 1 2 3 are errors):
ECHO   %7
ECHO -------------------------------------------------------------
ECHO.
ECHO Begin SABPostProccessVideo...
ECHO.

REM Change these variables to match your system
REM Make sure TemporaryFolder is NOT inside your regular download folder
REM Make sure your regular download folder is NOT inside TemporaryFolder
REM Make sure TemporaryFolder contains NO SPACES if you are using XenonMKV
SET SabToSickBeard=C:\NewsgroupApps\sickbeard\autoProcessTV\sabToSickBeard.exe
SET VidProcessor=C:\NewsgroupApps\tools\XenonMKV\XenonMKV.exe
SET SickBeardCategory=sb
SET TemporaryFolder=T:\TemporaryVideoProcessor
SET OldFormat=mkv
SET NewFormat=mp4

REM Output the settings we used so there's a record of it (Helps with debugging)
ECHO SabToSickBeard:		%SabToSickBeard%
ECHO Processing Tool:		%VidProcessor%
ECHO SickBeard Category:	%SickBeardCategory%
ECHO Processing Folder:		%TemporaryFolder%
ECHO Converting from: %OldFormat% to: %NewFormat%
ECHO.

ECHO Looking in "%~1" for %OldFormat% files...

IF EXIST "%~1\*.%OldFormat%" (
ECHO %OldFormat%s Found, moving to %TemporaryFolder%...
IF NOT EXIST "%TemporaryFolder%" mkdir "%temporaryfolder%"
move /Y "%~1\*.%OldFormat%" "%TemporaryFolder%\"
ECHO Running Conversion to %NewFormat% format...
REM you could, conceivably, use any converter here to convert to any other format...
REM just put a line that works for your video processor here
%VidProcessor% -inputfolder %TemporaryFolder% -outputfolder %TemporaryFolder%
) ELSE (
ECHO No %OldFormat% files found. Jumping to SickBeard processing...
GOTO TRY_SAB_TO_SICKBEARD
)

ECHO Conversion to %NewFormat% complete, checking to see if files were created...

IF EXIST "%TemporaryFolder%\*.%NewFormat%" (
ECHO SUCCESS!
ECHO Move %NewFormat% files to the source directory
move /Y "%TemporaryFolder%\*.%NewFormat%" "%~1\"
) ELSE (
ECHO FAILED!
ECHO Move %OldFormat% files to the source directory
move /Y "%TemporaryFolder%\*.%OldFormat%" "%~1\"
)

ECHO Clean up the temporary folder
DEL /Q /S /F "%TemporaryFolder%\*.*"
ECHO.

:TRY_SAB_TO_SICKBEARD
ECHO Incoming Category is %~5
ECHO Category required is %SickBeardCategory%
IF %~5 == %SickBeardCategory% (
ECHO Video was sent to SAB from SickBeard
ECHO running SabToSickBeard script...
ECHO.
%SabToSickBeard% %1 %2 %3 %4 %5 %6 %7 
ECHO End SABPostProcessVideo.
exit
) ELSE (
ECHO Video was NOT sent to SAB from SickBeard
ECHO End SABPostProcessVideo.
exit
)

bmupton
Newbie
Newbie
Posts: 9
Joined: November 9th, 2012, 7:14 pm

Re: [Windows] Convert .MKV to .AVI and send to SickBeard

Post by bmupton »

I've updated this a little bit to use FFMPEG instead of XenonMKV as I was having lots of problems with XenonMKV not processing anything properly, or failing to process at all (and hanging my SAB Queue).

Code: Select all

@ECHO OFF
ECHO.
ECHO Begin SABPostProccessVideo...
ECHO.

REM Change these variables to match your system
SET SabToSickBeard=C:\NewsgroupApps\sickbeard\autoProcessTV\sabToSickBeard.exe
SET FFMPEG=C:\NewsgroupApps\tools\64\ffmpeg.exe
SET SickBeardCategory=sb
SET TemporaryFolder1=T:\TemporaryVideoProcessor\%random%
SET TemporaryFolder2=%TemporaryFolder1%\Vid_%random%
SET OldFormat=mkv
SET NewFormat=mp4
REM You shouldn't need to edit anything below here

ECHO Create the temporary processing folder...
IF NOT EXIST "%TemporaryFolder2%" mkdir "%TemporaryFolder2%"
ECHO Created %TemporaryFolder2%
ECHO.
ECHO Looking in "%~1" for %OldFormat% files...
IF EXIST "%~1\*.%OldFormat%" (
ECHO %OldFormat% files Found, copying to %TemporaryFolder2%...
copy /Y "%~1\*.%OldFormat%" "%TemporaryFolder2%\"
ECHO.
ECHO Using %FFMPEG% to convert %OldFormat% to %NewFormat%...
for %%i IN (%TemporaryFolder2%\*.%OldFormat%) DO (%FFMPEG% -y -ss 00:00:00 -threads 6 -i "%%i" -vcodec copy -f mp4 -strict experimental -acodec aac -ab 128k -ac 2 "%%i.%NewFormat%")

REM Pause for 30 seconds once processing is complete
PING 192.168.1.250 -n 1 -w 30000 >NUL
) ELSE (
ECHO Download did not contain %OldFormat% files. Jumping to SickBeard processing...
GOTO TRY_SAB_TO_SICKBEARD
)

REM This part checks to see if the old format files were converted to new format
IF EXIST "%TemporaryFolder2%\*.%NewFormat%" (
ECHO Found %NewFormat% in temporary folder!
ECHO Move them to the source directory
ECHO.
ECHO Pause for thirty seconds before trying to move files
PING 192.168.1.250 -n 1 -w 30000 >NUL
move /Y "%TemporaryFolder2%\*.%NewFormat%" "%~1\"
ECHO.
ECHO Delete %OldFormat% files from source directory
del /Q /S /F "%~1\*.%OldFormat%"
) ELSE (
ECHO DID NOT find %NewFormat% in temporary folder!
ECHO Since we copied the %OldFormat% files here originally we are done processing
)

ECHO Remove the temporary folder
RD /Q /S "%TemporaryFolder1%"
ECHO.

:TRY_SAB_TO_SICKBEARD
ECHO Incoming Category is %~5
ECHO Category required is %SickBeardCategory%
IF %~5 == %SickBeardCategory% (
ECHO Video was sent to SAB from SickBeard
ECHO.
%SabToSickBeard% %1 %2 %3 %4 %5 %6 %7 
ECHO End SABPostProcessVideo.
exit
) ELSE (
ECHO Video was NOT sent to SAB from SickBeard
ECHO.
ECHO End SABPostProcessVideo.
exit
)
This creates a random folder, copies MKV files from the download to it, converts to MP4 using FFMPEG (Direct stream copy video, converts audio to 2-channel AAC), moves the resulting MP4 back to the download folder then deletes the original MKV file. Then if the category coming in matches the category you set for SickBeard, it sends it over to the SABToSickBeard.exe for processing in to SickBeard.

I've been running this script for about a week now and it's working 99% of the time. The other 1% are MKV files that have something wrong with them and FFMPEG simply cannot handle them. For those I've been using TEncoder to manually try and convert them.
Post Reply