<?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>code-diesel &#187; tutorial</title>
	<atom:link href="http://www.codediesel.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>FFmpeg: A beginners guide &#8211; part 2</title>
		<link>http://www.codediesel.com/tutorial/ffmpeg-a-beginners-guide-part-2/</link>
		<comments>http://www.codediesel.com/tutorial/ffmpeg-a-beginners-guide-part-2/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 10:38:20 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[ffmpeg]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2993</guid>
		<description><![CDATA[Continuing the last post on FFmpeg, here we will discusses various fundamental tasks you can accomplish with the audio stream in FFmpeg. 2.1.1 Introduction to Transcoding One of the basic tasks you can perform on an audio track in FFmpeg is to convert it into another format. This process known as Transcoding, is the direct [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing the last post on FFmpeg, here we will discusses various fundamental tasks you can accomplish with the audio stream in FFmpeg.</p>
<h4>2.1.1  Introduction to Transcoding</h4>
<p>One of the basic tasks you can perform on an audio track in FFmpeg is to convert it into another format. This process known as Transcoding, is the direct digital-to-digital conversion of one stream encoding to another, whether video or audio. Transcoding is usually done in cases where a target device – media player such as iPod, iPAD, DVD players or a software application, does not support the format or has limited storage capacity that requires a condensed file size. Transcoding can also be used to convert an incompatible or obsolete format to a better-supported format.<br />
<span id="more-2993"></span><br />
Transcoding is generally a “lossy process” &#8211; a data encoding method which compresses data by discarding (losing) some of it to minimize the amount of data that need to be stored in a file; however, transcoding can also be “lossless” if the input is losslessly compressed and the output is either losslessly compressed or stored in a uncompressed state. Although compression can reduce file size considerably, repeatedly performing transcoding on a single file using lossy compression can create a ‘generation loss’ – a reduction in the quality of the audio when copying, which would cause further reduction in quality on making a copy of the copy. So you need to keep this in mind while repeatedly transcoding between various formats. </p>
<p>Although I could not show you here the difference between an original and lossy audio compression (due to the limitation of the media of course), the following shows an example of a lossy compression in an image. The original JPG image is on the left and a lossy image of the same after repeated compression is shown on the right. As we lose precious information forever during compression, we cannot get back the original image using the compressed image.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2012/02/compressed-image.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2012/02/compressed-image.jpg" alt="" title="compressed-image" width="491" height="189" class="aligncenter size-full wp-image-2995" /></a></p>
<h4>2.1.2  Audio compression</h4>
<p>Audio compression is a form of data compression designed to reduce the transmission bandwidth and storage requirement of a digital audio stream. Audio compression algorithms are implemented in software as audio codec’s, &#8211; which is a software program or library capable of encoding/decoding a digital audio stream. </p>
<p>Audio compression is either lossy or lossless as discussed earlier. Lossless audio compression produces a version of digital audio that can be decoded to an exact digital duplicate of the original audio stream. This is in contrast to the irreversible changes upon playback from lossy compression techniques such as Vorbis and MP3.</p>
<p>The whole idea behind audio compression in FFmpeg is to lower the audio bitrate (96kbps, 128kbps, 192 kbps etc.), this effectively also reduces the fidelity or quality of the audio. So you want to keep in mind that, a high bitrate audio file confirms a better sound quality, so by lowering its bitrate you are actually degrading the quality.<br />
For normal computer use, the 128kbs rate produces a quality equal to that of an audio CD. But in the case of an MP3 use, it is necessary to use a 256kbs bitrate to reach an identical result to that of the CD quality sound.               </p>
<h4>2.1.3  Getting your audio file ready</h4>
<p>Now that we have gone through a short introduction to compression, we will now work on the process of transcoding audio files. </p>
<p>To run the example commands in this section, you will need an audio file in a .wav or an .mp3 format. You can get hold of a wav file by ripping an audio track from a music CD or downloading an mp3 file from the Internet. Call the resulting file ‘myaudio.mp3’. For this section I used the ‘Solo Piano 7’ Opening file from <a href="http://www.archive.org/details/solo-piano-7" rel="nofollow"  target="_blank">http://www.archive.org/details/solo-piano-7</a>.</p>
<p>Next, we will get ffmpeg to identify the file. This will tell us the various details of the audio file. The simple way to get this information is to just tell ffmpeg to use it for input. For this we need to use the –i option. Enter the following command at your prompt.</p>

<div class="wp_codebox"><table><tr id="p299319"><td class="code" id="p2993code19"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span>  myaudio.mp3</pre></td></tr></table></div>

<p>The exact output on my PC is shown below; which may differ from yours depending on the version of ffmpeg you are using.</p>
<blockquote><p>
D:\ffmpeg>ffmpeg  -i  myaudio.mp3<br />
ffmpeg version N-31100-g9251942, Copyright (c) 2000-2011 the FFmpeg developers<br />
Input #0, mp3, from &#8216;myaudio.mp3&#8242;:<br />
  Metadata:<br />
    album           : solo piano 7<br />
    artist          : Torley<br />
    album_artist    : Torley<br />
    composer        : Torley<br />
    genre           : Piano<br />
    track           : 001/176<br />
    title           : 001 – Openings<br />
    date            : 2008<br />
  Duration: 00:01:39.50, start: 0.000000, bitrate: 193 kb/s<br />
  Stream #0.0: Audio: mp3, 44100 Hz, stereo, s16, 192 kb/s<br />
At least one output file must be specified
</p></blockquote>
<p>There is a lot of information we can gather from the output &#8211; the track is<br />
1 minute 39.50 seconds long, the bitrate is 193kb/s, the audio is encoded in mp3 format at 44100Hz (44.1KHz) and has two channels (stereo). All this information will come in handy during a transcoding process.</p>
<h4>2.1.4  Transcoding to a different format</h4>
<p>Let us now convert the downloaded file to a simple wav format.  Notice that we have not specified any format option or flag, just the complete output filename. FFmpeg automatically guesses which encoders to use by noticing the format of the input and output files, this can be a big help if you keep forgetting the option name or are just being lazy. If you are not going to specify the encoder format, make sure you mention the full filename, along with the appropriate format extension.</p>

<div class="wp_codebox"><table><tr id="p299320"><td class="code" id="p2993code20"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span>  myaudio.mp3  myaudio.wav</pre></td></tr></table></div>

<p>The output of the command is shown below.</p>
<blockquote><p>
ffmpeg version N-31100-g9251942, Copyright (c) 2000-2011 the FFmpeg developers<br />
Input #0, mp3, from &#8216;myaudio.mp3&#8242;:<br />
  Metadata:<br />
    album           : solo piano 7<br />
    artist          : Torley<br />
    album_artist    : Torley<br />
    composer        : Torley<br />
    genre           : Piano<br />
    track           : 001/176<br />
    title           : 001 &#8211; Openings<br />
    date            : 2008<br />
  Duration: 00:01:39.50, start: 0.000000, bitrate: 193 kb/s<br />
    Stream #0.0: Audio: mp3, 44100 Hz, stereo, s16, 192 kb/s<br />
File &#8216;myaudio.wav&#8217; already exists. Overwrite ? [y/N] y<br />
Output #0, wav, to &#8216;myaudio.wav&#8217;:<br />
  Metadata:<br />
    album           : solo piano 7<br />
    artist          : Torley<br />
    album_artist    : Torley<br />
    composer        : Torley<br />
    genre           : Piano<br />
    track           : 001/176<br />
    title           : 001 &#8211; Openings<br />
    date            : 2008<br />
    encoder         : Lavf53.4.0<br />
    Stream #0.0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s<br />
Stream mapping:<br />
  Stream #0.0 -> #0.0<br />
Press [q] to stop, [?] for help<br />
size=   17141kB time=00:01:39.50 bitrate=1411.2kbits/s<br />
video:0kB audio:17141kB global headers:0kB muxing overhead 0.000251%
</p></blockquote>
<p>Notice how large the resulting wav file is (17 Mb) as compared to the original mp3 format (2.1 Mb). This being for the reason that the wav file is not compressed like its mp3 counterpart.  Incidentally, the audio format of the wav is Pulse-code modulation (PCM), technically PCM signed 16 bit little-endian format.</p>
<p>As you can see from the screenshot above the output of an ffmpeg command is quite large, so from here on I’ll just specify the command and do away with the output screen unless it is required for explanation.</p>
<h4>2.1.5  Changing the bitrate of the audio</h4>
<p>As we learned in Chapter 1, bitrates control the file size and the quality of an audio or video stream. Lowering the bitrate will result not only in a reduced file size but also diminish the quality of the final output. This can be required if you have a high quality audio recording and need to lower the quality for a reduced file size to stream over the Web. For example the following command will set the bitrate of the mp3 file to 64kb/s. This uses the –ab option to the job.</p>
<p>-ab <value></p>

<div class="wp_codebox"><table><tr id="p299321"><td class="code" id="p2993code21"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span>  myaudio.mp3  <span style="color: #660033;">-ab</span> 64k  out.mp3</pre></td></tr></table></div>

<p>The higher the value the better is the audio quality. This is one of the important factors responsible for the audio quality. But that doesn&#8217;t mean you can make a poor audio file sound better by increasing its bitrate. The resultant file will just be of bigger size. </p>
<p>Another example &#8211; to transcode an mp3 file to an AAC format, with a bitrate of 128K, we can use the following.</p>

<div class="wp_codebox"><table><tr id="p299322"><td class="code" id="p2993code22"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span>  myaudio.mp3  <span style="color: #660033;">-ab</span> 128k  myaudio.aac</pre></td></tr></table></div>

<p>As we saw earlier the original audio track has 2 channels (stereo). Many times it is not necessary to have 2 channels, like in a speech recording, where its really doesn’t matter.. In such cases you can further reduce the file size by setting the audio channels to mono or ‘1’. For output streams it is set by default to the number of input audio channels. </p>
<p>-ac <value></p>

<div class="wp_codebox"><table><tr id="p299323"><td class="code" id="p2993code23"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span>  myaudio.mp3  <span style="color: #660033;">-ac</span> <span style="color: #000000;">1</span> out.mp3</pre></td></tr></table></div>

<p>Note that once you convert a stereo channel to a mono, you cannot convert it back to a stereo channel audio. That information is lost forever. The same thing happens with bitrates. Once you reduce a bitrate of an audio file, you cannot just increase the bitrate back again to get the original quality. That information is already gone. So as a precaution, never work with your original media files. Make a copy of the original and work with the copy.</p>
<p>The other important audio option is –acodec. This option lets you choose the type of audio codec you want to use. e.g. if you are using ffmpeg on a mp3 file, then it will need the audio codec libmp3lame. You can specify it using -acodec libmp3lame. Although, by default, ffmpeg takes care of the codecs you need (by guessing it from the output file format) but if you need anything different, then go for this option.  FFmpeg uses a default encoder for each audio stream, using the output file extension to guess the encoder to use. This option lets you force FFmpeg to use a specific audio encoder rather than the default. The following for example will extract the audio stream from a .flv video and save it as an .mp3 file using the libmp3lame encoder.</p>
<p>-acodec <encoder/decoder></p>

<div class="wp_codebox"><table><tr id="p299324"><td class="code" id="p2993code24"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span>  myvideo.flv   <span style="color: #660033;">-acodec</span>  libmp3lame  myaudio.mp3</pre></td></tr></table></div>

<p>Sometime you FFmpeg may be unable to correctly decode the input file, giving the error something like the following.</p>

<div class="wp_codebox"><table><tr id="p299325"><td class="code" id="p2993code25"><pre class="text" style="font-family:monospace;">Error while decoding stream #0.0</pre></td></tr></table></div>

<p>In such cases you can force FFmpeg to use a particular decoder to decode the input file. The following example will force FFmpeg to use the mp3 codec toe decode the input file audio.</p>

<div class="wp_codebox"><table><tr id="p299326"><td class="code" id="p2993code26"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-acodec</span>  libmp3lame  <span style="color: #660033;">-i</span>  myvideo.flv    myaudio.mp3</pre></td></tr></table></div>

<p>Note that the –acodec option comes before the –i option when we want the codec to apply to the input stream and comes after the –i option when we want the codec to apply to the output stream. To see what codecs are available on your system, issue the following command.</p>

<div class="wp_codebox"><table><tr id="p299327"><td class="code" id="p2993code27"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-codecs</span></pre></td></tr></table></div>

<p>Sometimes you may want to completely disable the audio recording for which we can use the –an option. This can be used to strip out an audio stream from a video file. When you use this option, all the other audio related attributes are cancelled out, which is fine, as they would not matter without the audio. So for example you are want to disable the audio from a video file and only copy the video stream, you can use the following.</p>

<div class="wp_codebox"><table><tr id="p299328"><td class="code" id="p2993code28"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span>  myvideo.flv  <span style="color: #660033;">-an</span> out.flv</pre></td></tr></table></div>

<p>Another important option is –ar, the audio sampling frequency. This lets you set the maximum sampling frequency of the audio stream. Audio sampling was discussed in Chapter 1. You can use the option to reduce the sampling frequency to a lower value to reduce file storage or Internet bandwidth capacity. The default value is set at 44100Hz.  The value is given in Hz. So the following will resample the input audio to 11025Hz with a single channel (mono).</p>
<p>-ar <value in Hz></p>

<div class="wp_codebox"><table><tr id="p299329"><td class="code" id="p2993code29"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span>  myaudio.mp3  <span style="color: #660033;">-ar</span> <span style="color: #000000;">11025</span>  <span style="color: #660033;">-ac</span> <span style="color: #000000;">1</span> myaudio.mp3</pre></td></tr></table></div>

<p>Note that once you have reduced the sampling frequency some of the audio data is lost. You cannot again resample it to a higher value and expect increase in the audio quality.</p>
<h4>2.1.6  Audio grabbing</h4>
<p>Until now we have looked into how to transform existing audio stream into other formats. FFmpeg can also grab audio from external devices such as a microphone. This can be useful if you need to record from your desktop microphone or create a screencast. Note that the following command will not work on a Windows machine. You need to have a Linux machine to correctly grab the mic audio. Enter the following command at your Linux prompt.</p>

<div class="wp_codebox"><table><tr id="p299330"><td class="code" id="p2993code30"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-f</span> oss  <span style="color: #660033;">-i</span>  <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>dsp  .<span style="color: #000000; font-weight: bold;">/</span>audio.wav</pre></td></tr></table></div>

<p>This will start recording the input audio from the mic to the ‘audio.wav’ file. Once started you will need to press ‘q’ to stop the recording. We will now look into the various options given above.<br />
The option –f denotes the format to be used for the input stream. There are various formats FFmpeg supports; you can find the complete list by issuing the following command.</p>

<div class="wp_codebox"><table><tr id="p299331"><td class="code" id="p2993code31"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-formats</span></pre></td></tr></table></div>

<p>Here we are using the &#8216;oss&#8217; format, which stands for Open Sound System input device. The Open Sound System (OSS) is an interface for making and capturing sound in Unix or Unix-like operating systems. In the Linux kernel, there have historically been two uniform sound APIs. One is OSS; the other is ALSA (Advanced Linux Sound Architecture). ALSA is available for Linux only. </p>
<p>The device ‘/dev/dsp’ is the default audio input device in the Linux system. It&#8217;s connected to the main speakers and the primary recording source such as a microphone. The system administrator can set /dev/dsp to be a symbolic link to the desired default device. </p>
<p>The ‘audio.wav’ file is where the recorded audio will be saved.<br />
Another example &#8211; the following will record the mic audio to the file ‘rec.flac’ in the current directory, this is a flac format file.</p>

<div class="wp_codebox"><table><tr id="p299332"><td class="code" id="p2993code32"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-f</span> alsa  <span style="color: #660033;">-ar</span> <span style="color: #000000;">48000</span>  <span style="color: #660033;">-i</span> front  .<span style="color: #000000; font-weight: bold;">/</span>rec.flac</pre></td></tr></table></div>

<h4>2.2 Some popular audio formats</h4>
<p><strong>.AAC</strong> Advanced Audio Coding File &#8211; declared the new audio-file standard in 1997, designed to replace its predecessor, MP3. It provides better quality at lower bit rates, and its Apple’s standard iTunes and iPod audio format.</p>
<p><strong>.AIF(F)</strong> Audio Interchange File Format &#8211; developed by Electronic Arts and Apple back in the ’80s. AIFF files contain uncompressed audio, resulting in large file sizes.</p>
<p><strong>.m4a</strong> Apple Lossless &#8211; This file format uses lossless compressions for digital music.</p>
<p><strong>.MP3 MPEG Layer 3</strong> &#8211; the most popular digital-audio music format, designed by a team of European engineers in 1991 to conserve the quality of a song while storing it in a small, compact file.</p>
<p><strong>.OGG Ogg Vorbis</strong> &#8211; one of the most popular license-free, open-source audio-compression formats. It’s efficient for streaming and compression because it creates smaller files than MP3 while maintaining audio quality. </p>
<p><strong>.RA(M) Real Audio Media</strong> &#8211; developed by RealNetworks in 1995. It has a wide variety of uses, from videos to music, but is mainly used for streaming audio such as that from Internet radio stations.</p>
<p><strong>.WAV Windows WAVE</strong> &#8211; IBM and Microsoft-developed format popular audio format among PC computer users; it can hold both compressed and uncompressed audio.</p>
<p><strong>.WMA Windows Media Audio </strong>- designed by Microsoft to be an MP3 competitor, but with the introduction of iTunes and iPods, it’s fallen far behind MP3 in popularity.</p>
<h4>2.3  Audio processing recipes</h4>
<p>MP3 to AAC High Quality Stereo</p>

<div class="wp_codebox"><table><tr id="p299333"><td class="code" id="p2993code33"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> in.mp3  <span style="color: #660033;">-acodec</span> aac <span style="color: #660033;">-ac</span> <span style="color: #000000;">2</span> <span style="color: #660033;">-ar</span> <span style="color: #000000;">48000</span> <span style="color: #660033;">-ab</span> 192k out.aac</pre></td></tr></table></div>

<p>MP3 to AAC High Quality 5.1</p>

<div class="wp_codebox"><table><tr id="p299334"><td class="code" id="p2993code34"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> in.mp3  <span style="color: #660033;">-acodec</span> aac <span style="color: #660033;">-ac</span> <span style="color: #000000;">6</span> <span style="color: #660033;">-ar</span> <span style="color: #000000;">48000</span> <span style="color: #660033;">-ab</span> 448k out.aac</pre></td></tr></table></div>

<p>Convert to low quality mp3 to preserve storage</p>

<div class="wp_codebox"><table><tr id="p299335"><td class="code" id="p2993code35"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span> in.mp3 <span style="color: #660033;">-ab</span> 64K out.mp3</pre></td></tr></table></div>

<p>MP3 to Vorbis OGG (can be played in HTML 5)</p>

<div class="wp_codebox"><table><tr id="p299336"><td class="code" id="p2993code36"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-i</span> in.mp3 <span style="color: #660033;">-acodec</span> vorbis <span style="color: #660033;">-aq</span> <span style="color: #000000;">50</span> out.ogg</pre></td></tr></table></div>

<p>In the next post in the series we will look into various video processing tasks.</p>
<p><a href="http://feedburner.google.com/fb/a/mailverify?uri=Code-diesel&amp;loc=en_US" rel="nofollow" >Subscribe now to get the next post update.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tutorial/ffmpeg-a-beginners-guide-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FFmpeg: A beginners guide &#8211; part 1</title>
		<link>http://www.codediesel.com/tutorial/ffmpeg-a-beginners-guide-part-1/</link>
		<comments>http://www.codediesel.com/tutorial/ffmpeg-a-beginners-guide-part-1/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 04:44:55 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[ffmpeg]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2988</guid>
		<description><![CDATA[The idea for this posts arose from my frustration on not finding any organized documentation for learning FFmpeg. Thus, my aim in writing this series has been to provide newbie learners to quickly get up-and-running with FFmpeg. 1.1.1 What is FFmpeg? FFmpeg is a command-line tool for *nix and Windows systems that, in its simplest [...]]]></description>
			<content:encoded><![CDATA[<p>The idea for this posts arose from my frustration on not finding any organized documentation for learning FFmpeg. Thus, my aim in writing this series has been to provide newbie learners to quickly get up-and-running with FFmpeg.<br />
<span id="more-2988"></span></p>
<h4>1.1.1 What is FFmpeg?</h4>
<p>FFmpeg is a command-line tool for *nix and Windows systems that, in its simplest form, provide a facility to decode and an encode media files. With the proliferation of video on the Internet and in our daily lives, users need the ability to transcode (convert) audio and video files from one format to another. For example, a user might have downloaded a video from YouTube and need to convent it to a format playable on an iPod or other media device. </p>
<p>Besides this obvious use, FFmpeg is also capable of a few other fundamental manipulations on the audio and video data. These manipulations include changing the sample rate of the audio and advancing or delaying it with respect to the video, reducing the size of the media file. They also include changing the frame rate of the resulting video, cropping it, resizing it, placing bars left and right and/or top and bottom in order to pad it when necessary, or changing the aspect ratio of the picture. Furthermore, ffmpeg allows importing audio and video from different sources such as a microphone.</p>
<p>The main components of FFmpeg are libavcodec, an audio/video codec library, libavformat, an audio/video container mux/demux library, and the ffmpeg command line program for passing various transcoding options to the main program.</p>
<p>The FFmpeg project was started by Fabrice Bellard, and has been maintained by Michael Niedermayer since 2004. The name of the project comes from the MPEG video standards group, together with &#8220;FF&#8221; for &#8220;fast forward&#8221;. On March 13, 2011 a group of FFmpeg developers decided to fork the project under the name Libav (<a href="http://libav.org/" rel="nofollow"  title="libav" target="_blank">http://libav.org/</a>) due to some project management related issues.</p>
<p>FFmpeg is used by many open source and proprietary projects, including ffmpeg2theora, VLC, MPlayer, HandBrake, Blender, Google Chrome, and various others.</p>
<h4>1.1.2 Components of FFmpeg</h4>
<p>FFmpeg is made of the following main components.</p>
<p><strong>Programs</strong><br />
ffmpeg &#8211; a command line tool to convert multimedia files between formats.<br />
ffserver &#8211; a multimedia streaming server for live broadcasts.<br />
ffplay &#8211; a simple media player based on SDL and the FFmpeg libraries.<br />
ffprobe &#8211; a simple multimedia stream analyzer.</p>
<p><strong>Libraries</strong><br />
libavutil &#8211; a library containing functions for simplifying programming, including random number generators, data structures, mathematics routines, core multimedia utilities, and much more.<br />
libavcodec &#8211; a library containing decoders and encoders for audio/video codecs.<br />
libavformat &#8211; a library containing demuxers and muxers for multimedia container formats.<br />
libavdevice &#8211; a library containing input and output devices for grabbing from and rendering to many common multimedia input/output software frameworks, including Video4Linux, Video4Linux2, VfW, and ALSA.<br />
libavfilter &#8211; a library containing media filters.<br />
libswscale &#8211; a library performing highly optimized image scaling and color space/pixel format conversion operations.</p>
<p>In this posts we will primarily focus on the ffmpeg program, the other programs like ffserver are used for video broadcasts and is outside the scope of this posts. Among the libraries, the most notable parts of FFmpeg are libavcodec, an audio/video codec library, and libavformat, an audio/video container mux and demux library</p>
<h4>1.1.3 Compression</h4>
<p>To be honest, trying to shoehorn the complete details of audio and video in a paragraph or two is plainly ridiculous, as the topic is rather complex. But since this is a beginner’s guide, a few basic overviews will be enough to get you started using ffmpeg properly.</p>
<p>If you are working with audio and video, you are well aware that these files take an inordinate space for storage. You cannot easily work with these files if they were not compressed beforehand. Assuming an NTSC standard video format; a raw (uncompressed) video at 720&#215;480 pixels, 30 frames per second and 24-bit RGB color, would take about 1,036,800 bytes (1 Mb) per frame. That&#8217;s almost 30MB per second, or over 200GB for a 2-hour movie. And that&#8217;s just the video. Audio stream also takes additional storage. Something needs to be done so that the movie can be stored on a consumer-grade medium such as a DVD. The data needs to be compressed beforehand. </p>
<p>Conventional, lossless compression algorithms such as ZIP, which everyone uses on a regular basis, don&#8217;t reduce the size of the data enough, so we need to look into lossy compression for further size reduction. Lossy compression works by discarding some data in the media which results in smaller file sizes. So now you might be thinking what data the compression algorithm discards. Well in general the algorithm does not discard any random data, which would be a disaster. The compression algorithm discards data only if it thinks that the data is redundant. For example in movie frames many times not much changes between successive frames; if the compression software discards some of these frames the viewer will hardly notice any difference, but the storage requirement of those frames have been saved.</p>
<p>Lossy compression is commonly used to compress multimedia data such as audio, video and still images. The only negative aspect of lossy compression is that as some data is removed during compression which can reduce the fidelity of the output.<br />
The algorithms that allow us to encode and decode the data, whether by using lossy or lossless technique are called codecs. Several codecs are enclosed in the libavcodec library supplied with ffmpeg, which enables you to work with a wide variety of video and audio formats.</p>
<p>Once the audio and video streams have been encoded by their respective codecs, this encoded data needs to be put together into a single file. This file is called the ‘container’.  A graphic of the process is shown below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2012/02/image006.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2012/02/image006.jpg" alt="" title="image006" width="479" height="186" class="aligncenter size-full wp-image-2991" /></a></p>
<h4>1.1.4 Bitrates</h4>
<p>A movie is made-up of two main components, Audio and Video. Both &#8220;components&#8221; produce a separate stream of data that must be decoded by your DVD-player or some program so we can see and hear the video properly. </p>
<p>The bitrate of a movie is the key to the quality of the audio and video of that movie. Also, particular formats specify the bitrate or the maximum bitrate to be used. Bitrate is a measurement of the number of bits that are transmitted over a set length of time. Your overall bitrate is a combination of your video stream and audio stream in your file with the majority coming from your video stream. Bitrate denotes the average number of bits that one second of audio or video data will take up in your compressed bit stream. The overall bitrate of your movie is a combination of your video stream and audio stream in your file with the majority coming from your video stream.</p>
<p>A bit rate is usually measured in some multiple of bits per second &#8211; for example, kilobits, or thousands of bits per second (Kbps &#8211; for example, kilobits, or thousands of bits per second (Kbps). </p>
<p>Bitrates come in two versions &#8211; VBR (Variable Bit Rate encoding) or CBR (Constant Bit Rate encoding).  VBR allows a higher bitrate (and therefore more storage space) to be allocated to the more complex segments of media files while less space is allocated to less complex segments. The average of these rates can be calculated to produce an average bitrate for the file. VBR allows you to set a maximum and minimum bitrate. The compression algorithm then tries to efficiently compress the data reducing to the minimum bitrate when there is little or no motion on screen and increasing to the maximum defined rate when the motion is prevalent. This helps to give you a smaller overall file size without compromising the quality of the video.</p>
<p>CBR is used when a predictable flat bit rate is needed. Although the flat bitrate throughout the entire file comes at the price of efficiency for the codec; usually resulting in a larger file, but smoother playback. CBR is useful for streaming multimedia content on limited capacity channels since it is the maximum bit rate that matters, not the average, so CBR would be used to take advantage of all of the capacity. CBR would not be the optimal choice for storage as it would not allocate enough data for complex sections (resulting in degraded quality) while wasting data on simple sections.</p>
<p>Depending on your video you might want to use a VBR for a streaming playback if the sudden spikes do not exceed your target user&#8217;s connection speed. For example if there is only one high motion scene in a video, you will be wasting considerable bandwidth on a CBR throughout the entire file and may better serve your user&#8217;s need by using a VBR. Either way try experimenting with the two settings to find what works best for your video.</p>
<p>Briefly, a bitrate specifies how many kilobits the file may use per second of audio. The following shows the quality for various standard audio bitrates.</p>
<table class="posts_table">
<colgroup>
<col width="46%" />
<col width="48%" />
</colgroup>
<tbody valign="top">
<tr>
<td>64 Kbps</td>
<td>Audio encoded at 64 Kbps have a 15:1 compression ratio. This bitrate is not recommended for digital music but is acceptable for voice-only recordings.</td>
<tr>
<td>96 Kbps</td>
<td>Audio encoded at 96 Kbps have a 15:1 compression ratio. One minute of music will be about 700KB of disk space.</td>
</tr>
<td>128 Kbps</td>
<td>Audio encoded at 128 Kbps have an 11:1 compression ratio. One minute of music is takes around 1MB of disk space.</td>
</tr>
<td>160 Kbps</td>
<td>Audio encoded at 160 Kbps have a 9:1 compression ratio. One minute of music will is about 1.5MB of disk space.</td>
</tr>
<td>192 Kbps and above</td>
<td>MP3s encoded at this setting take up the most space but have CD quality sound and can take up to 2MB of space per 60 seconds of music. Online music stores or music download services will have at least this high of a bitrate.</td>
</tr>
</tbody>
</table>
<h4>1.1.5 Audio Sampling Frequency</h4>
<p>The audio sampling frequency is the number of times per second audio is sampled and stored &#8211; CD audio is sampled at 44.1 KHz, which means when the sound is converted from analog to digital, 44100 samples per second are taken of the audio signal. The higher the sampling rate the audio has, the wider the frequency range it provides. In other words, higher is better quality. Your lows will be lower; your highs will be higher.  For example the following image shows an analog signal on the left converted to a digital representation using two different sampling rates. As you can see the higher sampling will lead to an even more exact reproduction of the original signal.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2012/02/bitrate.gif"><img src="http://www.codediesel.com/wp-content/uploads/2012/02/bitrate.gif" alt="" title="bitrate" width="502" height="117" class="aligncenter size-full wp-image-2990" /></a></p>
<p>The sample rate can be thought of as how often or how much the sound is described. CD quality audio has 44,100 of these measurements a second. That’s why it’s called 44.1 kilohertz (khz).</p>
<p>So what is the relationship between bitrate and sampling frequency? Bitrate simply specifies the number of bits per second that are used to encode the audio stream. The uncompressed bitrate for CD audio is 16 bits x 44100 samples x 2 channels = 1411200bps, or approximately 1411kbps. When audio is stored in an uncompressed format, the bitrate is a linear function of the sample rate; i.e. doubling the sample rate doubles the bitrate.</p>
<p>With uncompressed audio, there is a direct relationship between the sample rate and the bitrate. A 44.1kHz 16-bit stereo signal takes 1411.2 kbps, or approximately 10.4Mb per minute to record.  A 44.1kHz 16-bit mono file would take half of this, as would a 44.1kHz 8-bit stereo file or a 22.05kHz 16-bit stereo file.</p>
<p>But now formats like Ogg Vorbis and MP3, compress audio by making calculated guesses about the sounds humans aren&#8217;t likely to hear and then discard these sound samples.  As part of this process, such formats allow us to make some of the decisions by deciding how much to throw away, or to put it more simply, how much data to use to represent the original sound.  So, using our 44.1kHz stereo sample, we can choose to use as little as 48kbps or as much as approx 500kbps to store this sound.  At 500kbps, more of the original sound fidelity is preserved than at 48kbps.</p>
<p><strong>Calculating values</strong></p>
<blockquote><p>
An audio file&#8217;s bit rate can be easily calculated when given sufficient information.<br />
Bit rate = (sampling rate) x (bit depth) x (number of channels)</p>
<p>e.g., a recording with a 44.1 kHz sampling rate, a 16 bit depth, and 2 channels:<br />
44100  x 16 x 2 = 1411200 bits per second, or 1411.2 kbit/s</p>
<p>The file size of an audio recording can also be calculated using a similar formula:<br />
File Size (Bytes) = (sampling rate) x (bit depth) x (total channels) x (seconds) / 8</p>
<p>e.g. a 70 minutes long CD quality recording will take up 740MB:<br />
44100 x 16 x 2 x 4200 / 8 = 740880000 Bytes
</p></blockquote>
<p>Some standard sampling frequencies with their applications is given below.</p>
<table class="posts_table">
<colgroup>
<col width="46%" />
<col width="48%" />
</colgroup>
<thead valign="bottom">
<tr>
<th class="head"><strong>Sampling Rate</strong></th>
<th class="head"><strong>Use</strong></th>
</tr>
</thead>
<tbody valign="top">
<tr>
<td>8,000 Hz</td>
<td>Telephone, walkie-talkie, wireless intercom and wireless microphone transmission; adequate for human speech.</td>
<tr>
<td>11,025 Hz</td>
<td>used for lower-quality PCM, MPEG</td>
</tr>
<tr>
<td>22,050 Hz</td>
<td>One half the sampling rate of audio CDs; used for lower-quality PCM and MPEG</td>
</tr>
<tr>
<td>32,000 Hz</td>
<td>miniDV digital video camcorder, video tapes with extra channels of, DAT, High-quality digital wireless microphones, digitizing FM radio.</td>
</tr>
<tr>
<td>44,100 Hz</td>
<td>Audio CD, also most commonly used with MPEG-1 audio (VCD, SVCD, MP3). Most professional audio equipment uses 44.1 kHz sampling and above.</td>
</tr>
<tr>
<td>48,000 Hz</td>
<td>he standard audio sampling rate used by professional digital video equipment such as tape recorders, video servers, vision mixers and so on. Also used for sound with consumer video formats like DV, digital TV, DVD, and films.</td>
</tr>
<tr>
<td>96,000 Hz</td>
<td>DVD-Audio, some LPCM DVD tracks, Blu-ray Disc audio tracks, HD DVD High-Definition DVD) audio tracks.</td>
</tr>
</tbody>
</table>
<h4>1.1.6 Frame rate</h4>
<p>The frame rate is how many unique consecutive images are displayed per second in the video to give the illusion of movement; each image thus is called a ‘frame’. The human brain perceives a smooth continuous motion if shown around 24 frames per second. If the frames are less than this magic number, you will see a jerky motion rather than a smooth one. Most video creators use this frame rate.</p>
<p>This is not a standard of course, if your video is a screen cast you can get to frame rates as low as 5fps. Television standards such as PAL (common in Europe and some parts of Asia) uses 25fps, while NTSC standard (used in the US and Japan) uses 29.97fps. Generally you should never exceed the frame rate of the source video. Obviously, the best results will be achieved if the frame rate is kept the same as your original source.</p>
<h4>1.1.7 Containers</h4>
<p>A container file is used to identify and combine different data types. Simpler container formats can contain different types of audio formats, while more advanced container formats can support multiple audio and video streams, subtitles and meta-data — along with the synchronization information needed to play back the various streams together. In most cases, the file header and most of the metadata are specified by the container format. For example, container formats exist for optimized, low-quality, internet video streaming which differs from high-quality DVD streaming requirements.</p>
<p>The video file formats we’re familiar with, such as Quicktime movies (.mov), .avi are media container formats. Some container formats just contain audio, like WAV file fro Windows, MP3 music files or AIFF files for Macs. Others contain audio and video, such as ASF files for Windows, which contain audio compressed with the WAV codec and video compressed with the WMV codec. There are dozens of these container formats. If you’re uploading a video to an online site, check to see what formats the site supports. Sometimes this can be confusing because the list of accepted formats may have both compression formats like MPEG-4 and container formats like .mov listed. </p>
<h4>1.2	Installing FFmpeg</h4>
<p>FFmpeg is developed under GNU/Linux, but it can be compiled under most operating systems, including Mac OS X, Microsoft Windows, AmigaOS. In most of the Linux distros, you can directly install ffmpeg using their respective package managers. But in case you are looking for installing the latest version or want to customize the installation, you might need direct installation from the source code too, but as it is an involved and tricky procedure, I’m not discussing it here.<br />
<strong><br />
Installing FFmpeg on Ubuntu</strong><br />
Run the following command in the terminal to install FFmpeg.</p>

<div class="wp_codebox"><table><tr id="p298842"><td class="code" id="p2988code42"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #c20cb9; font-weight: bold;">ffmpeg</span></pre></td></tr></table></div>

<p><strong>Installing FFmpeg on Fedora</strong><br />
FFmpeg can be directly installed from the repos using the following command.</p>

<div class="wp_codebox"><table><tr id="p298843"><td class="code" id="p2988code43"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">su</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'yum install ffmpeg'</span></pre></td></tr></table></div>

<p><strong>Installing FFmpeg on CentOS</strong><br />
FFmpeg can be directly installed from the repos using the following command.</p>

<div class="wp_codebox"><table><tr id="p298844"><td class="code" id="p2988code44"><pre class="bash" style="font-family:monospace;">$ yum <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> ffmpeg-devel</pre></td></tr></table></div>

<p><strong>Installing FFmpeg on Windows</strong><br />
By far the easiest way to start using FFmpeg is to get a precompiled binary. Zeranoe.com has pre-built binaries for windows, which makes it easier to install ffmpeg. So if you are using Windows you can get up and running FFmpeg in no time. Go ahead and grab the binaries from the below link.<br />
<a href="http://ffmpeg.zeranoe.com/builds/" rel="nofollow"  target="_blank">http://ffmpeg.zeranoe.com/builds/</a></p>
<p>Once installed use the following command to get the ffmpeg version and the versions of the codecs installed.</p>

<div class="wp_codebox"><table><tr id="p298845"><td class="code" id="p2988code45"><pre class="bash" style="font-family:monospace;">C:\<span style="color: #c20cb9; font-weight: bold;">ffmpeg</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span>  <span style="color: #660033;">-version</span></pre></td></tr></table></div>

<p>On my Windows machine it returns the following; of course this may be different on your system, depending on the version of FFmpeg installed:</p>
<blockquote><p>
ffmpeg version N-31100-g9251942, Copyright (c) 2000-2011 the FFmpeg developers<br />
 built on Jun 30 2011 21:17:59 with gcc 4.5.3<br />
  libavutil    51. 11. 0 / 51. 11. 0<br />
  libavcodec   53.  7. 0 / 53.  7. 0<br />
  libavformat  53.  4. 0 / 53.  4. 0<br />
  libavdevice  53.  2. 0 / 53.  2. 0<br />
  libavfilter   2. 24. 0 /  2. 24. 0<br />
  libswscale    2.  0. 0 /  2.  0. 0<br />
  libpostproc  51.  2. 0 / 51.  2. 0<br />
ffmpeg N-31100-g9251942<br />
libavutil    51. 11. 0 / 51. 11. 0<br />
libavcodec   53.  7. 0 / 53.  7. 0<br />
libavformat  53.  4. 0 / 53.  4. 0<br />
libavdevice  53.  2. 0 / 53.  2. 0<br />
libavfilter   2. 24. 0 /  2. 24. 0<br />
libswscale    2.  0. 0 /  2.  0. 0<br />
libpostproc  51.  2. 0 / 51.  2. 0
</p></blockquote>
<h4>1.2.2 FFmpeg Command syntax</h4>
<p>Adhering to the UNIX culture, FFmpeg relies on a plethora of command-line options to do its work. The generic syntax of an FFmpeg command is shown below.</p>

<div class="wp_codebox"><table><tr id="p298846"><td class="code" id="p2988code46"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>infile options<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'-i'</span> infile<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>...<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>outfile options<span style="color: #7a0874; font-weight: bold;">&#93;</span> outfile<span style="color: #7a0874; font-weight: bold;">&#125;</span>...</pre></td></tr></table></div>

<p>Each section of the command is explained below.</p>
<p><em>ffmpeg</em> &#8211; The first is the FFmpeg executable file name.</p>
<p><em>infile option</em> &#8211; This is where you put options for your input video or audio file.  This tells FFmpeg to apply any options give here to the input file before processing starts. This section is not as widely used as the ‘outfile options’.</p>
<p><em>-i infile</em> &#8211; This is the actual video or audio file you use for processing, and also the directory of where it is located.<br />
e.g  /home/george/media/myvideo.flv. You will always need to include the `-i` option before your file name.</p>
<p><em>outfile options</em> &#8211; This is where you will put the various options that are required which you want to be applied to the video or audio you will be creating. </p>
<p><em>outfile</em> – The name of the output file you want to create, and also the directory path if it not the same as your input file directory.<br />
e.g is /home/george/media/out.flv </p>
<p>Now that we have FFmpeg installed, in the next post we will learn about audio processing.</p>
<p><a href="http://feedburner.google.com/fb/a/mailverify?uri=Code-diesel&amp;loc=en_US" rel="nofollow" >Subscribe now to get the next post update.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tutorial/ffmpeg-a-beginners-guide-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Selenium IDE Tutorial &#8211; Part 2</title>
		<link>http://www.codediesel.com/php/selenium-ide-tutorial-part-2/</link>
		<comments>http://www.codediesel.com/php/selenium-ide-tutorial-part-2/#comments</comments>
		<pubDate>Sat, 22 Nov 2008 05:49:01 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[selenium]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=114</guid>
		<description><![CDATA[In part 1 of this tutorial you have seen how to write a simple web test using Selenium. In this part we will see how to use the test generated using the IDE with Selenium RC and PHPUnit. In this part we will use Delicious as our test target. I&#8217;ve already created a small Selenium [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-3476693800710463";
/* 468x60, created 6/14/10 */
google_ad_slot = "3005910052";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>In <a href="http://www.codediesel.com/php/selenium-ide-tutorial-part-1/"title="selenium tutorial part 1"  target="_self">part 1</a> of this tutorial you have seen how to write a simple web test using Selenium. In this part we will see how to use the test generated using the IDE with Selenium RC and PHPUnit.<br />
<span id="more-114"></span><br />
In this part we will use Delicious as our test target. I&#8217;ve already created a small Selenium test that you can use. You can download the HTML source from <a href="http://www.codediesel.com/data/scripts/delicious.html.txt">here</a>.</p>
<p><strong>Starting up.</strong><br />
Restart the Selenium IDE. In the source tab replace the default HTML with the one from the above file. Set the Base URL to http://delicious.com/. After doing this your IDE should like below.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium221.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium221.gif" alt="selenium ide tutorial 2" title="selenium221" width="408" height="556" class="aligncenter size-full wp-image-132" /></a></p>
<p>If you are on a slow internet connection than it may help to slow the test speed.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium241.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium241.gif"  style="border: 1px solid #c0c0c0;" alt="" title="selenium241" width="295" height="114" class="aligncenter size-full wp-image-135" /></a></p>
<p>So what exactly does this test do? Simple: </p>
<p>1. First it opens the delicious home page<br />
2. Then it searches by the keyword &#8216;selenium&#8217;<br />
3. Reads the total number of bookmarks from the results page as shown below</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium23.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium23.gif" style="border: 1px solid #c0c0c0;" alt="" title="selenium tutorial" width="500" height="255" class="aligncenter size-full wp-image-133" /></a></p>
<p>Now this may not look like a test for you , but it is just an exercise in using Selenium, you will surely apply it to a good test case. Onwards&#8230;</p>
<p>Before running this test make sure that you are logged out of Delicious.<br />
Now run the selenium IDE test. After the test is successfully completed this is what you should see in the log pane.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium25.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium25.gif" alt="" title="selenium25" width="468" height="186" class="aligncenter size-full wp-image-136" /></a></p>
<p><strong>Is that all?</strong><br />
If this was all there was I wouldn&#8217;t be really writing this post. The interesting part comes now &#8211; running the same test from PHP. From the file menu select &#8216;Export Test Case As&#8230;&#8217; and as we are using PHP, select &#8216;PHP &#8211; Selenium RC&#8217;. Save the file by the name &#8216;Example.php&#8217;.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium26.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium26.gif"  style="border: 1px solid #c0c0c0;" alt="" title="selenium26" width="469" height="363" class="aligncenter size-full wp-image-137" /></a></p>
<p>This is what we will get in the &#8216;Example.php&#8217; file.</p>

<div class="wp_codebox"><table><tr id="p11450"><td class="code" id="p114code50"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Extensions/SeleniumTestCase.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Example <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Extensions_SeleniumTestCase
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setBrowser</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;*chrome&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setBrowserUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://delicious.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> testMyTestCase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">open</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;homepage-searchinput&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;selenium&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">click</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;homepage-searchsubmit&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">waitForPageToLoad</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;30000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xpath=/html/body[@id='index']/div
                         [@id='doc3']/div[@id='bd']/div
                         [@id='yui-main']/div[@id='content']/h3/div/em&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>A short notice.</strong><br />
Before we proceed a few points to consider. Make sure that the file name and the class name are the same, here &#8216;Example&#8217;. Say if you change the class name to &#8216;Delicious&#8217; make sure that you also change the file name to &#8216;Delicious.php&#8217;.</p>
<p>What the code basically does is it initializes the browser to &#8216;chrome&#8217; (you can set the browser to your preference by changing to &#8216;*iexplore&#8217; or &#8216;*firefox&#8217;);  sets the baseurl to &#8216;http://delicious.com/&#8217; and runs the test.</p>
<p><strong>Whats the complex looking line!?</strong><br />
Everything may look fine to you except maybe the last line &#8211; &#8216;$this->getText&#8230;&#8217;.  The function &#8216;getText&#8217; returns the text found at the element specified in the expression. The complex looking line is an xpath expression to the results element:</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium27.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium27.gif" style="border: 1px solid #c0c0c0;" alt="" title="selenium27" width="248" height="91" class="aligncenter size-full wp-image-138" /></a></p>
<p>You don&#8217;t have to rake your brains to find an xpath to an element. An easy way is to use the <a href="https://addons.mozilla.org/en-US/firefox/addon/1192" rel="nofollow" target="_blank" >XPather </a>Firefox addon to get the xpath of any element on a page. After you download and install the addon, restart Firefox, right-click on any element and from the context menu select &#8216;Show in XPather&#8217;. XPather retrieves the xpath to the specified element, which you can than use in your test code.</p>
<p>Note: This site uses &#8216;delicious&#8217; as an example, so even some minor changes made by them to their site will render the xpath invalid, throwing an &#8216;element not found&#8217; error in selenium. If this happens please notify me or you can find the new xpath using the Xpather plugin.</p>
<p><strong>Downloading and installing Selenium RC</strong><br />
Selenium RC is a Java based command line server that starts browsers and runs commands you pass from your tests.</p>
<p>1. First make sure you have a Java runtime installed on your machine.<br />
2. Download Selenium RC from <a href="http://selenium-rc.seleniumhq.org/download.html" rel="nofollow" target="_blank" >here</a>.<br />
3. After extracting the files from the archive copy the &#8216;selenium-server.jar&#8217; file to any directory you feel appropriate. I copied it to my PHP installations bin directory.<br />
4. Start the Selenium RC server from the command-line by issuing the following command:</p>
<p><em>java -jar selenium-server.jar</em></p>
<p>This will start the server on port 4444.<br />
5. Now the server is ready to accept test commands from your PHP script. Make sure you keep this server running till you finish testing.</p>
<p><strong>Installing PHPUnit</strong><br />
1. An easy way to install PHPUnit is to use the PEAR installer. The PEAR channel (pear.phpunit.de) is used to distribute PHPUnit so make sure that it is registered with your local PEAR environment: </p>
<p><em>pear channel-discover pear.phpunit.de</em></p>
<p>After the channel is registered install PHPUnit:</p>
<p><em>pear install phpunit/PHPUnit</em></p>
<p><strong>Actual testing</strong><br />
Now that PHPUnit is installed and the Selenium RC server is up and running, its time to run our test we saved before in our &#8216;Example.php&#8217; file. Type the following on your command-line:</p>
<p><em>phpunit Example</em></p>
<p>This will start the test. The PHPUnit Selenium driver will execute each test command from your file and send it to the Selenium server, which does the job of launching the appropriate browser, opening web pages, and performing various specified actions; and closing the browser after the test completes.</p>
<p>Now the above test does basically nothing important. So we will add some simple conditional statements to the code.</p>

<div class="wp_codebox"><table><tr id="p11451"><td class="code" id="p114code51"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
    <span style="color: #000088;">$bookmarks</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xpath=/html/body[@id='index']/div
                       [@id='doc3']/div[@id='bd']/div
                       [@id='yui-main']/div[@id='content']/h3/div/em&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bookmarks</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3000</span><span style="color: #009900;">&#41;</span>? <span style="color: #0000ff;">&quot;On the top&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Still not there&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This is what PHPUnit has to say about the above test.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium28.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium28.gif" style="border: 1px solid #c0c0c0;"  alt="" title="selenium28" width="477" height="244" class="aligncenter size-full wp-image-139" /></a></p>
<p>Or we can use an assertion as below, deliberately rigged to fail the test:</p>

<div class="wp_codebox"><table><tr id="p11452"><td class="code" id="p114code52"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
    <span style="color: #000088;">$bookmarks</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xpath=/html/body[@id='index']/div
                       [@id='doc3']/div[@id='bd']/div
                       [@id='yui-main']/div[@id='content']/h3/div/em&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertGreaterThan</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">33000</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookmarks</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now PHPUnit will shout failure:</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium29.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium29.gif" alt="" title="selenium29" width="500" height="269" class="aligncenter size-full wp-image-140" /></a></p>
<p>You can look at more functions to implement in your tests <a href="http://www.phpunit.de/wiki/Documentation" rel="nofollow" target="_blank" >here</a> and <a href="http://selenium-core.seleniumhq.org/reference.html" rel="nofollow" target="_blank" >here</a>. </p>
<p>This concludes the second part of the tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/selenium-ide-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>61</slash:comments>
		</item>
		<item>
		<title>Simple Pagination in PHP tutorial</title>
		<link>http://www.codediesel.com/php/simple-pagination-in-php/</link>
		<comments>http://www.codediesel.com/php/simple-pagination-in-php/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 05:03:48 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=118</guid>
		<description><![CDATA[PHP pagination tutorial using the pears pager library]]></description>
			<content:encoded><![CDATA[<p>Pagination is a frequent requirement in web development projects. Most PHP developers must have already implementated paging in one form or other in their projects. In this post we will see how to add pagination the easy way using PEAR&#8217;s Pager class. Note that in all the posts I use PHP 5.x.x, so if you are still stuck at version 4.x.x, its already time to upgrade.<br />
<span id="more-118"></span></p>
<p>Its always nice to see some working code first, before getting into the details. So here goes.</p>

<div class="wp_codebox"><table><tr id="p11853"><td class="code" id="p118code53"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Pager/Pager.php'</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* We will bypass the database connection code ... */</span>
<span style="color: #000088;">$sqlQuery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SOME SQL QUERY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlQuery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$totalRows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalRows</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pager</span> <span style="color: #339933;">=</span> Pager<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above code will return the following pagination links.</p>
<p>1   |   2   |   3   |   4   |   5   |   6   |   7   |   8   |   9      »      [10]</p>
<p>Assuming the page where the above code is included is named &#8216;dataPage.php&#8217;; the pager links will have a url like follows:</p>
<p>dataPage.php?pageID=<em>n</em></p>
<p>where <em>n</em> is the page number.</p>
<p>You can see how easy it is using the Pager class. All the link building is handled by the class. Now lets go into the details.</p>
<h4>1. Installation</h4>
<p>If you are installing using the Pear installer, enter the following at the command prompt to install Pager.</p>

<div class="wp_codebox"><table><tr id="p11854"><td class="code" id="p118code54"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\pear install Pager</pre></td></tr></table></div>

<p>Or else you can just download the module from <a href="http://pear.php.net/package/Pager/download" rel="nofollow" >here</a> and then copy it to your Pear includes directory, which will probably be &#8216;c:\<em>your php installation directory</em>\Pear&#8217;.</p>
<h4>2. A simple example</h4>
<p>In the following example we will create a pagination for a &#8216;logs&#8217; table.</p>

<div class="wp_codebox"><table><tr id="p11855"><td class="code" id="p118code55"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* Include the Pear::Pager file */</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pager/Pager.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Replace this with your database details */</span>
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> username<span style="color: #339933;">,</span> password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span>database name<span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* First we need to get the total rows in the table */</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT count(*) AS total FROM logs&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Total number of rows in the logs table */</span>
<span style="color: #000088;">$totalItems</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set some options for the Pager */</span>
<span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// Sliding or Jumping mode. See below.</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// Total rows to show per page</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// See below</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the Pager class with the above options */</span>
<span style="color: #000088;">$pager</span> <span style="color: #339933;">=</span> Pager<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the links */</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The following code will retreive the result using the pager options */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The function below will get the page offsets to be used with
the database query. For e.g if we are on the third page then the
$from variable will have the value of '21' (we are showing 10 items per 
page, remember) and the $to variable will have the value of '30'.
*/</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #339933;">,</span> <span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOffsetByPageId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* The MySQL 'LIMIT' clause index starts from '0', 
    so decrease the $from by 1 */</span>
<span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$from</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The number of rows to get per query */</span>
<span style="color: #000088;">$perPage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'perPage'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM alogs LIMIT <span style="color: #006699; font-weight: bold;">$from</span> , <span style="color: #006699; font-weight: bold;">$perPage</span>&quot;</span><span style="color: #339933;">,</span>
<span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009933; font-style: italic;">/* Do something with the query results */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the Pager class there are two modes to display the pagination: Sliding &amp; Jumping.</p>
<p>With Pager in &#8220;Sliding&#8221; mode the pagination links change smoothly, and the current page is always shown at the center of the &#8220;window&#8221; (except for the start and end pages). When in Sliding mode the delta option implies how many links to show on the left and right of the current page link.<br />
For e.g with a delta set to &#8217;3&#8242; the links are displayed as shown below, page 10 being the current page:</p>
<p>[1]      «      7   |   8   |   9   |   <strong>10</strong> |   11   |   12   |   13      »      [13]</p>
<p>With delta option set to &#8217;4&#8242; the links are shown as below. As you can see there are four links to the left and right of the current page.</p>
<p>[1]      «      5   |   6   |   7   |   8   |   <strong>9</strong> |   10   |   11   |   12   |   13      »      [13]</p>
<p>With the Pager in &#8220;Jumping&#8221; mode and delta set to &#8217;4&#8242; the Pager always shows the same 4 page links while you are on one of these pages.<br />
For e.g with a delta set to &#8217;4&#8242; the links are displayed as shown below, page 5 being the current page:</p>
<p>[1] &lt;&lt; Back <strong>5</strong> 6 7 8  Next &gt;&gt; [13]</p>
<p>If you are on page &#8217;9&#8242; the links displayed are as below:</p>
<p>[1] &lt;&lt; Back <strong>9</strong> 10 11 12  Next &gt;&gt; [13]</p>
<h4>3. More options</h4>
<p>You can also change the default separator to any character or image you like.</p>

<div class="wp_codebox"><table><tr id="p11856"><td class="code" id="p118code56"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'separator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">','</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will output the following:<br />
[1]      «      1   ,   2   ,   3   ,   4   ,   5   ,   6   ,   7   ,   8   ,   9      »      [13]</p>
<p>If you would like to add images to the &#8216;next&#8217; and &#8216;previous&#8217; links, use the following options:</p>

<div class="wp_codebox"><table><tr id="p11857"><td class="code" id="p118code57"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'nextImg'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;next.png&quot;  /&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'prevImg'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;prev.png&quot;  /&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Which will display the links as shown below:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2008/10/pager2.gif"><img class="aligncenter size-full wp-image-121" style="border:1px solid #c0c0c0;" title="pager2" src="http://www.codediesel.com/wp-content/uploads/2008/10/pager2.gif" alt="" width="472" height="32" /></a></p>
<p>The default number of spaces before each separator is three. To change the number of spaces use the &#8216;spacesBeforeSeparator&#8217; option. For e.g:</p>

<div class="wp_codebox"><table><tr id="p11858"><td class="code" id="p118code58"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #0000ff;">'spacesBeforeSeparator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<h4>4. Styling</h4>
<p>You can style the links by putting them in a div tag and applying a css. A example css and its output is shown below.</p>
<p>Add a div tag:</p>

<div class="wp_codebox"><table><tr id="p11859"><td class="code" id="p118code59"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;pager&quot;&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Apply CSS:</p>

<div class="wp_codebox"><table><tr id="p11860"><td class="code" id="p118code60"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.pager</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.pager</span> a <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">17px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">17px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#0C74BA</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.pager</span> a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#c0c0c0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>The output:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2008/10/pager.gif"><img class="aligncenter size-full wp-image-119" style="border:1px solid #c0c0c0;" title="pager" src="http://www.codediesel.com/wp-content/uploads/2008/10/pager.gif" alt="pager" width="376" height="35" /></a></p>
<p>That&#8217;s it. There are many other options you can use with the Pager class, the details which you can find in the Pager <a href="http://pear.php.net/manual/en/package.html.pager.php" rel="nofollow"  target="_blank">documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/simple-pagination-in-php/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Selenium IDE Tutorial &#8211; Part 1</title>
		<link>http://www.codediesel.com/php/selenium-ide-tutorial-part-1/</link>
		<comments>http://www.codediesel.com/php/selenium-ide-tutorial-part-1/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 06:34:32 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[selenium]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=105</guid>
		<description><![CDATA[Selenium IDE tutorial for php programmers]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-3476693800710463";
/* 468x60, created 6/14/10 */
google_ad_slot = "3005910052";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>Selenium IDE</strong> is an integrated development environment for performing <a href="http://www.openqa.org/selenium/" rel="nofollow" >Selenium</a> tests.  Selenium tests can be written as HTML tables or coded in various languages like C#, PHP, Perl, Python and can be run directly in most modern browsers.The IDE can help you to record, edit and debug tests. Currently the IDE is only available for Firefox (as if we developers will use any other) as a addon.</p>
<p>Here is a possible scenario for using Selenium. Imagine you have created a HTML form with about twenty fields and you have to repeatedly test the form. Filling the form every time can quickly become tedious. With Selenium you can automate the whole process and run the test as required. In this part we will see how to create a simple test in Selenium. So let&#8217;s get started.<br />
<span id="more-105"></span><br />
<strong>STEP 1 &#8211; installation :</strong></p>
<p>You can download Selenium IDE from this locations.<br />
<a href="http://selenium-ide.openqa.org/download.jsp" rel="nofollow" title="selenium ide"  target="_blank">http://selenium-ide.openqa.org/download.jsp</a><br />
<a href="https://addons.mozilla.org/en-US/firefox/addon/2079" rel="nofollow" title="selenium ide"  target="_blank">https://addons.mozilla.org/en-US/firefox/addon/2079</a><br />
Once the addon is installed make sure you do not forget to restart Firefox.</p>
<p><strong>STEP 2 &#8211; running a simple test : </strong></p>
<p>a. Start Selenium IDE  in Firefox: <em>Tools-&gt;Selenium IDE</em>. You will see the following popup.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/10/selenium1.gif"><img class="aligncenter size-full wp-image-107" title="selenium1" src="http://www.codediesel.com/wp-content/uploads/2008/10/selenium1.gif" alt="selenium" /></a></p>
<p>b. Click on the red record button on the right.<br />
c. Browse to Google.com and enter &#8216;selenium&#8217; in the search box and click enter.<br />
d. Click on the first result, which is that of selenium.openqa.org.<br />
e. Stop the recording by clicking on the record button.</p>
<p>You should see something like below. If you click on the &#8216; Source&#8217; tab you can see the test html generated by selenium.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/10/selenium2.gif"><img class="aligncenter size-full wp-image-108" title="selenium2" src="http://www.codediesel.com/wp-content/uploads/2008/10/selenium2.gif" alt="selenium ide" width="403" height="521" /></a></p>
<p>The &#8216;table&#8217; tab shows the commands recorded by Selenium.</p>
<p>f. Open a new tab in Firefox and click on the Selenium IDE&#8217;s play button to run the recorded test.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/10/selenium3.gif"><img class="aligncenter size-full wp-image-109" style="border:1px solid #c0c0c0;" title="selenium3" src="http://www.codediesel.com/wp-content/uploads/2008/10/selenium3.gif" alt="selenium ide" width="388" height="109" /></a></p>
<p>The IDE should play your recorded test. After the test is complete you should have landed on the selenium page (http://selenium.openqa.org/). The IDE after the test run is shown below. In the &#8216;Log section&#8217; you can see the various events run by the test. In the table tab you can see that all the rows are green, which means that the test ran successfully.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/10/selenium4.gif"><img class="aligncenter size-full wp-image-110" title="selenium4" src="http://www.codediesel.com/wp-content/uploads/2008/10/selenium4.gif" alt="selenium test run" width="399" height="521" /></a></p>
<p>Now lets add a small assertion to the above test.</p>
<p>a. Click on the blank line below after the last &#8216;clickAndWait&#8217; command and insert &#8216;assertTextPresent&#8217; command from the drop down box as shown below. You should see something like this.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/10/selenium51.gif"><img class="aligncenter size-full wp-image-112" title="selenium51" src="http://www.codediesel.com/wp-content/uploads/2008/10/selenium51.gif" alt="" width="437" height="366" /></a></p>
<p>This test checks to see if the text &#8216;Selenium News&#8217; is present in the last rendered page. Run the test again and you should see that the test has run successfully. No try replacing the text with something that is not present on the page, take for example &#8216;elvis&#8217; and run the test again. Now the test fails and you should get the screen below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/10/selenium6.gif"><img class="aligncenter size-full wp-image-113" title="selenium6" src="http://www.codediesel.com/wp-content/uploads/2008/10/selenium6.gif" alt="" width="436" height="563" /></a></p>
<p>You have just completed your first test run. And it was so simple.</p>
<p>&#8216;assertTextPresent&#8217; is one of the hundreds of commands available for your testing needs. Just browse throught the command drop down to get a feel of what you can you get.</p>
<p>In the next part we will see more advanced features of Selenium.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/selenium-ide-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>62</slash:comments>
		</item>
	</channel>
</rss>

