Monday, September 19, 2011

Extracting MP3 from SWF with Hsalf

With revision 13a5495aa50b, Hsalf provides a tool to extract MP3 from SWF.

The gist of this tool is these few simple lines:
# open swf file
fi = swf.SwfFile(inp)
# open output file
fo = open(outp, 'wb')
# iterate through all tags
for tag in fi.iter_body():
  # is this a SoundStreamBlock?
  if isinstance(tag, swf.SoundStreamBlockTag):
    data = StringIO(tag.sound_data)
    mp3 = swf.Mp3StreamSoundData().deserialize(data)
    data = mp3.sound_data.frames
# done, close all files
Basically, we find all SoundStreamBlock tags. These tags hold data of embedded sound streams. With each tag, we take the data, strip off Flash-related headers, and write out pure MP3 frames to output file.

That's quite simple, yea?

1 comment: