Friday, April 16, 2010

Python script for creating video using images and adding audio

I am usng a python script for converting images into a vidoe (using Java JMF) and then adding the audio to the vidoe and converting it to an mp4 format so that I can play it on my phone.
'''
Created on 15 Apr 2010

@author: viswanav
'''
import glob, subprocess, os

vlcLocation = "C:\Development\Apps\PortableApps\VLCPortable\App\\vlc\\vlc.exe"

for g in glob.glob("C:\Development\Docs\Books\VideoCDs\*\PK*"):
    audioFile = ""
    for flvGlob in glob.glob(g + "\*\media\*.flv"):
        audioFile = flvGlob
        break;
    
    
    convertToMov = ["java","-cp","C:\Development\workspaces\Trials\JavaTrials\JMF\out\production\JMFTrial","JpegImagesToMovie", "-w", "400","-h","300","-f","15",g]
    #print convertToMov
    print "Convert to Mov File"
    subprocess.call(convertToMov)
    
    movFileLocation = ""
    for movFile in glob.glob(g + "\*.mov"):
        movFileLocation= movFile
        break
    
    stdoutCommand = "--sout=#transcode{vcodec=mp4v,vb=800,acodec=mp4a,ab=128,scale=1,channels=2,audio-sync}:std{access=file,mux=mp4,dst=\"%s.mp4\"}" % movFileLocation
    print stdoutCommand  
    addAudioAndConverToMp4Command = [vlcLocation,"-I","dummy",movFileLocation,"--input-slave=\"%s\"" % audioFile,stdoutCommand,"vlc://quit"]
    print addAudioAndConverToMp4Command 
    print "Creating mp4 file"
    subprocess.call(addAudioAndConverToMp4Command)
    
    deleteMovCommand = ["del",movFileLocation]
    print deleteMovCommand
    print "Deleting mov file"
    os.remove(movFileLocation)