Use FFmpeg to generate missing episode images


Hi guys, Got fedup with not having images for aired episodes…so decided to fix it.
I’m new to coding so please forgive if I appear very stupid:slight_smile:
I’ve spent past days trying to get sonarr to run post processing script which would use FFmpeg to generate a poster for the episode.

I can use a batch D:/Image.bat file to make ffmpeg generate the required image using the following command in batch file:

ffmpeg -ss 100 -i c:/test/mytest_episode_file.mkv -vf "scale='if(gt(dar,400/225),225*dar,400)':'if(gt(dar,400/225),225,400/dar)',setsar=1,crop=400:225"  -frames:v 1 c:/test/mytest_episode_file.jpg

This will generate the image for the episode perfectly.

So now I am trying to get Sonarr to pass the “%Sonarr_EpisodeFile_Path%” to the batch file, without success:(
I thought following would work:

Set File="%Sonarr_EpisodeFile_Path%"

ffmpeg -ss 100 -i %File% -vf "scale='if(gt(dar,400/225),225*dar,400)':'if(gt(dar,400/225),225,400/dar)',setsar=1,crop=400:225"  -frames:v 1 %File%.jpg

In sonarr, connect - custom script.
Path: C:\Windows\System32\cmd.exe
Arguments: /c D:\Image.bat

Im sure many people would appreciate such an addon for the community as many eps dont have images for 3-4days after airing.

Thanks to anyone for your help.


Anyone can help me please?


You posted in Third Party Add-ons, if you need assistance you should post in help and support.

All environment variables that Sonarr passes the script are lower-case, but it looks like you’re capitalizing each word.


I changed above too:

Still not working:(


Is %File% set properly? Have you tried logging those variables?


Thanks for the reply Markus:smile:
Im still trying to get it to work but so far Ive manged to nearly complete it with following code:
I’ve install ffmpeg for python here []

import os
import sys
import shutil
import subprocess
import shlex 
import os.path
import pipes
import ffmpeg

inputFile = os.environ.get('Sonarr_EpisodeFile_Path')

newone = (os.path.splitext(inputFile)[0])
newimage = ''.join([newone , '-thumb.jpg '])
outputFile = newimage

in_stream = ffmpeg.input('inputFile')
video_stream = in_stream  # automatically equivalent to
video_stream = ffmpeg.filter_(video_stream, '-ss', '00:15:00', '-i',)
video_stream = ffmpeg.filter_(video_stream, '-vframes', '1', '-q:v', '2')
out_stream = ffmpeg.output(out_stream, 'outputFile')

So this get the full path of the
inputFile from Sonarr_EpisodeFile_Path,

which looks like:

D:\Rebuild\Done\Series Source\Looming Tower, The\Season 1\The.Looming.Tower.S01E01.WEBDL-720p.Proper.h264.EAC3-NTb.mkv

Then it removes the extension making newone like:

D:\Rebuild\Done\Series Source\Looming Tower, The\Season 1\The.Looming.Tower.S01E01.WEBDL-720p.Proper.h264.EAC3-NTb

Then I want to add “-thumbs.jpg” to the end of that filepath and endup with outputFile which is:

D:\Rebuild\Done\Series Source\Looming Tower, The\Season 1\The.Looming.Tower.S01E01.WEBDL-720p.Proper.h264.EAC3-NTb-thumbs.jpg

Then ffmegp is fired with the inputfile & outputFile
It all appears to work until outputting the file, where it failes. in sonarr logs is says:

|python.exe|NameError: name 'out_stream' is not defined|14:49|
|python.exe|out_stream = ffmpeg.output(out_stream, 'outputFile')|14:49|
|python.exe|File "D:\Scripts\Testing\", line 42, in <module>|14:49|
|python.exe|Traceback (most recent call last):|14:49|

the line: |python.exe|File “D:\Scripts\Testing\”, line 42, in related to following line:

out_stream = ffmpeg.output(out_stream, ‘outputFile’)

implying its outputfile problem??

Any ideas on how to fix would be appreciated, thanks in advance.


No, it’s saying you’re trying to use out_stream, but it’s not yet defined, which is true because you’re trying to define it on the same line you’re trying to use it.


Wow thanks for speedy reply marcus:) been pulling my hair out here. but isnt this outputFile already definded top of script

“newimage = ‘’.join([newone , '-thumb.jpg '])
outputFile = newimage”


Umm, outputFile isn’t the problem… please re-read my last reply.

Anyways, this isn’t a problem with Sonarr, this is a problem with the code you’re writing and not something we support here.


This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.