Posts Tagged 'mplayer'

Converting flv to avi

I wrote a small python script to convert flv files to avi format using divx or xvid encoding. The trick is to use MPlayer’s “mencoder” tool to convert video streams. I copied the idea from a bash script I found on the internet, but needed a python module to integrate the flvtoavi() function into a greater project.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import commands

BITRATE  = 1000

def flvtoavi(flvfile, options):
	try:
		cmd = " ".join(["mencoder",  flvfile, options, "-vf pp=lb -oac mp3lame"])
		cmd += " -lameopts fast:preset=standard -o "
		cmd += get_filename_without_suffix(flvfile)+".avi"
		commands.getstatusoutput(cmd)
	except Exception as e:
		print e

def get_filename_without_suffix(file):
	return ".".join(os.path.basename(file).split(".")[:-1])

def get_divx_options():
	return "-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=%i:mbd=2:v4mv:autoaspect" % BITRATE

def get_xvid_options():
	return "-ovc xvid -xvidencopts bitrate=%i:autoaspect" % BITRATE


def usage(prog):
	print "Usage:", prog, "-divx|-xvid FLVFILES..."

if __name__ == "__main__":
	argv = sys.argv[1:]
	if len(argv) <= 1:
		usage(sys.argv[0])
		sys.exit(-1)
	format = argv[0]
	if format not in ["-divx", "-xvid"]:
		usage(sys.argv[0])
		sys.exit(-1)
	
	for file in argv[1:]:
		if format == "-divx":
			flvtoavi(file, get_divx_options())
		elif format == "-xvid":
			flvtoavi(file, get_xvid_options())


Advertisements