Transcode FLV to MP4

On a recent business trip to Kuala Lumpur and Dubai, I had a 16 hour flight and thought I would watch some old Udacity videos.  I downloaded some lectures from Udacity[1] for offline viewing and discovered they were in FLV format.  Unfortunately, the media player built into Windows 8, Windows Media Player, does not play FLV videos out of the box.  There are many solutions to this problem from installing various codecs and alternative video players to transcoding.  I chose to do the latter, and this is my story.

I know there are many different programs that promise to take care of transcoding for you, but I prefer to install as few unknown/untrusted bits of software as possible.  As a quick method to bulk convert the files, I wrote the Python script below. You will need two things to run this script:

  1. Python - I wrote this using version 2.7.5 from the Anaconda package[2]
  2. ffmpeg[3]
from os import listdir, remove
from os.path import isfile, join
import subprocess

vid_path = '[PATH TO VIDEOS]'
ffmpeg_path = '[PATH TO ffmpeg.exe]'

vids = [f for f in listdir(vid_path) if isfile(join(vid_path,f))]

for vid in vids:
    file_name = vid[:-4] + ' -i ' + join(vid_path,vid)
        + ' -qscale 0 -ar 22050 ' + join(vid_path,file_name)
        + '.mp4', shell=True) 

This script is quick and dirty.  It simply loops over all files (hopefully just .flv videos are present) in the vid_path directory specified and uses ffmpeg to transcode them to mp4.  Be sure to set ffmpeg_path to the path of ffmpeg.exe on your system.  After a video is transcoded, it is deleted. One very import note: beware of shell=True in  In some cases, this can be a security risk.  Please see [4].

Possible improvement opportunities for the reader:

  • make removing the original file conditional
  • only join vid_path and vid once
  • filter discovered files so that only .flv files (or the target type of your choice) are processed
  • traverse nested directories looking for files
  • research the arguments ffmpeg accepts and provide a way to change them

[1] Udacity
[2] Continuum Analytics
[3] ffmpeg.zeranoe