<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scienceoss.com &#187; mencoder</title>
	<atom:link href="http://scienceoss.com/tags/mencoder/feed/" rel="self" type="application/rss+xml" />
	<link>http://scienceoss.com</link>
	<description>useful tidbits for using open source software in science</description>
	<lastBuildDate>Wed, 26 May 2010 03:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Convert images into a movie with mencoder</title>
		<link>http://scienceoss.com/convert-images-into-a-movie-with-mencoder/</link>
		<comments>http://scienceoss.com/convert-images-into-a-movie-with-mencoder/#comments</comments>
		<pubDate>Sat, 26 Jan 2008 17:10:51 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[utilities]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[jpeg]]></category>
		<category><![CDATA[lavc]]></category>
		<category><![CDATA[mencoder]]></category>
		<category><![CDATA[mf]]></category>
		<category><![CDATA[mpeg4]]></category>
		<category><![CDATA[mplayer]]></category>
		<category><![CDATA[vcodec]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=71</guid>
		<description><![CDATA[Here&#8217;s how to create a movie out of a collection of images, along with a more detailed example on creating multiple images with nested folders of images. The basic idea You&#8217;ll need the command line utility mencoder (which comes with mplayer) for this. The command to create a movie out of a list of files [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how to create a movie out of a collection of images, along with a more detailed example on creating multiple images with nested folders of images.<span id="more-71"></span></p>
<h3>The basic idea</h3>
<p>You&#8217;ll need the command line utility <span class="c"><a href="http://www.mplayerhq.hu/design7/news.html">mencoder</a></span> (which comes with mplayer) for this.</p>
<p>The command to create a movie out of a list of files is this (note this command should all be on one line, but it needs to be wrapped to be displayed here):</p>
<pre class = "prettyprint"><code class = "code">
mencoder "mf://@temp.txt" -mf fps=25 -o output.avi -ovc lavc
 -lavcopts vcodec=mpeg4
</code></pre>
<p><span class="c">temp.txt</span> is a file containing a list of image files, and the new movie will be called <span class="c">output.avi</span>.</p>
<h3>A specific example</h3>
<p>This a Python script for my future reference, but perhaps someone will find it useful.<br />
The problem was this:</p>
<ul>
<li>I had many subfolders with up to 20,000 jpeg images per folder.  Each image was named after the experiment and had a number at the end specifying the frame number.</li>
<li>Some of those subfolders had up to three different experiments in them.  Each experiment needed its own movie.</li>
<li>It was OK to have multiple movies for a single experiment.</li>
</ul>
<p>Directory structure was something like this;</p>
<pre class = "prettyprint"><code class = "code">
workingDir
    -- day1
            -- a_001.jpg
            -- a_002.jpg
            ...
            -- a_21999.jpg
            -- b_001.jpg
            -- b_002.jpg
            ...
            -- b_487.jpg
    --  day2
            -- c_001.jpg
            -- c_002.jpg
            ...
            -- c_1478.jpg
    -- day3
            ...
    ...
    -- day20
</code></pre>
<p>I decided to use Python to parse an input file containing a list of the directories (created using <span class="c"> ls workingDir > folders-to-run.txt</span>).  If I wanted to have a movie created from the contents of a folder, I marked that folder by putting a &#8220;*&#8221; as the first character on the line.</p>
<p>I parsed the contents of each marked directory, separated out files that had different base names, then sorted by the frame number.  I decided on a movie length of 2000 frames to keep file sizes manageable.  For each batch of 2000 images from an experiment, I wrote the filenames to temp.txt and fed that to mencoder.</p>
<p><span class="c">folders-to-run.txt</span> looked like this:</p>
<pre class = "prettyprint"><code class = "code">day1
*day2
*day3
...
day20</code></pre>
<p>(Since only day2 and day3 had a *, they would be the only ones run)</p>
<p>In the end, I&#8217;ll end up with movies called:</p>
<pre class = "prettyprint"><code class = "code">
a_0-2000.avi
a_2000-4000.avi
a_4000-6000.avi
...
a_20000-22000.avi
b_0-2000.avi
c_0-2000.avi
...</code></pre>
<p>Those last two will be smaller than each of the &#8220;a&#8221; movies, since they have less frames, but that&#8217;s OK for my purposes.</p>
<p>Here&#8217;s the working Python script.</p>
<pre class = "prettyprint"><code class = "code">
"""A Python script for creating movies out of directories of jpegs.
Requires mencoder to be installed."""

import os
import re

directory = 'images/workingDir''

# Parse the list of folders to run from the input file.
folderlist = open('folders-to-run.txt')
folders = [line.rstrip() for line in folderlist if line.startswith('*')]
folders = [i.replace('*','') for i in folders]

# Set up regular expressions for later
RE = re.compile('.*-(\d*)\.jpg')
fbRE = re.compile('(.*)-.*\.jpg')

# How many frames to use per movie
framestep = 2000

for folder in folders:

    print '\n\n\tlisting contents of %s . . .'%folder
    files = os.listdir(os.path.join(directory,folder))
    print '%s files found.\n\n' % len(files)

    # If a file is called "asdf-003.jpg", the basename will be 'asdf'.
    basenames = [fbRE.match(i).groups()[0] for i in files if fbRE.match(i)]

    # Get the set of unique basenames.  In the
    basenames = list(set(basenames))

    print '\t***There are %s different runs here.***' % len(basenames)

    # This loop will only execute once if there was only a single experiment
    # in the folder.
    for j,bn in enumerate(basenames):
        these_files = [i for i in files if bn in i]

        # Sort using the "decorate-sort-undecorate" approach
        these_sorted_files = [(int(RE.match(i).groups()[0]),i) for i in these_files if RE.match(i)]
        these_sorted_files.sort()
        these_sorted_files = [i[1] for i in these_sorted_files]

        # these_sorted_files is now a list of the filenames a_001.jpg, a_002.jpg, etc.
        for k in range(0, len(these_sorted_files), framestep):

            frame1 = k
            frame2 = k+framestep

            # Name the output file based on the folder, basename, a number
            # to indicate if it was a second basename in the folder, and the
            # frame range.  Then replace any spaces with "_" so as not
            # to confuse the mencoder command
            this_output_name = '%s_%s_%s_%s-%s.avi' % (folder,bn,j,frame1,frame2
            this_output_name = this_output_name.replace(' ','-')
            print '\n\n\toutput will be %s.' % this_output_name

            #Create a temporary text file that will contain pathnames.  This
            # will be passed to mencoder.
            f = open('temp.txt','w')
            filenames = [os.path.join(directory,folder,i)+'\n' \
                                for i in these_sorted_files[frame1:frame2]]
            f.writelines(filenames)
            f.close()

            # Finally!  Now execute the command to create the video.
            cmd = 'mencoder "mf://@temp.txt" -mf fps=25 -o %s -ovc lavc\
            -lavcopts vcodec=mpeg4' % this_output_name

            os.system(cmd)
            print '\n\nDONE with %s' % this_output_name
print 'Done with all marked folders.'</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/convert-images-into-a-movie-with-mencoder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

