Archive

Posts Tagged ‘swftools’

Tecplot’s avi use broken windows codecs

April 29th, 2010 No comments

For those of you that use tecplot, you know that while the interface is fairly straightforward and the resulting visuals look pretty good, tecplot has one major flaw. The animations are generated in with essentially the MSRLE codec from a decade ago. The codec’s color palette generally cannot handle continuous color flooding  in tecplot; if you try to open it up on other machines or convert them to say flash video or mp4, you will find that the the colors are all messed up. Sometimes the frame size in tecplot causes the video to not display correctly.

So to fix this issue, I found out that the swf output actually works correctly. Modifying the previous pbox2avi script, I created a tecplot specific script to generate mpeg’s or whatever else ffmpeg can spit out. The script is pretty much a more rudimentary version of pbox2avi.py.

Requirements: swftools, ffmpeg

Download tecplot2mpeg.py.

As usual the code for the script is below:

# Copyright 2010 Jonathan Wong
# python script to convert SWF files generated from tecplot to mpeg
 
#requires swfextract
 
import commands,sys,re,math
 
def parse(filename):
#check filename for .swf extension
if not filename.find('.swf'): # not a comprehensive check
return
 
fileprefix = filename[0:filename.find('.swf')]
commands.getstatusoutput("rm %s.mpeg" % (fileprefix,))
 
#open file
status, output = commands.getstatusoutput("swfextract %s" % (filename,))
 
print output
 
#find "JPEGs: ID(s)"
slide_identifier = "PNGs: ID(s)"
start=output.find(slide_identifier)
slide_extract=[]
if start:
start += len(slide_identifier)+1
slide_end = output.find("[-f]",start)
print start,slide_end
slide_extract=output[start:slide_end-2].replace(" ","")
 
# now extract all the data
print "swfextract %s -P -p %s" % (filename,slide_extract)
 
slides = slide_extract.split(",")
digits = 1 + int(math.log10(int(slides[-1])))
 
for slide in slides:
print "Processing frame: %d" % (int(int(slide)/2)+1,)
print ("swfextract %%s -p %%s & mv output.png %s%%0%dd.png" % (fileprefix,digits,)) % (filename,slide,int(int(slide)/2)+1)
status, output = commands.getstatusoutput(("swfextract %%s -p %%s && mv output.png %s%%0%dd.png" % (fileprefix,digits,)) % (filename,slide,int(int(slide)/2)+1))
 
status, output = commands.getstatusoutput("ffmpeg -f image2 -i %s%%04d.png -b 600k %s.mpeg && rm %s*.png" % (fileprefix,fileprefix,fileprefix))
return slide_extract
 
if __name__=="__main__":
slides=parse(sys.argv[1])
Categories: Useful Apps Tags: , , , , ,

Pbox-to-avi conversion

December 15th, 2008 3 comments

So I just finished another round of grad finals yesterday. I’ve been meaning to update the blog with some other useful or interesting projects that I’ve been playing around with in my spare time.

Now last week, a friend of mine from my wonderful Cal days, came down from UCSF (med student) for a concert at Stanford.  He dropped by afterwards to catch up. Lucky for him, (and future UCSF students) he and his friends (amusing) mentioned that poor UCSF med students have these silly lecture “videos” in swf format, instead of standard web streamed video that Stanford and Cal both have abundantly. The great thing about video is that you can fast forward people. 2 hour lectures become 1!

Of course, having just finished the SCPD video downloading strips, I figured that it was probably video embedded in an swf video player. Turns out, it’s actually a bunch of pictures and audio. There’s no actual video! Apparently what happens is they take pictures every so often and then record sound, take another picture record the sound that goes with it. So, being the nerd I am, I had to try to see if I could convert it to video. So my friend sends me a sample link later that night. Thus, began my Pbox (that’s what they call it) to avi endeavor.

Before I briefly describe the process, I set certain goals I had to make.

  1. In order for all UCSF students to take advantage of it, it would have to be cross-platform.
  2. Automatic. Med students are busy animals that don’t sleep. The simpler and stabler it works, the happier they will be, and the more likely they would use it.
  3. The resulting video quality and size should be similar to that of the swf. After all, it’s just pictures and audio. It’s not REALLY video.
  4. The lowest priority is that it should be decently fast. After all, people need to use their computers at some point. One can’t spend the whole day decoding a lecture they could be watching.

To cut a long story short, I researched different ways of extract data from swf flash files. It turns out that swftools is a crossplatform (with precompiled binaries) for linux, os x, and windows. After figuring out how swfextract works, I realized that I would have to make a video clip of the presentation slide and the corresponding audio clip. To do that, I found mjpegtools, a cross-platform picture-to-video set of tools. To add the audio and video, I resorted to using my familiar tool mencoder. However, I had to make sure that the picture-video was exactly as long as the audio. I resorted to using exiftool, a program that parses tags of stuff. Last but not least, to automate all these tools, so a med student wouldn’t have to know how any of them worked, I used python to glue them altogether.

