Home > Python > Hardsub your video with python and mencoder

Hardsub your video with python and mencoder

March 14th, 2009

All modern video players have subtitle handling capabilities, for example it means that the video player can read subtitle from external file and embed them inside the video during playing … except that bundled in the xbox360 operating system.

I use my xbox also like a media center, it’s connected with my home network so I can watch on tv the videos stored on my home server. This feature it’s normally available using the xbox as a media center extender for windows media center, but using GNU/Linux, you can do it using ushare, an opensource uPnP server compatible with xbox360 streaming protocol.

I have a lot of video with subtitles because I don’t like watching videos dubbed in italian language, I appreciate much more the english version but i need subtitles to understand them better.

So, as I said before, because the xbox360 video player doesn’t handle subtitles from external files, I wrote a small python program to hardsub my video using mencoder.

Mencoder is a command line tool released under GNU GPL license that allow you to decode, encode or filter video files in various format. You can use different video and audio codec, customize them encoding/decoding options and you can use this tool for embedding subtitles from srt files into an existing video.

Scripting programs is very simple in python and in a few line of code, you can automate long command lines, spawn a process and parse its output. It’s exactly what I need so I start coding my program.

In this post I want to tell you only about automating the encoding process for a single file, in the next posts I can explain you how to realize a program that periodically check your filesystem and automatically create an hardsub version of your videos.

This small piece of code use python subprocess module to run an instance of mencoder with the right configuration parameters.

hardsub.py
  1. import subprocess
  2.  
  3. sourceVideo = '/home/rocco/foo/foo.avi'
  4. destinationVideo = '/home/rocco/foo/foo[hardsub].avi'
  5. srtFile = '/home/rocco/foo/foo.srt'
  6.  
  7. cmdLine = ['mencoder',
  8.            sourceVideo,
  9.            '-ovc',
  10.            'xvid',
  11.            '-xvidencopts',
  12.            'fixed_quant=4',
  13.            '-oac',
  14.            'copy',
  15.            '-o',
  16.            destinationVideo,
  17.            '-sub',
  18.            srtFile,
  19.            '-subfont-text-scale',
  20.            '3']
  21.  
  22. subprocess.call(cmdLine)

There is only one very important requirement, in the home directory of the user who start encoding, there must be a directory called “.mplayer” and inside this directory one TrueType font file called “subfont.ttf”.
This is the font used for writing subtitles inside your video, you can choose any TrueType font, copy it’s .ttf file in that directory and rename it.

That’s all!

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon

Python , , , , ,

  1. June 1st, 2009 at 07:14 | #1

    Good afternoon! Kyivska prostitute information there. Event men. Your Kyivska prostitute Rybka. You can visit my blog.

  2. evms
    August 10th, 2009 at 05:33 | #2

    I heard that this won’t work with a newest version of mplayer. xbox360 won’t handle the format produced. any problems with MEncoder 2:1.0~rc2

  1. No trackbacks yet.