here is a python script i wrote that will process a directory of SWF files and 
create a folder for each swf and put every jpeg in that swf in the 
corresponding folder. Make the script executable and then just do:

./swf_jpeg_extractor.py </path/to/your/directory>

and it will do it's magic. 

Hope it helps someone.

best,

michael geary


#!/usr/bin/python

import os, re, optparse, sys
import Image

def extractMCsFromSwf(path_to_swf,output_dir):
	"""docstring for extractJpegsFromSwf"""
	raw_list = os.popen("/usr/local/bin/swfextract %s | grep -i movieclips" % path_to_swf).readlines();
	#print raw_list
	if (len(raw_list) == 0):
		return False;
	
	item_output = raw_list[0];

	match_res = re.search("ID\(s\) ([-\d, ]+)",item_output);

	id_list = match_res.group(1)

	##make it an array
	master_array = []
	id_array = id_list.split(", ")
	for this_id in id_array:
		match_res = re.search("(\d+)-(\d+)",this_id);
		
		if match_res:
			(start,end) = match_res.groups()
			master_array.extend(range(int(start),int(end)+1))
		else:
			master_array.append(this_id)
	
	#print master_array
	iteration = 1
	for this_id in master_array:
		#print this_id
		image_name = output_dir + "/%s.swf" % this_id
		cmd = "/usr/local/bin/swfextract -o %s -i %s %s" % (image_name,this_id,path_to_swf)
		os.system(cmd)
		iteration += 1


def extractJpegsFromSwf(path_to_swf,output_dir):
	"""docstring for extractJpegsFromSwf"""
	## get a list of the jpeg IDs
	raw_list = os.popen("/usr/local/bin/swfextract %s | grep -i jpeg" % path_to_swf).readlines();
	#print raw_list
	if (len(raw_list) == 0):
		return False;
	
	jpeg_output = raw_list[0];

	match_res = re.search("ID\(s\) ([-\d, ]+)",jpeg_output);

	id_list = match_res.group(1)

	##make it an array
	master_array = []
	id_array = id_list.split(", ")
	for this_id in id_array:
		match_res = re.search("(\d+)-(\d+)",this_id);
		
		if match_res:
			(start,end) = match_res.groups()
			master_array.extend(range(int(start),int(end)+1))
		else:
			master_array.append(this_id)
	
	#print master_array
	iteration = 1
	for this_id in master_array:
		#print this_id
		image_name = output_dir + "/%s.jpg" % this_id
		cmd = "/usr/local/bin/swfextract -o %s -j %s %s" % (image_name,this_id,path_to_swf)
		os.system(cmd)
		iteration += 1
		## make sure the resulting image is larger that 1x1
		im = Image.open(image_name)
		if (im.size == (1,1)):
			## delete it
			os.remove(image_name)


def processDirectory(the_dir):
	"""docstring for processDirectory"""
	allDirectoryEntities = os.listdir(the_dir)
	for thisEntity in allDirectoryEntities:
		abs_path_to_file = the_dir + "/" + thisEntity
		if (os.path.isfile(abs_path_to_file)):
			print "processing %s" % thisEntity
			
			##make a dir from that basename
			basename_res = re.search("(.*)\.([^\.])+$",thisEntity)
			basename = basename_res.group(1)
			if (basename):
				new_dir = the_dir + "/" + basename;
				#print "making %s " % new_dir
				try:
					os.mkdir(new_dir)
				except OSError:
					print "dir exists"
				extractJpegsFromSwf(abs_path_to_file,new_dir)
				#extractMCsFromSwf(abs_path_to_file,new_dir)


def main():

	parser = optparse.OptionParser()
	parser.set_usage("swf_jpeg_extractor.py [options] <directory>")
	parser.set_description("simply pass the directory containing your swfs")

	## no options supported for now:
	"""
	parser.add_option("-p", "--pages", dest="pages", default="1",
					  help="comma-delimited pages (e.g. 1,2,3; ALL)", metavar="STRING")
	parser.add_option("-r", "--res", dest="res", default=72.0,
					  help="resolution (e.g. 72)", metavar="FLOAT")
	parser.add_option("-o", "--out", dest="outputPath", default=None,
					  help="path to JPEG for output", metavar="FILE")
	parser.add_option("--width", dest="maxWidth", default=0,
					  help="maximum width of jpeg", metavar="INTEGER")
	parser.add_option("--height", dest="maxHeight", default=0,
					  help="maximum height of jpeg", metavar="INTEGER")
	"""
	(opts, args) = parser.parse_args()

	if len (args) < 1:
		parser.print_help()
		sys.exit()

	for path in args:
		processDirectory(path)

if __name__=='__main__':

	#start = clock()
	#print "Starting"
	main()
	#finish = clock()
	#print("seconds:%r" %(finish - start))



On Jan 5, 2010, at 3:31 PM, Matthias Kramm wrote:

> On Fri, Jan 01, 2010 at 04:24:53PM +0100, jean-michel.voicechat_fan 
> <[email protected]> wrote:
>> swfextract -j 1  dummy.swf  -o pic1.jpg
>> swfextract -j 2  dummy.swf  -o pic2.jpg
>> swfextract -j 3  dummy.swf  -o pic3.jpg
> 
> how about
>    for i in 1 2 3;do swfextract -j $i dummy.swf -o pic$i.jpg;done
> ?
> 
> Matthias
> 
> 
> 

Reply via email to