Requirements:

Usage:

python pbox2avi.py [lecture.swf] [output.avi]

Speed:

  • For an HSWF301 lecture, it took my 2.4Ghz computer 8 minutes to convert it. Encoding the video is a relatively quick process, since it’s mainly smashing pictures and audio into clips and then smashing the clips together.

If you’re a UCSF med student and you benefit from this conversion utility, and you feel like you would like to repay me somehow, feel free to send me an email. Since I’m working on biocomputational cardiac modeling, any future cardiologists, I would like to talk to you!

If you have questions, as usual feel free to leave a comment. Or if you know my friend at UCSF, you can ask him.

Code:

# python script to extract lecture slides and mp3's from UCSF lecture files for conversion
 
#requires swfextract
 
import commands,sys,re
 
def parse(filename):
    #check filename for .swf extension
    if not filename.find('.swf'): # not a comprehensive check
        return
 
    #open file
    status, output = commands.getstatusoutput("swfextract %s" % (filename,))
 
    print output
 
    #find "JPEGs: ID(s)"
    slide_identifier = "JPEGs: ID(s)"
    start=output.find(slide_identifier)
    slide_extract=[]
    if start:
        start += len(slide_identifier)+1
        slide_end = output.find("[-s]",start)
        print start,slide_end
        slide_extract=output[start:slide_end-2].replace(" ","")
 
    #find "Sounds: ID(s)"
    sound_identifier = "Sounds: ID(s)"
    start=output.find(sound_identifier)
    sound_extract=[]
    if start:
        start += len(sound_identifier)+1
        sound_end = output.find("[-f]",start)
        sound_extract=output[start:sound_end-2].replace(" ","")
 
    # now extract all the data
    print "swfextract %s -P -j %s -s %s" % (filename,slide_extract,sound_extract)
 
    status, output = commands.getstatusoutput("swfextract %s -P -j %s -s %s" % (filename,slide_extract,sound_extract))
 
    return slide_extract,sound_extract
 
def create(slide_extract,sound_extract,outputfile):
    #first throw out every other picture because second picture is always a thumbnail
    slides=slide_extract.replace(","," ").split(" ");
    slides=[slides[i] for i in range(len(slides)) if i%2 ==0]
 
    sounds=sound_extract.replace(","," ").split(" ");
 
    print "Number of slides: %s, number of mp3's: %s" % (len(slides),len(sounds))
    for i in range(len(sounds)):
 
        vidcmd="jpeg2yuv -n %d -I p -f 2 -j %s | yuv2lav -o temp.avi" % (int(round(2*10*(duration("sound%s.mp3" % (sounds[i],))))/10),"pic%s.jpg" % (slides[i],))
        # print vidcmd
        status,output=commands.getstatusoutput(vidcmd)
 
        sndcmd="mencoder temp.avi -o slide%d.avi -ovc lavc -lavcopts vcodec=msmpeg4 -oac copy -audiofile sound%s.mp3" %(i,sounds[i])
 
        # print sndcmd
        commands.getstatusoutput(sndcmd)
        print "Finished processing slide %d" % (i,)
 
    print "Cleaning up..."
    # remove unnecessary data
    commands.getstatusoutput("rm *.jpg *.mp3 temp.avi")
 
    slidevids=["slide%s.avi" % (i,) for i in range(len(slides))]
 
    slidevids=" ".join(slidevids)
 
    print "Combining slides..."
    # combine all of them
    commands.getstatusoutput("mencoder -oac copy -ovc copy %s -o %s" % (slidevids,outputfile))
 
    print "Cleaning up..."
    commands.getstatusoutput("rm slide*.avi")
 
def duration(mp3):
    # find duration of mp3 using exiftool
    cmd="exiftool -Duration %s" % (mp3,)
    status,output = commands.getstatusoutput(cmd)
    time=output[output.find(":")+1:output.find("(approx)")-1]
    reg=re.compile(r"(\d+):(\d+)|([\d\.]+) s")
    timestr=reg.search(time)
    #print time,timestr
    Min,Sec,Secs=timestr.groups()
    #print Min,Sec,Secs
    if Secs==None:
        # in terms of minutes
        return int(Min)*60+int(Sec)
    else:
        return float(Secs)
 
if __name__=="__main__":
    slides,sounds=parse(sys.argv[1])
    if len(sys.argv)==2:
        create(slides,sounds,"output.avi")
    elif len(sys.argv)==3:
        create(slides,sounds,sys.argv[2])