Search code examples
ffmpegoverlaydurationdrawtext

FFMPEG: how can I properly add a text overlay to my video and save it to a thumbnail?


I have a ffmpeg command that goes to a url and successfully creates thumbnails at specified intervals. What I would like to do is add the time (eg: 03:45:20) to the bottom-left corner of the video in white text with a black shadow. I have seen a few examples online with drawtext but none of them seem to work with my current command:

C:\ffmpeg\bin\ffmpeg.exe -ss 00:23:12 -i "http://myvideourl.com/videofile.mp4" -f mjpeg -vframes 1 -y C:\thumb2.jpg

Can someone suggest how I can implement the correct drawtext filter with my current command so that it outputs the thumbnail with the duration stamped at the bottom-left corner?


Solution

  • As I was not able to find any solutions online, and I had further image effects and scaling to do, I decided to use Python's PIL module to manipulate the screenshot once saved from the ffmpeg command.

    Using PIL:

    from PIL import Image
    from PIL import ImageFont
    from PIL import ImageDraw
    
    img = Image.open("C:\\path\\to\\saved\\thumb.jpg")
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("C:\\path\\to\\your\\font.ttf", 20)
    draw.text((10, img.size[1] - 30), dur, (255, 255, 255), font=font)
    #uncomment line below to scale image
    #img.thumbnail((400, 300), Image.ANTIALIAS)
    img.save("C:\\path\\to\\saved\\thumb.jpg")
    

    All that's left is figuring out how to set the background color to black and add a little text shading and I'll be good to go.