Server Version#: 1.18.0.1944
I have a Synology 1019+, with hardware transcoding capabilities. I run plex-dvr on the Synology box using HDHomeRun CableCard.
I wanted to:
- use a post-processing DVR script to convert the MPEG2 video recordings to H.264 format to save space and increase compatibility
- use the GPU rather than CPU on Synology, because CPU compressing hits the processor really hard and is slow.
Took me a lot of trial and error, but here is how I did it.
- install FFMPEG package from SynoCommunity: SynoCommunity
- Install Plex package from Synology DSM Package Manager
- write the script (code is below)
- tell Plex to use post-processing script in Plex - settings - LiveTV & DVR - DVR settings, and then put in the full path of the script.
- And this is the really tricky part. Not for the faint of heart. Copy a driver from the FFMPEG install into the PLEX installation using this command:
sudo cp /var/packages/ffmpeg/target/lib/dri/i965_drv_video.so /volume1/@appstore/Plex Media Server/lib/dri/
The problem is that the Plex version of the i965 driver does not support the hardware encoding commands that FFMPEG wants. (I include an error log below). I think the FFMPEG version of the driver is newer.
Here is the script (lots of ways to skin this cat). I put it in this directory: “/volume1/Multimedia/zScripts/” but you can put it anywhere Plex has access to:
#!/bin/bash
# this is a shell script to call ffmpeg to transcode Plex videos to h264
dvrPostLog=‘/volume1/Multimedia/zScripts/DVRPostProcessing.log’
echo “=================================” | tee -a $dvrPostLog
[ $# -ge 1 -a -f “$1” ]
infile=$1
outfile=“${1%.ts}TMP.ts”
echo “
date '+%Y-%m-%d'” | tee -a $dvrPostLogecho “
date '+%H:%M:%S'file ‘$1’ is opened, trying to use hardware accel.” | tee -a $dvrPostLogecho “
date '+%H:%M:%S'writing transcode to temp file: ‘$outfile’” | tee -a $dvrPostLog#make sure you use the version of FFmpeg that is installed with the package. DSM ships with an older version that is default. Here is how to call the newer build explicitly, with hardware encoding flags:
#This version directs the errors to a log file for debugging.
/var/packages/ffmpeg/target/bin/ffmpeg -init_hw_device vaapi=foo:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_device foo -hwaccel_output_format vaapi -i “$infile” -filter_hw_device foo -acodec copy -vcodec h264_vaapi -preset veryfast -loglevel info “$outfile” > /dev/null 2>> $dvrPostLog# finished with ffmpeg encode
echo “date '+%H:%M:%S'ffmpeg has finished encoding. Renamed old file to .ts” | tee -a $dvrPostLog# remove the old file; rename the converted file to .ts bc Plex wants this.
rm -f “$infile”
mv -f “$outfile” “${infile%.ts}.ts”echo “
date '+%H:%M:%S'conversion done. old file deleted. new file saved in .ts” | tee -a $dvrPostLog
It took me forever to figure out why the script ran successfully when I executed it from the shell (e.g." > DVRPostProcessing.sh test.ts"), but got an error when Plex ran to post-process a recording.
It turns out that there are two versions of this driver on the Synology: “/i965_drv_video.so” One is installed with the FFmpeg package, and one is installed with the Plex package. When I ran directly, the FFmpeg one was used, and when Plex ran the script the one in the Plex directory was used. The one in the Plex directory is not compatible with the FFMpeg hardware encoding calls. Here is a snippet of the FFMPeg error log that shows this:
2019-10-14
15:00:01 file ‘/volume1/Multimedia/TVShows/.grab/6e5aee5391e51ac6c0d29a635feca75a135594a1/The Talk (2010) - S10E26 - Marie Osmond
; Osmond Brothers; Donny Osmond.ts’ is opened, trying to use hardware accel.
15:00:01 writing transcode to temp file: ‘/volume1/Multimedia/TVShows/.grab/6e5aee5391e51ac6c0d29a635feca75a135594a1/The Talk (2
010) - S10E26 - Marie Osmond; Osmond Brothers; Donny OsmondTMP.ts’
ffmpeg version 4.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 4.9.3 (crosstool-NG 1.20.0) 20150311 (prerelease)
configuration: --target-os=linux --cross-prefix=/spksrc/toolchains/syno-x64-6.1/work/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gn
u- --prefix=/var/packages/ffmpeg/target --extra-cflags=-I/spksrc/spk/ffmpeg/work-x64-6.1/install/var/packages/ffmpeg/target/inclu
de --extra-ldflags=-L/spksrc/spk/ffmpeg/work-x64-6.1/install/var/packages/ffmpeg/target/lib --extra-libs=‘-lxml2 -ldl’ --pkg-conf
ig=/usr/bin/pkg-config --ranlib=/spksrc/toolchains/syno-x64-6.1/work/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ranlib --enable-
cross-compile --enable-rpath --enable-pic --enable-shared --enable-gpl --enable-fontconfig --enable-libass --enable-libbluray --e
nable-avresample --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-libopus --enable-libsoxr --enable-libvorbi
s --enable-libvpx --enable-libx264 --enable-libx265 --enable-gnutls --disable-debug --disable-doc --disable-static --arch=x86_64
–enable-thumb --enable-vaapi
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
Input #0, mpegts, from ‘/volume1/Multimedia/TVShows/.grab/6e5aee5391e51ac6c0d29a635feca75a135594a1/The Talk (2010) - S10E26 - Mar
ie Osmond; Osmond Brothers; Donny Osmond.ts’:
Duration: 00:07:51.69, start: 1.400000, bitrate: 13205 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv, bt709, top first), 1920x1080 [SAR 1:1 DAR 1
6:9], Closed Captions, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:10x101: Audio: ac3 ([129][0][0][0] / 0x0081), 48000 Hz, 5.1(side), fltp, 384 kb/s
Stream #0:20x102: Audio: ac3 ([129][0][0][0] / 0x0081), 48000 Hz, mono, fltp, 192 kb/s
[AVHWDeviceContext @ 0x25e2480] libva: /volume1/@appstore/Plex Media Server/lib/dri/i965_drv_video.so has no function __vaDriverInit_1_0
[AVHWDeviceContext @ 0x25e2480] Failed to initialise VAAPI connection: -1 (unknown libva error).
Device creation failed: -5.
[mpeg2video @ 0x25e2780] No device available for decoder: device type vaapi needed for codec mpeg2video.
Stream mapping:
Stream #0:0 → #0:0 (mpeg2video (native) → h264 (h264_vaapi))
Stream #0:1 → #0:1 (copy)
Device setup failed for decoder on input stream #0:0 : Input/output error
Hopefully, the Plex developers will update the build of the i965_drv_video.so driver soon. I hope that nothing in Plex breaks by replacing this driver with the other version!