<?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>George&#039;s weblog &#187; Code</title>
	<atom:link href="http://www.helyar.net/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.helyar.net</link>
	<description>From the desktop of George Helyar</description>
	<lastBuildDate>Thu, 26 Aug 2010 14:27:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Compiling zlib.lib on Windows</title>
		<link>http://www.helyar.net/2010/compiling-zlib-lib-on-windows/</link>
		<comments>http://www.helyar.net/2010/compiling-zlib-lib-on-windows/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 14:25:59 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[zlib]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=553</guid>
		<description><![CDATA[zlib is the standard for lossless data compression. The DEFLATE compression algorithm is the basis for just about every lossless compression format out there, including &#8220;zip&#8221; and &#8220;gzip&#8221;, which is itself part of zlib. There are two ways that it can be used from C/C++ projects in Windows. Firstly, it can be used by dynamic [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://zlib.net/">zlib</a> is the standard for lossless data compression. The DEFLATE compression algorithm is the basis for just about every lossless compression format out there, including &#8220;zip&#8221; and &#8220;gzip&#8221;, which is itself part of zlib.</p>
<p>There are two ways that it can be used from C/C++ projects in Windows.</p>
<p>Firstly, it can be used by dynamic linking (dll). This means using zdll.lib and shipping the appropriate version of zlib1.dll with your project. This is not a problem, as Windows versions of both of these files are provided.</p>
<p>The second way is to use static linking. That is, having all of the code in one .lib file and compiling it into your exe so that you do not have to distribute zlib1.dll. This means compiling zlib.lib.</p>
<p>In version 1.2.4 of zlib, a &#8220;projects&#8221; directory was provided, with a Microsoft Visual C++ 6 project. However, it seems that version 1.2.5 has not included this project. This means that the best solution is to go and get the 1.2.4 source and compile it yourself. However, the zlib project seems to be kept inside the <a href="https://sourceforge.net/projects/libpng/files/">libpng</a> project on sourceforge.net, so it is not immediately obvious where to find older versions of the zlib source code.</p>
<p><a href="https://sourceforge.net/projects/libpng/files/zlib/1.2.4/zlib124.zip/download">zlib 1.2.4 source (zip)</a></p>
<p>Extract the zip, open projects\visualc6\zlib.dsp in Visual Studio (I used 2005) and compile &#8220;LIB Release&#8221; (and optionally &#8220;LIB Debug&#8221;)</p>
<p>Copy zlib.h and zconf.h from &#8220;include&#8221; to your Visual Studio &#8220;include&#8221; directory, and zlib.lib (and zlibd.lib if you made it) to your Visual Studio &#8220;lib&#8221; directory.</p>
<p>On 64 bit Windows, with Visual Studio 2005, this is &#8220;C:\Program Files (x86)\Microsoft Visual Studio 8\VC\&#8221; so adjust for your version of Visual Studio.</p>
<p>You now just need to add &#8220;zlib.lib&#8221; to your &#8220;Linker -> Input -> Additional Dependencies&#8221; line in your C++ project configuration to use it (and optionally zlibd.lib for the debug version).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2010/compiling-zlib-lib-on-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remote recursive sha1sum with php</title>
		<link>http://www.helyar.net/2010/remote-recursive-sha1sum-with-php/</link>
		<comments>http://www.helyar.net/2010/remote-recursive-sha1sum-with-php/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 13:29:11 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=495</guid>
		<description><![CDATA[To calculate the SHA-1 sums, display them and make them available for download in a sums.gz file: &#60;?php echo&#40;`find ./some_directory/ -type f &#124; grep -v sums.gz &#124; xargs sha1sum &#124; gzip -c &#124; tee sums.gz &#124; zcat`&#41;; ?&#62; To check the sums: &#60;?php echo&#40;`zcat sums.gz &#124; sha1sum -c -`&#41;; ?&#62;]]></description>
			<content:encoded><![CDATA[<p>To calculate the SHA-1 sums, display them and make them available for download in a sums.gz file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;color: black;"><span style="color: #000000; font-weight: bold;color: blue;">&lt;?php</span> <span style="color: #b1b100;color: blue;">echo</span><span style="color: #009900;color: black;">&#40;</span>`find <span style="color: #339933;color: black;">./</span>some_directory<span style="color: #339933;color: black;">/</span> <span style="color: #339933;color: black;">-</span>type f <span style="color: #339933;color: black;">|</span> grep <span style="color: #339933;color: black;">-</span>v sums<span style="color: #339933;color: black;">.</span>gz <span style="color: #339933;color: black;">|</span> xargs sha1sum <span style="color: #339933;color: black;">|</span> gzip <span style="color: #339933;color: black;">-</span>c <span style="color: #339933;color: black;">|</span> tee sums<span style="color: #339933;color: black;">.</span>gz <span style="color: #339933;color: black;">|</span> zcat`<span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>To check the sums:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;color: black;"><span style="color: #000000; font-weight: bold;color: blue;">&lt;?php</span> <span style="color: #b1b100;color: blue;">echo</span><span style="color: #009900;color: black;">&#40;</span>`zcat sums<span style="color: #339933;color: black;">.</span>gz <span style="color: #339933;color: black;">|</span> sha1sum <span style="color: #339933;color: black;">-</span>c <span style="color: #339933;color: black;">-</span>`<span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2010/remote-recursive-sha1sum-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Get external IP address with Python</title>
		<link>http://www.helyar.net/2010/get-external-ip-address-with-python/</link>
		<comments>http://www.helyar.net/2010/get-external-ip-address-with-python/#comments</comments>
		<pubDate>Wed, 26 May 2010 18:04:43 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=493</guid>
		<description><![CDATA[Here&#8217;s a quick snippet of Python code (tested in 3.0) to quickly look up your external IP address over HTTP and display it: import urllib.request print&#40;str&#40;urllib.request.urlopen&#40;&#34;http://www.whatismyip.com/automation/n09230945.asp&#34;&#41;.read&#40;&#41;, &#34;utf8&#34;&#41;&#41;]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick snippet of Python code (tested in 3.0) to quickly look up your external IP address over HTTP and display it:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;color: black;"><span style="color: #ff7700;font-weight:bold;color: blue;">import</span> <span style="color: #dc143c;color: blue;">urllib</span>.<span style="color: black;color: black;">request</span>
<span style="color: #ff7700;font-weight:bold;color: blue;">print</span><span style="color: black;color: black;">&#40;</span><span style="color: #008000;color: blue;">str</span><span style="color: black;color: black;">&#40;</span><span style="color: #dc143c;color: blue;">urllib</span>.<span style="color: black;color: black;">request</span>.<span style="color: black;color: black;">urlopen</span><span style="color: black;color: black;">&#40;</span><span style="color: #483d8b;color: maroon;">&quot;http://www.whatismyip.com/automation/n09230945.asp&quot;</span><span style="color: black;color: black;">&#41;</span>.<span style="color: black;color: black;">read</span><span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#41;</span>, <span style="color: #483d8b;color: maroon;">&quot;utf8&quot;</span><span style="color: black;color: black;">&#41;</span><span style="color: black;color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2010/get-external-ip-address-with-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>wp-syntax to look like Visual Studio</title>
		<link>http://www.helyar.net/2010/wp-syntax-to-look-like-visual-studio/</link>
		<comments>http://www.helyar.net/2010/wp-syntax-to-look-like-visual-studio/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 13:08:50 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=471</guid>
		<description><![CDATA[wp-syntax is a nice plugin for WordPress using GeSHi to produce syntax highlighted blocks of code. The default colours aren&#8217;t very nice though, and there is no way to easily change them. wp-syntax-colorizer (horrible name) makes it easier to set the colours, but defaults to even worse colours. Most of us want readable colours that [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://wordpress.org/extend/plugins/wp-syntax/">wp-syntax</a> is a nice plugin for <a href="http://wordpress.org/">WordPress</a> using <a href="http://qbnz.com/highlighter/">GeSHi</a> to produce syntax highlighted blocks of code.</p>
<p>The default colours aren&#8217;t very nice though, and there is no way to easily change them. <a href="http://articles.akgfx.com/2008/04/wp-syntax-colorizer/">wp-syntax-colorizer</a> (horrible name) makes it easier to set the colours, but defaults to even worse colours.</p>
<p>Most of us want readable colours that we are used to from IDEs, and most of us will be using either Visual Studio or Eclipse. I mainly use the former. To get it to use the visual studio colours I edit wp-syntax-colorizer as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;color: black;"><span style="color: #000000; font-weight: bold;color: blue;">function</span> my_custom_geshi_styles<span style="color: #009900;color: black;">&#40;</span><span style="color: #339933;color: black;">&amp;</span><span style="color: #000088;">$geshi</span><span style="color: #009900;color: black;">&#41;</span>
<span style="color: #009900;color: black;">&#123;</span>
  <span style="color: #000088;">$overall</span> <span style="color: #339933;color: black;">=</span> <span style="color: #0000ff;color: maroon;">&quot;black&quot;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$keyword</span> <span style="color: #339933;color: black;">=</span> <span style="color: #0000ff;color: maroon;">&quot;blue&quot;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$literal</span> <span style="color: #339933;color: black;">=</span> <span style="color: #0000ff;color: maroon;">&quot;maroon&quot;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$comment</span> <span style="color: #339933;color: black;">=</span> <span style="color: #0000ff;color: maroon;">&quot;green&quot;</span><span style="color: #339933;color: black;">;</span>
&nbsp;
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_overall_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$overall</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
&nbsp;
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_keyword_group_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #cc66cc;color: maroon;">1</span><span style="color: #339933;color: black;">,</span> <span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$keyword</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_keyword_group_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #cc66cc;color: maroon;">2</span><span style="color: #339933;color: black;">,</span> <span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$keyword</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_keyword_group_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #cc66cc;color: maroon;">3</span><span style="color: #339933;color: black;">,</span> <span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$keyword</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_keyword_group_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #cc66cc;color: maroon;">4</span><span style="color: #339933;color: black;">,</span> <span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$keyword</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
&nbsp;
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_symbols_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$overall</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_methods_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #cc66cc;color: maroon;">1</span><span style="color: #339933;color: black;">,</span> <span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$overall</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_regexps_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #cc66cc;color: maroon;">1</span><span style="color: #339933;color: black;">,</span> <span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$overall</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
&nbsp;
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_strings_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$literal</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_numbers_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$literal</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
&nbsp;
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_comments_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #cc66cc;color: maroon;">1</span><span style="color: #339933;color: black;">,</span> <span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$comment</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
  <span style="color: #000088;">$geshi</span><span style="color: #339933;color: black;">-&gt;</span><span style="color: #004000;color: black;">set_comments_style</span><span style="color: #009900;color: black;">&#40;</span><span style="color: #0000ff;">'MULTI'</span><span style="color: #339933;color: black;">,</span><span style="color: #0000ff;color: maroon;">&quot;color: <span style="color: #006699; font-weight: bold;">$comment</span>;&quot;</span><span style="color: #339933;color: black;">,</span> <span style="color: #009900; font-weight: bold;color: blue;">true</span><span style="color: #009900;color: black;">&#41;</span><span style="color: #339933;color: black;">;</span>
<span style="color: #009900;color: black;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2010/wp-syntax-to-look-like-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove Size bbcode from phpbb</title>
		<link>http://www.helyar.net/2010/remove-size-bbcode-from-phpbb/</link>
		<comments>http://www.helyar.net/2010/remove-size-bbcode-from-phpbb/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 17:16:13 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=451</guid>
		<description><![CDATA[One of the most annoying things about phpBB is the ability for people to randomly use [size="200"]Huge[/size] BB code, with no easy way to remove it and every time you update between versions it comes back. To disable it: in /include/bbcode.php remove the whole case 5: ... break; section. To remove it from the posting [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most annoying things about phpBB is the ability for people to randomly use [size="200"]Huge[/size] BB code, with no easy way to remove it and every time you update between versions it comes back.</p>
<p>To disable it:<br />
in /include/bbcode.php remove the whole</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;color: black;"><span style="color: #b1b100;color: blue;">case</span> <span style="color: #cc66cc;color: maroon;">5</span><span style="color: #339933;color: black;">:</span>
<span style="color: #339933;color: black;">...</span>
<span style="color: #b1b100;color: blue;">break</span><span style="color: #339933;color: black;">;</span></pre></div></div>

<p> section.</p>
<p>To remove it from the posting page:<br />
in /styles/prosilver/template/posting_buttons.html remove the whole</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;color: black;">&lt;select&gt;
...
&lt;/select&gt;</pre></div></div>

<p> section.</p>
<p>After that, delete your &#8220;cache&#8221; directory so that it uses the new copy.</p>
<p>Remember that you have to repeat this process after every update to phpBB3.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2010/remove-size-bbcode-from-phpbb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Moving comments in WordPress</title>
		<link>http://www.helyar.net/2010/moving-comments-in-wordpress/</link>
		<comments>http://www.helyar.net/2010/moving-comments-in-wordpress/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 15:03:02 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=440</guid>
		<description><![CDATA[In MySQL, first move the comment: UPDATE wp_comments SET comment_post_ID = P_TO_ID WHERE comment_ID = C_ID; P_TO_ID is the ID of the post you are moving it to. C_ID is the ID of the comment you are moving. You can find both of these IDs easily from the dashboard. Then correct the comment counts for [...]]]></description>
			<content:encoded><![CDATA[<p>In MySQL, first move the comment:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;color: black;"><span style="color: #993333; font-weight: bold;color: blue;">UPDATE</span> wp_comments <span style="color: #993333; font-weight: bold;color: blue;">SET</span> comment_post_ID <span style="color: #66cc66;color: black;">=</span> P_TO_ID <span style="color: #993333; font-weight: bold;color: blue;">WHERE</span> comment_ID <span style="color: #66cc66;color: black;">=</span> C_ID;</pre></div></div>

<p>P_TO_ID is the ID of the post you are moving it to. C_ID is the ID of the comment you are moving. You can find both of these IDs easily from the dashboard.</p>
<p>Then correct the comment counts for the posts:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;color: black;"><span style="color: #993333; font-weight: bold;color: blue;">UPDATE</span> wp_posts <span style="color: #993333; font-weight: bold;color: blue;">SET</span> comment_count <span style="color: #66cc66;color: black;">=</span> <span style="color: #66cc66;color: black;">&#40;</span><span style="color: #993333; font-weight: bold;color: blue;">SELECT</span> count<span style="color: #66cc66;color: black;">&#40;</span><span style="color: #66cc66;color: black;">*</span><span style="color: #66cc66;color: black;">&#41;</span> <span style="color: #993333; font-weight: bold;color: blue;">FROM</span> wp_comments <span style="color: #993333; font-weight: bold;color: blue;">WHERE</span> comment_post_id <span style="color: #66cc66;color: black;">=</span> id <span style="color: #993333; font-weight: bold;color: blue;">AND</span> comment_approved <span style="color: #66cc66;color: black;">=</span> <span style="color: #cc66cc;color: maroon;">1</span><span style="color: #66cc66;color: black;">&#41;</span>
<span style="color: #993333; font-weight: bold;color: blue;">WHERE</span> comment_count !<span style="color: #66cc66;color: black;">=</span> <span style="color: #66cc66;color: black;">&#40;</span><span style="color: #993333; font-weight: bold;color: blue;">SELECT</span> count<span style="color: #66cc66;color: black;">&#40;</span><span style="color: #66cc66;color: black;">*</span><span style="color: #66cc66;color: black;">&#41;</span> <span style="color: #993333; font-weight: bold;color: blue;">FROM</span> wp_comments <span style="color: #993333; font-weight: bold;color: blue;">WHERE</span> comment_post_id <span style="color: #66cc66;color: black;">=</span> id <span style="color: #993333; font-weight: bold;color: blue;">AND</span> comment_approved <span style="color: #66cc66;color: black;">=</span> <span style="color: #cc66cc;color: maroon;">1</span><span style="color: #66cc66;color: black;">&#41;</span>;</pre></div></div>

<p>The second line in this query is not strictly necessary but it lets you know how many posts were affected.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2010/moving-comments-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert any audio/video files to mp3 for free</title>
		<link>http://www.helyar.net/2009/convert-any-audiovideo-files-to-mp3-for-free/</link>
		<comments>http://www.helyar.net/2009/convert-any-audiovideo-files-to-mp3-for-free/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 01:28:58 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[batch]]></category>
		<category><![CDATA[ffmpeg]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=419</guid>
		<description><![CDATA[It seems that there is some confusion about audio conversion tools and some people even pay for them. The truth is that most encoders, even ones you pay for, use ffmpeg internally to do all of the actual work and ffmpeg is free and open source. You can even get it pre-compiled for Windows, though [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that there is some confusion about audio conversion tools and some people even pay for them.</p>
<p>The truth is that most encoders, even ones you pay for, use <a href="http://www.ffmpeg.org">ffmpeg</a> internally to do all of the actual work and ffmpeg is free and open source.</p>
<p>You can even get it <a href="http://www.videohelp.com/tools/ffmpeg">pre-compiled for Windows</a>, though the version isn&#8217;t great.</p>
<p>The only problem with using ffmpeg directly is that it has a command line interface and most novices find it hard to use.</p>
<p>Here&#8217;s a batch file for use on Windows which will convert anything you drop onto the file into a 320kbps mp3:</p>

<div class="wp_syntax"><div class="code"><pre class="winbatch" style="font-family:monospace;color: black;">ffmpeg <span style="color: #66cc66;color: black;">-</span>i <span style="color: #66cc66;color: black;">%</span>1 <span style="color: #66cc66;color: black;">-</span>ab 320k <span style="color: #66cc66;color: black;">-</span>y <span style="color: #66cc66;color: black;">%</span>1.mp3
<span style="color: #66cc66;color: black;">@</span><span style="color: #800080;color: blue;">if</span> errorlevel <span style="color: #cc66cc;color: maroon;">1</span> <span style="color: #66cc66;color: black;">@</span><span style="color: #0000FF;color: blue;">pause</span></pre></div></div>

<p>To create this file, open notepad. Copy and paste the above 2 lines in. Save it as &#8220;convert.bat&#8221; or something similar (the .bat is important) and select &#8220;All files&#8221; from the drop down list.</p>
<p>Put the bat file in the same directory as ffmpeg.exe and simply drag anything you want converted on top of the bat file.</p>
<p>You can use almost anything. wav, m4a, m4v, mov, mpg, avi, etc. If you use a file with both audio and video, such as a movie, it will just extract the audio and save that as an mp3 file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2009/convert-any-audiovideo-files-to-mp3-for-free/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ffmpeg target file sizes (again)</title>
		<link>http://www.helyar.net/2009/ffmpeg-target-file-sizes-again/</link>
		<comments>http://www.helyar.net/2009/ffmpeg-target-file-sizes-again/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 17:41:05 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=330</guid>
		<description><![CDATA[This script is now available as an ongoing project that can be downloaded. Please see the Video Encoding Project. As a bit of a revision to a previous entry, I have converted the combination bash/python scripts I gave earlier into a single large python script. This can now be used without editing any files to [...]]]></description>
			<content:encoded><![CDATA[<p>This script is now available as an ongoing project that can be downloaded. Please see the <a href="http://www.helyar.net/enc">Video Encoding Project</a>.<br />
<hr />
<p>As a bit of a revision to a <a href="http://www.helyar.net/2009/how-to-set-target-file-sizes-in-ffmpeg/">previous entry</a>, I have converted the combination bash/python scripts I gave earlier into a single large python script.</p>
<p>This can now be used without editing any files to set sizes. An example of use is:<br />
<code>$ ./enc.py -s 700 -a 192 -o '-deinterlace' da_*.VOB</code><br />
All of these flags are optional except the file list, where at least 1 file must be specified.</p>
<p><a href='http://www.helyar.net/wp-content/uploads/2009/07/enc.tar.gz'>enc.tar.gz</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;color: black;"><span style="color: #808080; font-style: italic;color: green;">#!/usr/bin/python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">from</span> <span style="color: #dc143c;color: blue;">getopt</span> <span style="color: #ff7700;font-weight:bold;color: blue;">import</span> <span style="color: #dc143c;color: blue;">getopt</span>, GetoptError
<span style="color: #ff7700;font-weight:bold;color: blue;">from</span> <span style="color: #dc143c;color: blue;">sys</span> <span style="color: #ff7700;font-weight:bold;color: blue;">import</span> argv, exit
<span style="color: #ff7700;font-weight:bold;color: blue;">from</span> <span style="color: #dc143c;color: blue;">subprocess</span> <span style="color: #ff7700;font-weight:bold;color: blue;">import</span> Popen, PIPE
<span style="color: #ff7700;font-weight:bold;color: blue;">from</span> <span style="color: #dc143c;color: blue;">re</span> <span style="color: #ff7700;font-weight:bold;color: blue;">import</span> search
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">def</span> secs<span style="color: black;color: black;">&#40;</span>h, m, s<span style="color: black;color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;color: blue;">return</span> <span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#40;</span>h<span style="color: #66cc66;color: black;">*</span><span style="color: #ff4500;color: maroon;">60</span><span style="color: black;color: black;">&#41;</span>+m<span style="color: black;color: black;">&#41;</span><span style="color: #66cc66;color: black;">*</span><span style="color: #ff4500;color: maroon;">60</span><span style="color: black;color: black;">&#41;</span>+s
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">def</span> calc_video_bitrate<span style="color: black;color: black;">&#40;</span>target_bytes, ab, <span style="color: #dc143c;color: blue;">time</span><span style="color: black;color: black;">&#41;</span>:
  audio_bytes = <span style="color: black;color: black;">&#40;</span>ab <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">1000</span> / <span style="color: #ff4500;color: maroon;">8</span><span style="color: black;color: black;">&#41;</span> <span style="color: #66cc66;color: black;">*</span> <span style="color: #dc143c;color: blue;">time</span>
  <span style="color: #ff7700;font-weight:bold;color: blue;">return</span> <span style="color: black;color: black;">&#40;</span>target_bytes - audio_bytes<span style="color: black;color: black;">&#41;</span> / <span style="color: #dc143c;color: blue;">time</span> <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">8</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">def</span> get_duration<span style="color: black;color: black;">&#40;</span><span style="color: #008000;color: blue;">file</span><span style="color: black;color: black;">&#41;</span>:
  ffmpeg = Popen<span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#40;</span><span style="color: #483d8b;color: maroon;">&quot;ffmpeg&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;-i&quot;</span>, <span style="color: #008000;color: blue;">file</span><span style="color: black;color: black;">&#41;</span>, stderr=PIPE<span style="color: black;color: black;">&#41;</span>
  match = search<span style="color: black;color: black;">&#40;</span><span style="color: #483d8b;color: maroon;">&quot;(<span style="color: #000099; font-weight: bold;">\\</span>d+):(<span style="color: #000099; font-weight: bold;">\\</span>d+):(<span style="color: #000099; font-weight: bold;">\\</span>d+)<span style="color: #000099; font-weight: bold;">\\</span>.<span style="color: #000099; font-weight: bold;">\\</span>d+&quot;</span>, ffmpeg.<span style="color: black;color: black;">communicate</span><span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#41;</span><span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">1</span><span style="color: black;color: black;">&#93;</span><span style="color: black;color: black;">&#41;</span>
  ffmpeg.<span style="color: black;color: black;">stderr</span>.<span style="color: black;color: black;">close</span><span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;color: blue;">if</span> match == <span style="color: #008000;color: blue;">None</span>: <span style="color: #ff7700;font-weight:bold;color: blue;">return</span> <span style="color: #ff4500;color: maroon;">0</span>
  dur = match.<span style="color: black;color: black;">groups</span><span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;color: blue;">return</span> secs<span style="color: black;color: black;">&#40;</span><span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>dur<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">0</span><span style="color: black;color: black;">&#93;</span><span style="color: black;color: black;">&#41;</span>, <span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>dur<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">1</span><span style="color: black;color: black;">&#93;</span><span style="color: black;color: black;">&#41;</span>, <span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>dur<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">2</span><span style="color: black;color: black;">&#93;</span><span style="color: black;color: black;">&#41;</span><span style="color: black;color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">def</span> encode<span style="color: black;color: black;">&#40;</span>source, dest, opts, vbr, abr=<span style="color: #ff4500;color: maroon;">128</span><span style="color: black;color: black;">&#41;</span>:
  command1 = <span style="color: #483d8b;color: maroon;">&quot;ffmpeg -i %s -pass 1 -an -vcodec libx264 -vpre fastfirstpass -b %d -bt %d -threads 0 %s -y pass1.mp4&quot;</span> <span style="color: #66cc66;color: black;">%</span> <span style="color: black;color: black;">&#40;</span>source, vbr, vbr, opts<span style="color: black;color: black;">&#41;</span>
  command2 = <span style="color: #483d8b;color: maroon;">&quot;ffmpeg -i %s -pass 2 -acodec libfaac -ac 2 -ab %dk -vcodec libx264 -vpre hq -b %d -bt %d -threads 0 %s -y %s&quot;</span> <span style="color: #66cc66;color: black;">%</span> <span style="color: black;color: black;">&#40;</span>source, abr, vbr, vbr, opts, dest<span style="color: black;color: black;">&#41;</span>
&nbsp;
  pass1 = Popen<span style="color: black;color: black;">&#40;</span>command1.<span style="color: black;color: black;">split</span><span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#41;</span><span style="color: black;color: black;">&#41;</span>
  pass1.<span style="color: black;color: black;">wait</span><span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#41;</span>
&nbsp;
  pass2 = Popen<span style="color: black;color: black;">&#40;</span>command2.<span style="color: black;color: black;">split</span><span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#41;</span><span style="color: black;color: black;">&#41;</span>
  pass2.<span style="color: black;color: black;">wait</span><span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#41;</span>
&nbsp;
&nbsp;
target_size = <span style="color: #ff4500;color: maroon;">350</span> <span style="color: #808080; font-style: italic;color: green;">#MiB</span>
ab = <span style="color: #ff4500;color: maroon;">128</span> <span style="color: #808080; font-style: italic;color: green;">#kbps</span>
extra_options = <span style="color: #483d8b;color: maroon;">&quot;&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;color: green;"># Parse args</span>
<span style="color: #ff7700;font-weight:bold;color: blue;">try</span>:
  opts, args = <span style="color: #dc143c;color: blue;">getopt</span><span style="color: black;color: black;">&#40;</span>argv<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">1</span>:<span style="color: black;color: black;">&#93;</span>, <span style="color: #483d8b;color: maroon;">&quot;s:a:o:&quot;</span>, <span style="color: black;color: black;">&#91;</span><span style="color: #483d8b;color: maroon;">&quot;size=&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;ab=&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;opts=&quot;</span><span style="color: black;color: black;">&#93;</span><span style="color: black;color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;color: blue;">except</span> GetoptError, err:
  <span style="color: #ff7700;font-weight:bold;color: blue;">print</span> <span style="color: #008000;color: blue;">str</span><span style="color: black;color: black;">&#40;</span>err<span style="color: black;color: black;">&#41;</span>
  exit<span style="color: black;color: black;">&#40;</span><span style="color: #ff4500;color: maroon;">2</span><span style="color: black;color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">for</span> o, a <span style="color: #ff7700;font-weight:bold;color: blue;">in</span> opts:
  <span style="color: #ff7700;font-weight:bold;color: blue;">if</span> o <span style="color: #ff7700;font-weight:bold;color: blue;">in</span> <span style="color: black;color: black;">&#40;</span><span style="color: #483d8b;color: maroon;">&quot;-s&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;--size&quot;</span><span style="color: black;color: black;">&#41;</span>:
    target_size = <span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>a<span style="color: black;color: black;">&#41;</span> <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">1024</span> <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">1024</span>
  <span style="color: #ff7700;font-weight:bold;color: blue;">elif</span> o <span style="color: #ff7700;font-weight:bold;color: blue;">in</span> <span style="color: black;color: black;">&#40;</span><span style="color: #483d8b;color: maroon;">&quot;-a&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;--ab&quot;</span><span style="color: black;color: black;">&#41;</span>:
    ab = <span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>a<span style="color: black;color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;color: blue;">elif</span> o <span style="color: #ff7700;font-weight:bold;color: blue;">in</span> <span style="color: black;color: black;">&#40;</span><span style="color: #483d8b;color: maroon;">&quot;-o&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;--opts&quot;</span><span style="color: black;color: black;">&#41;</span>:
    extra_options = a
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">if</span> <span style="color: #008000;color: blue;">len</span><span style="color: black;color: black;">&#40;</span>args<span style="color: black;color: black;">&#41;</span> <span style="color: #66cc66;color: black;">&lt;</span> <span style="color: #ff4500;color: maroon;">1</span>:
  <span style="color: #ff7700;font-weight:bold;color: blue;">print</span> <span style="color: #483d8b;color: maroon;">&quot;Usage: &quot;</span> + argv<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">0</span><span style="color: black;color: black;">&#93;</span> + \
    <span style="color: #483d8b;color: maroon;">&quot; [-s &lt;size (MiB)&gt;] [-a &lt;audio bit rate (kbps)&gt;] [-o &lt;additional options&gt;] &lt;file list&gt;&quot;</span>
  exit<span style="color: black;color: black;">&#40;</span><span style="color: #ff4500;color: maroon;">2</span><span style="color: black;color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;color: green;"># Loop through files</span>
<span style="color: #ff7700;font-weight:bold;color: blue;">for</span> <span style="color: #008000;color: blue;">file</span> <span style="color: #ff7700;font-weight:bold;color: blue;">in</span> args:
  duration = get_duration<span style="color: black;color: black;">&#40;</span><span style="color: #008000;color: blue;">file</span><span style="color: black;color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;color: blue;">if</span> duration <span style="color: #66cc66;color: black;">&lt;</span>= <span style="color: #ff4500;color: maroon;">0</span>:
    <span style="color: #ff7700;font-weight:bold;color: blue;">print</span> <span style="color: #483d8b;color: maroon;">&quot;Unable to get length of&quot;</span>, <span style="color: #008000;color: blue;">file</span>
    <span style="color: #ff7700;font-weight:bold;color: blue;">continue</span>
&nbsp;
  bitrate = calc_video_bitrate<span style="color: black;color: black;">&#40;</span>target_size, ab, duration<span style="color: black;color: black;">&#41;</span>
&nbsp;
  encode<span style="color: black;color: black;">&#40;</span><span style="color: #008000;color: blue;">file</span>, <span style="color: #008000;color: blue;">file</span> + <span style="color: #483d8b;color: maroon;">&quot;.mp4&quot;</span>, extra_options, bitrate, ab<span style="color: black;color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2009/ffmpeg-target-file-sizes-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Network speed test</title>
		<link>http://www.helyar.net/2009/network-speed-test/</link>
		<comments>http://www.helyar.net/2009/network-speed-test/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 14:56:58 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[network]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=309</guid>
		<description><![CDATA[I currently have the need to test the connection speed between two remote machines, so that I know when it is safe to transfer a large file from one to the other without significantly affecting service by causing long periods of downtime. I couldn&#8217;t find any software that did this easily, ran as a single [...]]]></description>
			<content:encoded><![CDATA[<p>I currently have the need to test the connection speed between two remote machines, so that I know when it is safe to transfer a large file from one to the other without significantly affecting service by causing long periods of downtime. I couldn&#8217;t find any software that did this easily, ran as a single executable (no installation) and was free, so I made one.</p>
<p>This currently only tests speed in one direction (upload from the client to the server) because I have symmetric connections on both machines, so either way should be exactly the same, and so that I can just write a client program and use netcat as a server to accept the packets and dump them to /dev/null. It is simple enough to convert this client into a server so that netcat becomes a client or so that they can be used together. Using netcat as a client probably requires quite a large pre-generated random file and may introduce a bottleneck of the hard drive read speed.</p>
<p>This could also be used in the home for something like testing the effect of wifi strength.</p>
<p>The client takes the command line arguments, connects to the server, generates some random data and sends that data repeatedly until the timer expires. It then prints out the results.</p>
<p>The command to use netcat to listen and ignore any data it receives is <code>nc -lp 9000 > /dev/null</code> though the -p isn&#8217;t always needed (it was needed on my Debian box but not my ESX 4 box).</p>
<p>An example of use of this program as a client is:</p>
<pre>C:\>SpeedTestClient.exe 192.168.1.157 9000
Uploaded 1181614080 bytes in 10.015 seconds.
943875.45 kbps (943.88 mbps)
115219.17 kB/s (112.52 MB/s)</pre>
<p>The results are given in kilobits per second and megabits per second (standard measurements for network speed), kilobytes per second and megabytes per second (more useful for knowing how long a file will take to transfer). In this example, a gigabit ethernet connection is getting roughly 944 mbps.</p>
<p>A few seconds later, VLC 1.0 is opened and paused, so nothing is playing and the test is run again. The results are quite interesting:</p>
<pre>Uploaded 225083392 bytes in 10.015 seconds.
179797.02 kbps (179.80 mbps)
 21947.88 kB/s ( 21.43 MB/s)</pre>
<p>It says it is using next to no system resources when paused but it slows data transfer dramatically and this is with 8GiB of RAM, an otherwise idle CPU and no hard drives being used. When hard drives enter the equation this drops down to about 8MB/s (while VLC is still paused &#8211; I have particularly fast hard drives so I still get about 110MB/s when hard drives are used and VLC is turned off).</p>
<p>The code listing is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: black;"><span style="color: #0600FF;color: blue;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;color: black;">;</span>
<span style="color: #0600FF;color: blue;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;color: black;">;</span>
<span style="color: #0600FF;color: blue;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;color: black;">;</span>
<span style="color: #0600FF;color: blue;">using</span> <span style="color: #008080;">System.Net.Sockets</span><span style="color: #008000;color: black;">;</span>
<span style="color: #0600FF;color: blue;">using</span> <span style="color: #008080;">System.Security.Cryptography</span><span style="color: #008000;color: black;">;</span>
&nbsp;
<span style="color: #0600FF;color: blue;">namespace</span> SpeedTest
<span style="color: #000000;color: black;">&#123;</span>
    <span style="color: #FF0000;color: blue;">class</span> SpeedTestClient
    <span style="color: #000000;color: black;">&#123;</span>
        <span style="color: #0600FF;color: blue;">static</span> <span style="color: #FF0000;color: blue;">int</span> Main<span style="color: #000000;color: black;">&#40;</span><span style="color: #FF0000;color: blue;">string</span><span style="color: #000000;color: black;">&#91;</span><span style="color: #000000;color: black;">&#93;</span> args<span style="color: #000000;color: black;">&#41;</span>
        <span style="color: #000000;color: black;">&#123;</span>
            <span style="color: #0600FF;color: blue;">if</span> <span style="color: #000000;color: black;">&#40;</span>args.<span style="color: #0000FF;color: black;">Length</span> <span style="color: #008000;color: black;">&lt;</span> <span style="color: #FF0000;color: maroon;">2</span><span style="color: #000000;color: black;">&#41;</span>
            <span style="color: #000000;color: black;">&#123;</span>
                Console.<span style="color: #0000FF;color: black;">WriteLine</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #666666;color: maroon;">&quot;Usage: SpeedTestClient &lt;ip&gt; &lt;port&gt; [&lt;seconds&gt;]&quot;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
                <span style="color: #0600FF;color: blue;">return</span> <span style="color: #FF0000;color: maroon;">1</span><span style="color: #008000;color: black;">;</span>
            <span style="color: #000000;color: black;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;color: blue;">try</span>
            <span style="color: #000000;color: black;">&#123;</span>
                <span style="color: #FF0000;color: blue;">ulong</span> bytes <span style="color: #008000;color: black;">=</span> <span style="color: #FF0000;color: maroon;">0</span><span style="color: #008000;color: black;">;</span>
                <span style="color: #FF0000;color: blue;">int</span> startTime <span style="color: #008000;color: black;">=</span> Environment.<span style="color: #0000FF;color: black;">TickCount</span><span style="color: #008000;color: black;">;</span>
                <span style="color: #FF0000;color: blue;">bool</span> started <span style="color: #008000;color: black;">=</span> false<span style="color: #008000;color: black;">;</span>
                <span style="color: #FF0000;color: blue;">int</span> timeout <span style="color: #008000;color: black;">=</span> <span style="color: #FF0000;color: maroon;">10</span><span style="color: #008000;color: black;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;color: green;">// Get the optional number of seconds argument</span>
                <span style="color: #0600FF;color: blue;">if</span> <span style="color: #000000;color: black;">&#40;</span>args.<span style="color: #0000FF;color: black;">Length</span> <span style="color: #008000;color: black;">&gt;</span> <span style="color: #FF0000;color: maroon;">2</span><span style="color: #000000;color: black;">&#41;</span> timeout <span style="color: #008000;color: black;">=</span> <span style="color: #FF0000;color: blue;">int</span>.<span style="color: #0000FF;color: black;">Parse</span><span style="color: #000000;color: black;">&#40;</span>args<span style="color: #000000;color: black;">&#91;</span><span style="color: #FF0000;color: maroon;">2</span><span style="color: #000000;color: black;">&#93;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;color: green;">// Connect to the server via TCP</span>
                TcpClient client <span style="color: #008000;color: black;">=</span> <span style="color: #008000;color: blue;">new</span> TcpClient<span style="color: #000000;color: black;">&#40;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
                client.<span style="color: #0000FF;color: black;">Connect</span><span style="color: #000000;color: black;">&#40;</span>IPAddress.<span style="color: #0000FF;color: black;">Parse</span><span style="color: #000000;color: black;">&#40;</span>args<span style="color: #000000;color: black;">&#91;</span><span style="color: #FF0000;color: maroon;">0</span><span style="color: #000000;color: black;">&#93;</span><span style="color: #000000;color: black;">&#41;</span>, <span style="color: #FF0000;color: blue;">int</span>.<span style="color: #0000FF;color: black;">Parse</span><span style="color: #000000;color: black;">&#40;</span>args<span style="color: #000000;color: black;">&#91;</span><span style="color: #FF0000;color: maroon;">1</span><span style="color: #000000;color: black;">&#93;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
                NetworkStream stream <span style="color: #008000;color: black;">=</span> client.<span style="color: #0000FF;color: black;">GetStream</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;color: green;">// Generate some random data. Buffers too small will bottleneck.</span>
                RandomNumberGenerator rng <span style="color: #008000;color: black;">=</span> RandomNumberGenerator.<span style="color: #0000FF;color: black;">Create</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
                <span style="color: #FF0000;color: blue;">byte</span><span style="color: #000000;color: black;">&#91;</span><span style="color: #000000;color: black;">&#93;</span> buffer <span style="color: #008000;color: black;">=</span> <span style="color: #008000;color: blue;">new</span> <span style="color: #FF0000;color: blue;">byte</span><span style="color: #000000;color: black;">&#91;</span><span style="color: #FF0000;color: maroon;">32768</span><span style="color: #000000;color: black;">&#93;</span><span style="color: #008000;color: black;">;</span>
                rng.<span style="color: #0000FF;color: black;">GetBytes</span><span style="color: #000000;color: black;">&#40;</span>buffer<span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
&nbsp;
                <span style="color: #0600FF;color: blue;">while</span> <span style="color: #000000;color: black;">&#40;</span><span style="color: #0600FF;color: blue;">true</span><span style="color: #000000;color: black;">&#41;</span>
                <span style="color: #000000;color: black;">&#123;</span>
                    <span style="color: #008080; font-style: italic;color: green;">// Write the data</span>
                    stream.<span style="color: #0000FF;color: black;">Write</span><span style="color: #000000;color: black;">&#40;</span>buffer, <span style="color: #FF0000;color: maroon;">0</span>, buffer.<span style="color: #0000FF;color: black;">Length</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
&nbsp;
                    <span style="color: #008080; font-style: italic;color: green;">// Start the timer only after some data has been sent</span>
                    <span style="color: #0600FF;color: blue;">if</span> <span style="color: #000000;color: black;">&#40;</span><span style="color: #008000;color: black;">!</span>started<span style="color: #000000;color: black;">&#41;</span>
                    <span style="color: #000000;color: black;">&#123;</span>
                        startTime <span style="color: #008000;color: black;">=</span> Environment.<span style="color: #0000FF;color: black;">TickCount</span><span style="color: #008000;color: black;">;</span>
                        started <span style="color: #008000;color: black;">=</span> true<span style="color: #008000;color: black;">;</span>
                    <span style="color: #000000;color: black;">&#125;</span>
&nbsp;
                    <span style="color: #008080; font-style: italic;color: green;">// Add to the bytes counter</span>
                    bytes <span style="color: #008000;color: black;">+=</span> <span style="color: #000000;color: black;">&#40;</span><span style="color: #FF0000;color: blue;">ulong</span><span style="color: #000000;color: black;">&#41;</span>buffer.<span style="color: #0000FF;color: black;">LongLength</span><span style="color: #008000;color: black;">;</span>
&nbsp;
                    <span style="color: #0600FF;color: blue;">if</span> <span style="color: #000000;color: black;">&#40;</span>Environment.<span style="color: #0000FF;color: black;">TickCount</span> <span style="color: #008000;color: black;">-</span> startTime <span style="color: #008000;color: black;">&gt;=</span> timeout <span style="color: #008000;color: black;">*</span> <span style="color: #FF0000;color: maroon;">1000</span><span style="color: #000000;color: black;">&#41;</span> break<span style="color: #008000;color: black;">;</span>
                <span style="color: #000000;color: black;">&#125;</span>
&nbsp;
                PrintFinalStats<span style="color: #000000;color: black;">&#40;</span>startTime, Environment.<span style="color: #0000FF;color: black;">TickCount</span>, bytes<span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
&nbsp;
                <span style="color: #0600FF;color: blue;">try</span> <span style="color: #000000;color: black;">&#123;</span> client.<span style="color: #0000FF;color: black;">Close</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span> <span style="color: #000000;color: black;">&#125;</span>
                <span style="color: #0600FF;color: blue;">catch</span> <span style="color: #000000;color: black;">&#40;</span>IOException<span style="color: #000000;color: black;">&#41;</span> <span style="color: #000000;color: black;">&#123;</span> <span style="color: #008080; font-style: italic;color: green;">/* Do nothing */</span> <span style="color: #000000;color: black;">&#125;</span>
            <span style="color: #000000;color: black;">&#125;</span>
            <span style="color: #0600FF;color: blue;">catch</span> <span style="color: #000000;color: black;">&#40;</span>Exception ex<span style="color: #000000;color: black;">&#41;</span>
            <span style="color: #000000;color: black;">&#123;</span>
                Console.<span style="color: #0000FF;color: black;">Error</span>.<span style="color: #0000FF;color: black;">WriteLine</span><span style="color: #000000;color: black;">&#40;</span>ex<span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
                <span style="color: #0600FF;color: blue;">return</span> <span style="color: #FF0000;color: maroon;">1</span><span style="color: #008000;color: black;">;</span>
            <span style="color: #000000;color: black;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;color: blue;">return</span> <span style="color: #FF0000;color: maroon;">0</span><span style="color: #008000;color: black;">;</span>
        <span style="color: #000000;color: black;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;color: blue;">static</span> <span style="color: #0600FF;color: blue;">void</span> PrintFinalStats<span style="color: #000000;color: black;">&#40;</span><span style="color: #FF0000;color: blue;">int</span> start, <span style="color: #FF0000;color: blue;">int</span> end, <span style="color: #FF0000;color: blue;">ulong</span> bytes<span style="color: #000000;color: black;">&#41;</span>
        <span style="color: #000000;color: black;">&#123;</span>
            <span style="color: #FF0000;color: blue;">double</span> seconds <span style="color: #008000;color: black;">=</span> <span style="color: #000000;color: black;">&#40;</span>end <span style="color: #008000;color: black;">-</span> start<span style="color: #000000;color: black;">&#41;</span> <span style="color: #008000;color: black;">/</span> <span style="color: #FF0000;color: maroon;">1000.0</span><span style="color: #008000;color: black;">;</span>
            <span style="color: #FF0000;color: blue;">ulong</span> bits <span style="color: #008000;color: black;">=</span> bytes <span style="color: #008000;color: black;">*</span> <span style="color: #FF0000;color: maroon;">8</span><span style="color: #008000;color: black;">;</span>
&nbsp;
            <span style="color: #FF0000;color: blue;">double</span> bitsPerSecond <span style="color: #008000;color: black;">=</span> bits <span style="color: #008000;color: black;">/</span> seconds<span style="color: #008000;color: black;">;</span>
            <span style="color: #FF0000;color: blue;">double</span> bytesPerSecond <span style="color: #008000;color: black;">=</span> bytes <span style="color: #008000;color: black;">/</span> seconds<span style="color: #008000;color: black;">;</span>
&nbsp;
            <span style="color: #FF0000;color: blue;">string</span> kbps <span style="color: #008000;color: black;">=</span> <span style="color: #FF0000;color: blue;">string</span>.<span style="color: #0000FF;color: black;">Format</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #666666;color: maroon;">&quot;{0:0.00}&quot;</span>, bitsPerSecond <span style="color: #008000;color: black;">/</span> <span style="color: #FF0000;color: maroon;">1000</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
            <span style="color: #FF0000;color: blue;">string</span> mbps <span style="color: #008000;color: black;">=</span> <span style="color: #FF0000;color: blue;">string</span>.<span style="color: #0000FF;color: black;">Format</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #666666;color: maroon;">&quot;{0:0.00}&quot;</span>, bitsPerSecond <span style="color: #008000;color: black;">/</span> <span style="color: #FF0000;color: maroon;">1000</span> <span style="color: #008000;color: black;">/</span> <span style="color: #FF0000;color: maroon;">1000</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
            <span style="color: #FF0000;color: blue;">string</span> kibs <span style="color: #008000;color: black;">=</span> <span style="color: #FF0000;color: blue;">string</span>.<span style="color: #0000FF;color: black;">Format</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #666666;color: maroon;">&quot;{0:0.00}&quot;</span>, bytesPerSecond <span style="color: #008000;color: black;">/</span> <span style="color: #FF0000;color: maroon;">1024</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
            <span style="color: #FF0000;color: blue;">string</span> mibs <span style="color: #008000;color: black;">=</span> <span style="color: #FF0000;color: blue;">string</span>.<span style="color: #0000FF;color: black;">Format</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #666666;color: maroon;">&quot;{0:0.00}&quot;</span>, bytesPerSecond <span style="color: #008000;color: black;">/</span> <span style="color: #FF0000;color: maroon;">1024</span> <span style="color: #008000;color: black;">/</span> <span style="color: #FF0000;color: maroon;">1024</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
&nbsp;
            <span style="color: #FF0000;color: blue;">int</span> kPad <span style="color: #008000;color: black;">=</span> Math.<span style="color: #0000FF;color: black;">Max</span><span style="color: #000000;color: black;">&#40;</span>kbps.<span style="color: #0000FF;color: black;">Length</span>, kibs.<span style="color: #0000FF;color: black;">Length</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
            <span style="color: #FF0000;color: blue;">int</span> mPad <span style="color: #008000;color: black;">=</span> Math.<span style="color: #0000FF;color: black;">Max</span><span style="color: #000000;color: black;">&#40;</span>mbps.<span style="color: #0000FF;color: black;">Length</span>, mibs.<span style="color: #0000FF;color: black;">Length</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
&nbsp;
            Console.<span style="color: #0000FF;color: black;">WriteLine</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #666666;color: maroon;">&quot;Uploaded {0} bytes in {1} seconds.&quot;</span>, bytes, seconds<span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
            Console.<span style="color: #0000FF;color: black;">WriteLine</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #666666;color: maroon;">&quot;{0} kbps ({1} mbps)&quot;</span>, kbps.<span style="color: #0000FF;color: black;">PadLeft</span><span style="color: #000000;color: black;">&#40;</span>kPad<span style="color: #000000;color: black;">&#41;</span>, mbps.<span style="color: #0000FF;color: black;">PadLeft</span><span style="color: #000000;color: black;">&#40;</span>mPad<span style="color: #000000;color: black;">&#41;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
            Console.<span style="color: #0000FF;color: black;">WriteLine</span><span style="color: #000000;color: black;">&#40;</span><span style="color: #666666;color: maroon;">&quot;{0} kB/s ({1} MB/s)&quot;</span>, kibs.<span style="color: #0000FF;color: black;">PadLeft</span><span style="color: #000000;color: black;">&#40;</span>kPad<span style="color: #000000;color: black;">&#41;</span>, mibs.<span style="color: #0000FF;color: black;">PadLeft</span><span style="color: #000000;color: black;">&#40;</span>mPad<span style="color: #000000;color: black;">&#41;</span><span style="color: #000000;color: black;">&#41;</span><span style="color: #008000;color: black;">;</span>
        <span style="color: #000000;color: black;">&#125;</span>
    <span style="color: #000000;color: black;">&#125;</span>
<span style="color: #000000;color: black;">&#125;</span></pre></div></div>

<p>A compiled .net executable is available <a href="http://www.helyar.net/files/SpeedTestClient.exe">here</a>.</p>
<p>P.S. always remember to turn off VLC before transferring files over the network.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2009/network-speed-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to set target file sizes in ffmpeg</title>
		<link>http://www.helyar.net/2009/how-to-set-target-file-sizes-in-ffmpeg/</link>
		<comments>http://www.helyar.net/2009/how-to-set-target-file-sizes-in-ffmpeg/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 21:29:39 +0000</pubDate>
		<dc:creator>George Helyar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[aac]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[x264]]></category>

		<guid isPermaLink="false">http://www.helyar.net/?p=240</guid>
		<description><![CDATA[This script is now available as an ongoing project that can be downloaded. Please see the Video Encoding Project. This post describes the methods and mathematics behind the script. After much searching, I found almost nothing regarding how to target a specific file size with ffmpeg, such as 700MiB to fit on a CD. Almost [...]]]></description>
			<content:encoded><![CDATA[<p>This script is now available as an ongoing project that can be downloaded. Please see the <a href="http://www.helyar.net/enc">Video Encoding Project</a>. This post describes the methods and mathematics behind the script.<br />
<hr />
<p>After much searching, I found almost nothing regarding how to target a specific file size with ffmpeg, such as 700MiB to fit on a CD. Almost everything I found suggested to use a negative bitrate for mencoder, such as -700000. However, this did not work for me at all (even by copying and pasting verbatim). Because of this, I set about doing it manually with some mathematics.</p>
<p>What I am going to do here assumes a single audio stream, a single video stream and nothing else in the output file at all (i.e. no subtitles, no alternate languages, no director&#8217;s commentaries, etc).</p>
<p>When encoding media, audio uses a constant bit rate. This means that you can calculate how much space it will take if you know which bit rate you are using and how long the stream is in seconds. This also means that to know how big the video stream should be, we can calculate how big the audio stream will be and subtract it from the target file size. For example, if the target file size is 350MiB and the audio will take up 30MiB, we know the video should aim to be 320MiB (ignoring overheads in the multiplexing, container headers, etc). We can use the same calculations in reverse to work out an average or constant bit rate from a file size and a length in seconds.</p>
<p>In this example, I will be using <a href="http://www.amazon.co.uk/Dads-Army-Complete-Collection-DVD/dp/B000VA3J7K">Dad&#8217;s Army</a> Series 6 Episode 1 &#8211; The Deadly Attachment. I have already ripped this from the DVD with <a href="http://www.slysoft.com/en/anydvd.html">SlySoft AnyDVD</a> and PgcDemux on Windows. Automating PgcDemux rips is worth a whole entry in itself. Note that I have bought this legally and I do not intend to sell or share it. I just want a copy that I can store on my network and play from any computer. Storing 14 DVD images on hard drives takes quite a lot of space, which is where encoding comes in handy. In fact, some of my box sets contain over 60 DVDs (roughly 270GiB).</p>
<p>&#8212;&#8212;&#8212;-<br />
To install ffmpeg on Debian, it is best to get it from the Debian Multimedia repository. Add</p>
<pre>deb http://www.debian-multimedia.org lenny main
deb-src http://www.debian-multimedia.org lenny main</pre>
<p>to your /etc/apt/sources.list and then run</p>
<pre># apt-get update
# apt-get install debian-multimedia-keyring
# apt-get update
# apt-get install ffmpeg</pre>
<p>&#8212;&#8212;&#8212;-</p>
<p>The first thing we need to do to put this into practice is to get the length of the stream. If you supply the file to ffmpeg without telling it to do anything, it will give you an error. However, this error already contains all of the information we need:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;color: black;">$ <span style="color: #c20cb9; font-weight: bold;color: blue;">ffmpeg</span> <span style="color: #660033;">-i</span> da_6_01.VOB
FFmpeg version SVN-r13582, Copyright <span style="color: #7a0874; font-weight: bold;color: black;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;color: black;">&#41;</span> <span style="color: #000000;color: maroon;">2000</span>-<span style="color: #000000;color: maroon;">2008</span> Fabrice Bellard, et al.
  configuration: <span style="color: #660033;">--prefix</span>=<span style="color: #000000; font-weight: bold;color: black;">/</span>usr ...
  built on May  <span style="color: #000000;color: maroon;">3</span> <span style="color: #000000;color: maroon;">2009</span> <span style="color: #000000;color: maroon;">12</span>:07:<span style="color: #000000;color: maroon;">18</span>, <span style="color: #c20cb9; font-weight: bold;color: blue;">gcc</span>: 4.3.2
Input <span style="color: #666666; font-style: italic;">#0, mpeg, from 'da_6_01.VOB':</span>
  Duration: 00:<span style="color: #000000;color: maroon;">29</span>:<span style="color: #000000;color: maroon;">23.90</span>, start: <span style="color: #000000;color: maroon;">0.287267</span>, bitrate: <span style="color: #000000;color: maroon;">5987</span> kb<span style="color: #000000; font-weight: bold;color: black;">/</span>s
    Stream <span style="color: #666666; font-style: italic;">#0.0[0x1e0]: Video: mpeg2video, yuv420p, 720x576 [PAR 16:15 DAR 4:3], 9800 kb/s, 25.00 tb(r)</span>
    Stream <span style="color: #666666; font-style: italic;">#0.1[0x80]: Audio: ac3, 48000 Hz, stereo, 192 kb/s</span>
Must supply at least one output <span style="color: #c20cb9; font-weight: bold;color: blue;">file</span></pre></div></div>

<p>We can see that the duration is 00:29:23.90.</p>
<p>As this is an error, we need to redirect stderr to stdout. This can be done with 2>&#038;1. We then need the line that contains the &#8220;Duration&#8221;, which we can get with grep. We can cut this line up to take the time out.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;color: black;">$ <span style="color: #c20cb9; font-weight: bold;color: blue;">ffmpeg</span> <span style="color: #660033;">-i</span> da_6_01.VOB <span style="color: #000000;color: maroon;">2</span><span style="color: #000000; font-weight: bold;color: black;">&gt;&amp;</span><span style="color: #000000;color: maroon;">1</span> <span style="color: #000000; font-weight: bold;color: black;">|</span> <span style="color: #c20cb9; font-weight: bold;color: blue;">grep</span> Duration <span style="color: #000000; font-weight: bold;color: black;">|</span> <span style="color: #c20cb9; font-weight: bold;color: blue;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">' '</span> <span style="color: #660033;">-f</span> <span style="color: #000000;color: maroon;">4</span> <span style="color: #000000; font-weight: bold;color: black;">|</span> <span style="color: #c20cb9; font-weight: bold;color: blue;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">'.'</span> <span style="color: #660033;">-f</span> <span style="color: #000000;color: maroon;">1</span>
00:<span style="color: #000000;color: maroon;">29</span>:<span style="color: #000000;color: maroon;">23</span></pre></div></div>

<p>This can be converted into seconds with the formula <code>s' = (h * 60 + m) * 60 + s</code>.</p>
<p>I used a python script to do this and to perform the necessary calculations mentioned earlier. Note that most of this python script is just for the convenience of getopts and that, if needed, it can be cut down significantly at the cost of flexibility. If you always want the same output size and audio bitrate, this script can be simplified to just a couple of lines.</p>
<p>calc_bitrate.py:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;color: black;"><span style="color: #808080; font-style: italic;color: green;">#!/usr/bin/python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">from</span> <span style="color: #dc143c;color: blue;">getopt</span> <span style="color: #ff7700;font-weight:bold;color: blue;">import</span> <span style="color: #dc143c;color: blue;">getopt</span>, GetoptError
<span style="color: #ff7700;font-weight:bold;color: blue;">from</span> <span style="color: #dc143c;color: blue;">sys</span> <span style="color: #ff7700;font-weight:bold;color: blue;">import</span> argv, exit
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">def</span> secs<span style="color: black;color: black;">&#40;</span>h, m, s<span style="color: black;color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;color: blue;">return</span> <span style="color: black;color: black;">&#40;</span>h <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">60</span> + m<span style="color: black;color: black;">&#41;</span> <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">60</span> + s
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">def</span> calc_video_bitrate<span style="color: black;color: black;">&#40;</span>target_size, abr, <span style="color: #dc143c;color: blue;">time</span><span style="color: black;color: black;">&#41;</span>:
  target_bytes = target_size <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">1024</span> <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">1024</span>
  audio_bytes = <span style="color: black;color: black;">&#40;</span><span style="color: black;color: black;">&#40;</span>abr <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">1000</span><span style="color: black;color: black;">&#41;</span> / <span style="color: #ff4500;color: maroon;">8</span><span style="color: black;color: black;">&#41;</span> <span style="color: #66cc66;color: black;">*</span> <span style="color: #dc143c;color: blue;">time</span>
  <span style="color: #ff7700;font-weight:bold;color: blue;">return</span> <span style="color: black;color: black;">&#40;</span>target_bytes - audio_bytes<span style="color: black;color: black;">&#41;</span> / <span style="color: #dc143c;color: blue;">time</span> <span style="color: #66cc66;color: black;">*</span> <span style="color: #ff4500;color: maroon;">8</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">try</span>:
  opts, args = <span style="color: #dc143c;color: blue;">getopt</span><span style="color: black;color: black;">&#40;</span>argv<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">1</span>:<span style="color: black;color: black;">&#93;</span>, <span style="color: #483d8b;color: maroon;">&quot;s:a:t:&quot;</span>, <span style="color: black;color: black;">&#91;</span><span style="color: #483d8b;color: maroon;">&quot;size=&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;abr=&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;time=&quot;</span><span style="color: black;color: black;">&#93;</span><span style="color: black;color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;color: blue;">except</span> GetoptError, err:
  <span style="color: #ff7700;font-weight:bold;color: blue;">print</span> <span style="color: #008000;color: blue;">str</span><span style="color: black;color: black;">&#40;</span>err<span style="color: black;color: black;">&#41;</span>
  exit<span style="color: black;color: black;">&#40;</span><span style="color: #ff4500;color: maroon;">2</span><span style="color: black;color: black;">&#41;</span>
&nbsp;
target_size = <span style="color: #ff4500;color: maroon;">350</span> <span style="color: #808080; font-style: italic;color: green;">#MiB</span>
abr = <span style="color: #ff4500;color: maroon;">128</span> <span style="color: #808080; font-style: italic;color: green;">#kbps</span>
<span style="color: #dc143c;color: blue;">time</span> = <span style="color: #ff4500;color: maroon;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">for</span> o, a <span style="color: #ff7700;font-weight:bold;color: blue;">in</span> opts:
  <span style="color: #ff7700;font-weight:bold;color: blue;">if</span> o <span style="color: #ff7700;font-weight:bold;color: blue;">in</span> <span style="color: black;color: black;">&#40;</span><span style="color: #483d8b;color: maroon;">&quot;-s&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;--size&quot;</span><span style="color: black;color: black;">&#41;</span>:
    target_size = <span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>a<span style="color: black;color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;color: blue;">elif</span> o <span style="color: #ff7700;font-weight:bold;color: blue;">in</span> <span style="color: black;color: black;">&#40;</span><span style="color: #483d8b;color: maroon;">&quot;-a&quot;</span>, <span style="color: #483d8b;color: maroon;">&quot;--abr&quot;</span><span style="color: black;color: black;">&#41;</span>:
    abr = <span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>a<span style="color: black;color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">if</span> <span style="color: #008000;color: blue;">len</span><span style="color: black;color: black;">&#40;</span>args<span style="color: black;color: black;">&#41;</span> <span style="color: #66cc66;color: black;">&lt;</span> <span style="color: #ff4500;color: maroon;">1</span>:
  <span style="color: #ff7700;font-weight:bold;color: blue;">print</span> <span style="color: #483d8b;color: maroon;">&quot;Usage: &quot;</span> + argv<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">0</span><span style="color: black;color: black;">&#93;</span> + <span style="color: #483d8b;color: maroon;">&quot; [-s &lt;size (M)&gt;] [-a &lt;audio bit rate (k)&gt;] [[hh:]mm:]ss&quot;</span>
  exit<span style="color: black;color: black;">&#40;</span><span style="color: #ff4500;color: maroon;">2</span><span style="color: black;color: black;">&#41;</span>
&nbsp;
time_part = args<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">0</span><span style="color: black;color: black;">&#93;</span>.<span style="color: black;color: black;">split</span><span style="color: black;color: black;">&#40;</span><span style="color: #483d8b;color: maroon;">&quot;:&quot;</span><span style="color: black;color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;color: blue;">if</span> <span style="color: #008000;color: blue;">len</span><span style="color: black;color: black;">&#40;</span>time_part<span style="color: black;color: black;">&#41;</span> <span style="color: #66cc66;color: black;">&lt;</span> <span style="color: #ff4500;color: maroon;">2</span>: time_part = <span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">0</span><span style="color: black;color: black;">&#93;</span> + time_part
<span style="color: #ff7700;font-weight:bold;color: blue;">if</span> <span style="color: #008000;color: blue;">len</span><span style="color: black;color: black;">&#40;</span>time_part<span style="color: black;color: black;">&#41;</span> <span style="color: #66cc66;color: black;">&lt;</span> <span style="color: #ff4500;color: maroon;">3</span>: time_part = <span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">0</span><span style="color: black;color: black;">&#93;</span> + time_part
<span style="color: #dc143c;color: blue;">time</span> = secs<span style="color: black;color: black;">&#40;</span><span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>time_part<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">0</span><span style="color: black;color: black;">&#93;</span><span style="color: black;color: black;">&#41;</span>, <span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>time_part<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">1</span><span style="color: black;color: black;">&#93;</span><span style="color: black;color: black;">&#41;</span>, <span style="color: #008000;color: blue;">int</span><span style="color: black;color: black;">&#40;</span>time_part<span style="color: black;color: black;">&#91;</span><span style="color: #ff4500;color: maroon;">2</span><span style="color: black;color: black;">&#93;</span><span style="color: black;color: black;">&#41;</span><span style="color: black;color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;color: blue;">print</span> <span style="color: #483d8b;color: maroon;">&quot;%d&quot;</span> <span style="color: #66cc66;color: black;">%</span> calc_video_bitrate<span style="color: black;color: black;">&#40;</span>target_size, abr, <span style="color: #dc143c;color: blue;">time</span><span style="color: black;color: black;">&#41;</span></pre></div></div>

<p>A typical usage for this would be <code>./calc_python.py -s 350 -a 128 29:23</code>.</p>
<p>We can then use a combination of these inside a shell script easily. I&#8217;m using bash syntax just to nest the commands into a single line but for compatibility with other shells, backticks (`) can be used and this is what I normally prefer. This script loops through all of the .VOB files in the directory and gives their target video bitrate for a 350MiB output and 128kbps audio.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;color: black;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #000000; font-weight: bold;color: blue;">for</span> vob <span style="color: #000000; font-weight: bold;color: blue;">in</span> <span style="color: #000000; font-weight: bold;color: black;">*</span>.VOB
<span style="color: #000000; font-weight: bold;color: blue;">do</span>
  <span style="color: #007800;">BR</span>=$<span style="color: #7a0874; font-weight: bold;color: black;">&#40;</span>.<span style="color: #000000; font-weight: bold;color: black;">/</span>calc_bitrate.py <span style="color: #660033;">-s</span> <span style="color: #000000;color: maroon;">350</span> <span style="color: #660033;">-a</span> <span style="color: #000000;color: maroon;">128</span> $<span style="color: #7a0874; font-weight: bold;color: black;">&#40;</span><span style="color: #c20cb9; font-weight: bold;color: blue;">ffmpeg</span> <span style="color: #660033;">-i</span> <span style="color: #007800;color: black;">$vob</span> <span style="color: #000000;color: maroon;">2</span><span style="color: #000000; font-weight: bold;color: black;">&gt;&amp;</span><span style="color: #000000;color: maroon;">1</span> <span style="color: #000000; font-weight: bold;color: black;">|</span> <span style="color: #c20cb9; font-weight: bold;color: blue;">grep</span> Duration <span style="color: #000000; font-weight: bold;color: black;">|</span> <span style="color: #c20cb9; font-weight: bold;color: blue;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">' '</span> <span style="color: #660033;">-f</span> <span style="color: #000000;color: maroon;">4</span> <span style="color: #000000; font-weight: bold;color: black;">|</span> <span style="color: #c20cb9; font-weight: bold;color: blue;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">'.'</span> <span style="color: #660033;">-f</span> <span style="color: #000000;color: maroon;">1</span><span style="color: #7a0874; font-weight: bold;color: black;">&#41;</span><span style="color: #7a0874; font-weight: bold;color: black;">&#41;</span>
  <span style="color: #7a0874; font-weight: bold;color: blue;">echo</span> <span style="color: #007800;color: black;">$vob</span> <span style="color: #007800;color: black;">$BR</span>
<span style="color: #000000; font-weight: bold;color: blue;">done</span></pre></div></div>

<p>I use this in 2-pass VBR mode with the libx264 video encoder and libfaac audio encoder in an mp4 container just by replacing the &#8220;echo&#8221; line of the above script with the following 2 lines:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;color: black;"><span style="color: #c20cb9; font-weight: bold;color: blue;">ffmpeg</span> <span style="color: #660033;">-i</span> <span style="color: #007800;color: black;">$vob</span> <span style="color: #660033;">-an</span> <span style="color: #660033;">-pass</span> <span style="color: #000000;color: maroon;">1</span> <span style="color: #660033;">-vcodec</span> libx264 <span style="color: #660033;">-vpre</span> fastfirstpass <span style="color: #660033;">-b</span> <span style="color: #007800;color: black;">$BR</span> <span style="color: #660033;">-bt</span> <span style="color: #007800;color: black;">$BR</span> <span style="color: #660033;">-deinterlace</span> <span style="color: #660033;">-threads</span> <span style="color: #000000;color: maroon;">0</span> <span style="color: #660033;">-y</span> pass1.mp4
<span style="color: #c20cb9; font-weight: bold;color: blue;">ffmpeg</span> <span style="color: #660033;">-i</span> <span style="color: #007800;color: black;">$vob</span> <span style="color: #660033;">-acodec</span> libfaac <span style="color: #660033;">-ab</span> 128k <span style="color: #660033;">-ac</span> <span style="color: #000000;color: maroon;">2</span> <span style="color: #660033;">-pass</span> <span style="color: #000000;color: maroon;">2</span> <span style="color: #660033;">-vcodec</span> libx264 <span style="color: #660033;">-vpre</span> hq <span style="color: #660033;">-b</span> <span style="color: #007800;color: black;">$BR</span> <span style="color: #660033;">-bt</span> <span style="color: #007800;color: black;">$BR</span> <span style="color: #660033;">-deinterlace</span> <span style="color: #660033;">-threads</span> <span style="color: #000000;color: maroon;">0</span> <span style="color: #660033;">-y</span> <span style="color: #000000; font-weight: bold;color: black;">`</span><span style="color: #7a0874; font-weight: bold;color: blue;">echo</span> <span style="color: #007800;color: black;">$vob</span> <span style="color: #000000; font-weight: bold;color: black;">|</span> <span style="color: #c20cb9; font-weight: bold;color: blue;">sed</span> <span style="color: #ff0000;">'s/VOB/mp4/'</span><span style="color: #000000; font-weight: bold;color: black;">`</span></pre></div></div>

<p>I have 2 versions of this script, one with &#8220;-deinterlace&#8221; and one without. PAL video (576i &#8211; used in Europe), such as Dad&#8217;s Army needs deinterlacing. NTSC video (480 &#8211; used in North America) usually does not.</p>
<p>For ripping DVDs to H.264 and maintaining the quality, I wouldn&#8217;t use a bitrate lower than 1000k or an audio bitrate lower than 128k stereo. This means that for getting media onto a CD, you probably want no smaller than 350MiB for 30 to 40 minutes or 700MiB for 1 hour or more, depending on your resolution. Also note that I only intend to play these in computers and fitting them onto CDs is just a convenience &#8211; standalone DVD players will not be able to play H.264 or AAC (Blu-ray and HDDVD players may be able to &#8211; mp4 containers and reading CDs would be more of an issue).</p>
<h3>Update:</h3>
<p>I have successfully tested this in a LG BD370 Blu-ray player.</p>
<p>I have not tried actually burning these onto CDs yet so if 2 won&#8217;t fit on a 700MiB CD, even with overburn (because of overheads that were ignored, as stated earlier) then the simplest solution is to just target a smaller file size with something like &#8220;-s 348&#8243;.</p>
<h2>The result</h2>
<p>Dad&#8217;s Army episodes are not all of the same length. Some are several minutes longer than others. The following shows how no matter the size of the input file, they all produce the same size output file:
<pre>$ ll da_5_*.VOB
-r--r--r-- 1 mythtv mythtv 1364660224 2009-06-04 00:01 da_5_01.VOB
-r--r--r-- 1 mythtv mythtv 1360699392 2009-06-04 00:01 da_5_02.VOB
-r--r--r-- 1 mythtv mythtv  956084224 2009-06-04 00:02 da_5_03.VOB
-r--r--r-- 1 mythtv mythtv 1355888640 2009-06-04 00:02 da_5_04.VOB
-r--r--r-- 1 mythtv mythtv 1114351616 2009-06-03 23:59 da_5_05.VOB
-r--r--r-- 1 mythtv mythtv 1034289152 2009-06-04 00:00 da_5_06.VOB
-r--r--r-- 1 mythtv mythtv 1045202944 2009-06-04 00:00 da_5_07.VOB
-r--r--r-- 1 mythtv mythtv 1345318912 2009-06-04 00:00 da_5_08.VOB
-r--r--r-- 1 mythtv mythtv 1391529984 2009-06-04 00:02 da_5_09.VOB
-r--r--r-- 1 mythtv mythtv 1342748672 2009-06-04 00:03 da_5_10.VOB
-r--r--r-- 1 mythtv mythtv 1158017024 2009-06-04 00:03 da_5_11.VOB
-r--r--r-- 1 mythtv mythtv 1157097472 2009-06-04 00:03 da_5_12.VOB
-r--r--r-- 1 mythtv mythtv 1180098560 2009-06-04 00:04 da_5_13.VOB
$ ll -h da_5_*.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 06:18 da_5_01.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 06:55 da_5_02.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 07:30 da_5_03.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 08:08 da_5_04.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 08:44 da_5_05.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 09:21 da_5_06.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 09:58 da_5_07.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 10:36 da_5_08.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 11:15 da_5_09.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 11:54 da_5_10.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 12:29 da_5_11.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 13:05 da_5_12.mp4
-rw-r--r-- 1 mythtv mythtv 352M 2009-06-17 13:41 da_5_13.mp4</pre>
<h2>Update:</h2>
<p>A pure python replacement script is available <a href="http://www.helyar.net/2009/ffmpeg-target-file-sizes-again/">here</a> that simplifies use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.helyar.net/2009/how-to-set-target-file-sizes-in-ffmpeg/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
