Jump to content


Custom Sync using DVR Post Processing Windows Batch Script

  • Please log in to reply
No replies to this topic

#1 HeadHodge OFFLINE  



  • Members
  • 18 posts
  • Local time: 02:34 AM

Posted 02 January 2019 - 09:27 PM

I was very excited with Emby and their new sync feature until I found it would not keep my AC3 audio tracks and instead transcodes them to AAC. This does not play properly on my fire tv app and causes the fire tv to freeze and needing a fire tv restart to continue.
Sync is a feature that will be enhanced and fixed in a future version (Im using Version But in the meantime I decided to burn the time and effort to create a batch file to perform the type of sync I need.[/size]
I live in San Diego and part time in Sacramento CA. Im a Chargers NFL football fan and want to watch the games when I'm in Sacramento without paying hundreds of dollars for something like NFL ticket or monthly fees for other apps just to watch a few Charger games.[/size]
I get all the games in San Diego via antenna and own a HDHOMERUN tuner. So I thought I would try to figure a way to record the games in San Diego and watch them remote on my TV in Sacramento.
The Windows batch file script I wrote works to do this and I have successfully tested it to work fairly well.
I was thinking about adding a lot of documentation to explain how the script works. But on the chance that no one would  be interested in it and a waste of time. I decided to just post the script for your review and will be happy to answer any questions anyone may have.
Transcode.bat file content:

@[member="Echo"] off
@[member="Echo"] off
REM ************************************
REM Change to directory containing ffmpeg.exe and set path variable with emby parameter {path} 
REM ************************************
set path=%1

REM ************************************
REM Parse {path} parameter passed from emby dvr post processing script
REM ************************************
set indrive=%~d1
set outdrive=x:
set infile=%~nx1
set outfile=%~nx1
for /f "delims=\ tokens=1-4,*" %%a in ("%~p1") do (
   set inbase=%indrive%\%%a\%%b\%%c\%%d\
   set inpath=%indrive%\%%a\%%b\%%c\%%d\%%e
   set outbase=%outdrive%\My Drive\Emby\Transcodings\%%d\
   set outpath=%outdrive%\My Drive\Emby\Transcodings\%%d\%%e

REM ************************************
REM Set working variables
REM ************************************
set convertlog="%outpath%convert.log" 2>&1
set ffmpeglog="%outpath%ffmpeg.log" 2>&1

REM ************************************
REM Create directory on Google Drive for transcoded recording
REM ************************************
mkdir "%outpath%"
echo Using variables indrive=%indrive%, inbase=%inbase%, inpath=%inpath%, infile=%infile%, outdrive=%outdrive%, outbase=%outbase%, outpath=%outpath%, outfile=%outfile% > %convertlog%
C:\Windows\System32\robocopy "%inbase:~0,-1%" "%outbase:~0,-1%" /s /XF "%infile%" >> %convertlog%

REM ************************************
REM Run ffmpeg to transcode recording
REM ************************************
echo . >> %convertlog%
echo Call ffmpeg utility to transcode "%infile%" to "%outfile%" in "%inpath%" >> %convertlog%

echo Transcode recording %infile% to "%outfile%" with ffmpeg > %ffmpeglog%
echo . >> %ffmpeglog%

 -loglevel warning^
 -i "%inpath%%infile%"^
 -map 0^
 -filter:v "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2"^
 -c:a copy^
 -c:s copy^
 -c:v libx264 -x264-params "nal-hrd=cbr:force-cfr=1"^
 -b:v 500k -minrate 500k -maxrate 500k -bufsize 2M^
 "%outpath%%outfile%" >> %ffmpeglog% 2>&1

echo . >> %ffmpeglog%
echo All Done! >> %ffmpeglog%

REM ************************************
REM Delete from local drive
REM ************************************
rmdir /s /q "%inbase:~0,-1%" >> %convertlog%

echo . >> %convertlog%
echo All Done! >> %convertlog%

Basically the script does the following:
* Parses the path variable passed to the script by Emby during post processing into its drive, path, and file components.
* Transcode the recording with ffmpeg. HDHOMERUN outputs a very large Mpeg2 file in TS container.
   - The input file is a Mpeg2 stream with multiple AC3 dolby audio track streams and a subtitle stream. Its 1080p 20bits/sec
   - The output file in a TS container with all audio and subtitle untranscoded and the video transcoded to 720p 500kbits/sec
* Delete the original recording and move everthing to my Google Drive
To test I used my tablet at a restaurant in LaJolla connected to the restaurants hotspot and used the emby app to connect and play the recordings remotely using a ddns address I had set up to access my server via port forwarding on my router.
To access the recordings, I created a folder on the emby server to the directory my recording were located on my Google Drive.
It played the recordings at the restaurant even better than I thought it would. Video was great little to no tearing or blocking. Sound was great in Dolby digital Very happy with the results.
The only catch is that the upload speed from my provider is a little less than 1Mb and it takes a fair amount of time to get it hauled up to my Google Drive. This means it will take a while before I will be able watch a Charger game after it gets recorded.
I tried watching some recordings that were on the server itself and not the Google Drive. Because my upload speed is so slow they were completely unwatchable. But I was able to view and traverse the emby app screens fairly well.
I redirect all output from my script to two log files. Here are samples of my log files:

Using variables indrive=B:, inpath=\Content\Emby\Recordings\Maury (1992)\Season 21\, outpath=, inbase=B:\Content\Emby\Recordings\Maury (1992)\, outbase=x:\Emby\Transcodings\Maury (1992)\, infile=master.ts, outfile=Maury S21E35 I Slept With Your Wife ... DNA Will Prove You Are Raising My Baby!.ts 
Rename "B:\Content\Emby\Recordings\Maury (1992)\Season 21\Maury S21E35 I Slept With Your Wife ... DNA Will Prove You Are Raising My Baby!.ts" to "master.ts" 
All Done! 

ffmpeg log with log level set to warnings:

Transcode recording master.ts to "Maury S21E35 I Slept With Your Wife ... DNA Will Prove You Are Raising My Baby!.ts" with ffmpeg 
[mpeg2video @ 0000005367b32640] Invalid frame dimensions 0x0.
    Last message repeated 23 times
[mpegts @ 0000005367b0b780] PES packet size mismatch
    Last message repeated 3 times
[mpeg2video @ 0000005367bdd940] ac-tex damaged at 22 39
[mpeg2video @ 0000005367bdd940] Warning MVs not available
All Done! 

Attached File  1.png   611.88KB   3 downloads
Attached File  2.png   668.71KB   2 downloads

Attached Files

Edited by HeadHodge, 04 January 2019 - 08:16 PM.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users