<?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</title>
	<atom:link href="http://www.codediesel.com/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" 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">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/" 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/" 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">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>Using the new Page visibility API in your apps</title>
		<link>http://www.codediesel.com/javascript/using-the-new-page-visibility-api/</link>
		<comments>http://www.codediesel.com/javascript/using-the-new-page-visibility-api/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 07:01:43 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[interface]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2986</guid>
		<description><![CDATA[One of the features lacking in the current browser api is that of determining whether the web page is currently visible to the user or is hidden (either in another tab or window). The new Page Visibility API allows you to do just that &#8211; determine whether your web page is visible to the user, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the features lacking in the current browser api is that of determining whether the web page is currently visible to the user or is hidden (either in another tab or window). </p>
<p>The new <a href="http://www.w3.org/TR/2011/WD-page-visibility-20110602/" title="page visibility api" target="_blank">Page Visibility API</a> allows you to do just that &#8211; determine whether your web page is visible to the user, is hidden in a background tab or window, or is prerendering. It allows the developer to use the page visibility state in JavaScript logic to make the user experience more friendly; for example, by stopping video, animation or slideshow playback whenever the user switches to another browser tab or window, and resuming whenever the user switches back. Also if your page is doing some ajax processing periodically, which consumes precious system resources, we can pause it when the page is not in focus. Other use can be in analytics, checking how long the page had been in actual user focus, rather then as a hidden tab or window.</p>
<p>Check the below demo page to see how this works. The demo was tested in Safari, Opera 11.10, Chrome and Firefox.<br />
<span id="more-2986"></span><br />
<a class="view_demo" target="_blank" href="http://www.codediesel.com/demos/visibility/"></a></p>
<p>Notice how the video pauses whenever the page is hidden. Also check the title of the page as it switches state.</p>
<p>The api is quite simple in its design &#8211; whenever a web page visibility changes, the &#8216;visibilitychange&#8217; event is fired. This event can then be registered using the addEventListener method.</p>
<p>Currently the Page Visibility API supports three visibility states:<br />
   &#8216;visible&#8217; : user has opened the page and is working within it.<br />
   &#8216;hidden&#8217; : user has switched to another tab or minimized browser window.<br />
   &#8216;prerender&#8217; : browser is just prerendering a page.</p>
<p>However the problem with the current api implementation is that different browsers have implemented the api specification a little differntly, which can make working with them a little tedious. So one option is to use a wrapper which hides the various browser differences. <a href="https://github.com/evilmartians/visibility.js#readme" title="visibility.js" target="_blank">Visibility.js</a> is one such wrapper which eases the usage of the api by hiding vendor-specific property prefixes and adding some high-level functions.</p>
<p>Below is the complete source for the above demo page. I&#8217;d not go into further details, as the respective pages have all the detailed documentation.</p>

<div class="wp_codebox"><table><tr id="p298648"><td class="code" id="p2986code48"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>meta charset<span style="color: #339933;">=</span><span style="color: #3366CC;">'UTF-8'</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;/</span>title<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/visibility.fallback.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/visibility.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;swfobject.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>Visibility.<span style="color: #660066;">js</span> test<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Page Visibility API <span style="color: #339933;">&lt;</span>strong id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;support&quot;</span><span style="color: #339933;">&gt;&lt;/</span>strong<span style="color: #339933;">&gt;</span>.<span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Switch browser tabs or window.<span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;ytapiplayer&quot;</span><span style="color: #339933;">&gt;</span>
You need Flash player <span style="color: #CC0000;">8</span><span style="color: #339933;">+</span> and JavaScript enabled to view <span style="color: #000066; font-weight: bold;">this</span> video.
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> onYouTubePlayerReady<span style="color: #009900;">&#40;</span>playerId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  ytplayer <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;myytplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ytplayer<span style="color: #009900;">&#41;</span> ytplayer.<span style="color: #660066;">playVideo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> pause<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ytplayer<span style="color: #009900;">&#41;</span> ytplayer.<span style="color: #660066;">pauseVideo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> params <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> allowScriptAccess<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;always&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> atts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> id<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;myytplayer&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
swfobject.<span style="color: #660066;">embedSWF</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.youtube.com/v/ylLzyHk54Z0?&quot;</span> <span style="color: #339933;">+</span>
                   <span style="color: #3366CC;">&quot;enablejsapi=1&amp;playerapiid=ytplayer&amp;version=3&quot;</span><span style="color: #339933;">,</span>
                   <span style="color: #3366CC;">&quot;ytapiplayer&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;425&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;356&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;8&quot;</span><span style="color: #339933;">,</span> 
                   <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> params<span style="color: #339933;">,</span> atts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009966; font-style: italic;">/* Make sure page visibility api is supported */</span>                   
<span style="color: #003366; font-weight: bold;">var</span> support <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'support'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Visibility.<span style="color: #660066;">isSupported</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    support.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'is supported'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    support.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'isn’t supported'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
document.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> Visibility.<span style="color: #660066;">state</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/* Pause/Play video when the page changes state */</span>
Visibility.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/* Also change the page title on state change */</span>
    document.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> state<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>state <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;visible&quot;</span><span style="color: #009900;">&#41;</span>
        play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
        pause<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/using-the-new-page-visibility-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Generating clean URLs with javascript</title>
		<link>http://www.codediesel.com/javascript/generating-clean-urls-with-javascript/</link>
		<comments>http://www.codediesel.com/javascript/generating-clean-urls-with-javascript/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 08:43:55 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2983</guid>
		<description><![CDATA[In a recent project I needed to generate clean search urls on a form submit. There are basically two ways to do that. One is to post the search variables to the same page, and generate a clean url using PHP and then redirect it to the new url. The other way is to generate [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project I needed to generate clean search urls on a form submit. There are basically two ways to do that. One is to post the search variables to the same page, and generate a clean url using PHP and then redirect it to the new url. The other way is to generate clean urls using JavaScript and immediately direct the page to the new url. This saves some processing on the server and one redirection, and also allows us to generate a url without a page refresh. I decided to go with the JavaScript solution.<br />
<span id="more-2983"></span><br />
A rough idea of the first option is given below.</p>

<div class="wp_codebox"><table><tr id="p298355"><td class="code" id="p2983code55"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'search'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$clean_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
   <span style="color: #009933; font-style: italic;">// code to create a clean url. </span>
   <span style="color: #009933; font-style: italic;">// After that $clean_url will contain the new redirect url </span>
   <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: <span style="color: #006699; font-weight: bold;">$clean_url</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>form <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myForm&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;get&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;search&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;search&quot;</span> maxlength<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Search&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>The problem with the first option is that you cannot generate a clean url without refreshing the page. This can be problematic if your application requires you to generate clean urls without going back to the server (which entails a page refresh).</p>
<p>So for example if on form submission you have some ugly urls like the following.</p>

<div class="wp_codebox"><table><tr id="p298356"><td class="code" id="p2983code56"><pre class="html4strict" style="font-family:monospace;">http://mysite.com/search.php?keyword=blue+boxed+buttons</pre></td></tr></table></div>

<p>You can generate clean urls like the following on the client and then send it to the server.</p>

<div class="wp_codebox"><table><tr id="p298357"><td class="code" id="p2983code57"><pre class="html4strict" style="font-family:monospace;">http://mysite.com/search/blue-boxed-buttons</pre></td></tr></table></div>

<p>The basic search form is shown below.</p>

<div class="wp_codebox"><table><tr id="p298358"><td class="code" id="p2983code58"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myForm&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;get&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">maxlength</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Search&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<p>Now the following code will handle the clean url generation on the above form submission. It will intercept the form submission, generate a clean url, and direct the page to the new url, all without a page refresh.</p>

<div class="wp_codebox"><table><tr id="p298359"><td class="code" id="p2983code59"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Friendly URL rewrite */</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/* Remove unwanted characters, only accept alphanumeric and space */</span>
    <span style="color: #003366; font-weight: bold;">var</span> keyword <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#search'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^A-Za-z0-9 ]/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/* Replace multi spaces with a single space */</span>
    keyword <span style="color: #339933;">=</span> keyword.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s{2,}/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009966; font-style: italic;">/* Replace space with a '-' symbol */</span>
    keyword <span style="color: #339933;">=</span> keyword.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> cleanUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://mysite.com/search/'</span> <span style="color: #339933;">+</span> keyword.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    window.<span style="color: #660066;">location</span> <span style="color: #339933;">=</span> cleanUrl<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// Prevent default button behaviour</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Below is the Apache htaccess rule to go along with the above redirection. Our main search page is called &#8216;search.php&#8217;.</p>

<div class="wp_codebox"><table><tr id="p298360"><td class="code" id="p2983code60"><pre class="text" style="font-family:monospace;">RewriteEngine on
RewriteBase /
RewriteRule ^search/([A-Za-z0-9-]+)/?$ search.php?keyword=$1 [NC,L]</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/generating-clean-urls-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rejecting unwanted characters from input</title>
		<link>http://www.codediesel.com/php/rejecting-unwanted-characters-from-input/</link>
		<comments>http://www.codediesel.com/php/rejecting-unwanted-characters-from-input/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 09:23:12 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2981</guid>
		<description><![CDATA[It seems that some common elements of programming stump us from time to time. Take the task of filtering a input search string in PHP to remove unwanted characters. Using a RegEx many developers find it easy to search for a substring, but find it difficult to use the same to reject some particular characters [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that some common elements of programming stump us from time to time. Take the task of filtering a input search string in PHP to remove unwanted characters. Using a RegEx many developers find it easy to search for a substring, but find it difficult to use the same to reject some particular characters from a string. A simple solution is shown below, which rejects all the characters from the input except alphanumeric and a space.<br />
<span id="more-2981"></span></p>

<div class="wp_codebox"><table><tr id="p298166"><td class="code" id="p2981code66"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$search</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;the great /%&amp;&amp;world ,fair of 1964&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cleaned</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[^A-Za-z0-9 ]/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$search</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Returns:</p>

<div class="wp_codebox"><table><tr id="p298167"><td class="code" id="p2981code67"><pre class="text" style="font-family:monospace;">the great world fair of 1964</pre></td></tr></table></div>

<p>The important part of the regular expression is the caret ^ along with the character class [...]. The normal character class will match elements specified in the class. For example the following will match the alphanumeric characters and a space <em>in</em> the class and replace them with a empty character, effectively removing them, because we have specified a empty string as the second parameter in preg_replace.</p>

<div class="wp_codebox"><table><tr id="p298168"><td class="code" id="p2981code68"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$search</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;the great /%&amp;&amp;world ,fair of 1964&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cleaned</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[A-Za-z0-9 ]/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$search</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Returns:</p>

<div class="wp_codebox"><table><tr id="p298169"><td class="code" id="p2981code69"><pre class="text" style="font-family:monospace;">/%&amp;&amp;,</pre></td></tr></table></div>

<p>However if we use a negated character class, which is a character class starting with a caret ^ sign, we invert the meaning of the class. Now it means match all the characters <em>not</em> in the character class, which basically removes all the unwanted characters.</p>
<p>If you are using ereg_replace then you need to use the following,which does not have any delimiters. Note that this function has been DEPRECATED as of PHP 5.3.0, and will throw a &#8216;Deprecated&#8217; error.</p>

<div class="wp_codebox"><table><tr id="p298170"><td class="code" id="p2981code70"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cleaned</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ereg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[^A-Za-z0-9 ]&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$search</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/rejecting-unwanted-characters-from-input/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Amazon Advertising API BrowseNodes</title>
		<link>http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/</link>
		<comments>http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 16:25:35 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[amazon]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2978</guid>
		<description><![CDATA[Amazon uses a hierarchy of nodes to organize its various items. Each node represents a collection of items, such as Harry Potter books, not the items themselves. Browse node IDs are positive integers that uniquely identify product collections, for example, Literature &#038; Fiction: (17), Medicine: (13996), Mystery &#038; Thrillers: (18), Nonfiction: (53), Outdoors &#038; Nature: [...]]]></description>
			<content:encoded><![CDATA[<p>Amazon uses a hierarchy of nodes to organize its various items. Each node represents a collection of items, such as Harry Potter books, not the items themselves. Browse node IDs are positive integers that uniquely identify product collections, for example, Literature &#038; Fiction: (17), Medicine: (13996), Mystery &#038; Thrillers: (18), Nonfiction: (53), Outdoors &#038; Nature: (290060). Amazon uses over 120,000 browse node IDs in the US locale alone.</p>
<p>The BrowseNodes tool automatically gets all the child BrowseNodes and their names. The program recursively traverses the BrowserNode hierarchy and returns all the BrowseNodes and their respective names. You can display the nodes on the console or save it to a CSV file. You can also include the library in your existing projects to process BrowseNodes.<br />
<span id="more-2978"></span></p>
<h4>Library Usage</h4>
<p>The library is easy to use, and requires PHP and CURL installed on your system. The download includes a sample file with which you can immediately test the code. The sample file is shown below.</p>

<div class="wp_codebox"><table><tr id="p297881"><td class="code" id="p2978code81"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the BrowseNodes to the console */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMedia</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBrowseNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1266092011&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Before you begin to use the library, you need to enter your Amazon security credentials as shown above. Also in the &#8216;aws_signed_request.php&#8217; file you will need to enter your Amazon Associate ID, as it is now mandatory. Find the following line in the file and add your Associated ID.</p>

<div class="wp_codebox"><table><tr id="p297882"><td class="code" id="p2978code82"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* aws_signed_request.php */</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;AssociateTag&quot;</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR-ASSOCIATES-ID-HERE'</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>That is it. Now you are ready to get the BrowserNodes from Amazon. You can display the BrowserNodes on you console/browser or save it directly to a CSV file. For that you have to use the &#8216;setMedia&#8217; method. Both example are shown below.</p>
<h4>Display the BrowseNodes on the console</h4>

<div class="wp_codebox"><table><tr id="p297883"><td class="code" id="p2978code83"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the BrowseNodes to the console */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMedia</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBrowseNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1266092011&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You enter the parent browsenode in the &#8216;getBrowseNodes&#8217; method, after which the function recursively retrieves all the children nodes. To get started, you can find all the top browsenode values <a href="http://affiliate-program.amazon.com/gp/redirect.html/ref=amb_link_7285822_2?location=http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/BrowseNodeIDs.html&#038;token=948F5A3F73BA8D32EB91AADB246FBC94772F93D8&#038;pf_rd_m=ATVPDKIKX0DER&#038;pf_rd_s=assoc-center-1&#038;pf_rd_r=&#038;pf_rd_t=501&#038;pf_rd_p=&#038;pf_rd_i=assoc_help_t41_a6" target="_blank">here</a>, or you can get it from the Amazon link itself.</p>
<p>The output of the above is shown below. The output is indented so that you can visualize the hierarchy of the nodes.</p>

<div class="wp_codebox"><table><tr id="p297884"><td class="code" id="p2978code84"><pre class="text" style="font-family:monospace;">Television &amp; Video : AV Receivers &amp; Amplifiers : 3213035011
Television &amp; Video : Blu-ray Players &amp; Recorders : 3213025011
      Blu-ray Players &amp; Recorders : Blu-ray Recorders : 3213026011
      Blu-ray Players &amp; Recorders : Blu-ray Players : 352697011
Television &amp; Video : DVD Players &amp; Recorders : 3213027011
      DVD Players &amp; Recorders : DVD Players : 1036922
      DVD Players &amp; Recorders : DVD Recorders : 1036920
Television &amp; Video : DVD-VCR Combos : 886258
Television &amp; Video : HD DVD Players : 352696011
Television &amp; Video : Televisions : 172659
Television &amp; Video : Home Theater Systems : 281056
.
.</pre></td></tr></table></div>

<p>The format of each line is given below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/12/browsenode.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/12/browsenode.gif" alt="" title="amazon advertising browsenode" width="436" height="88" class="aligncenter size-full wp-image-2958" /></a></p>
<h4>Saving the BrowseNodes to a CSV</h4>
<p>The following code shows how to save the browsenodes to a CSV file.</p>

<div class="wp_codebox"><table><tr id="p297885"><td class="code" id="p2978code85"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Write the BrowseNodes to a CSV file */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMedia</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;csv&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;./nodes.csv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBrowseNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1266092011&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The code saves all the browsenodes in a CSV file, after which you can use it with Excel or a simple text editor. A sample CSV ouput is shown below. The format is the same as the one given above.</p>

<div class="wp_codebox"><table><tr id="p297886"><td class="code" id="p2978code86"><pre class="text" style="font-family:monospace;">&quot;PlayStation 3&quot;,&quot;All Games&quot;,&quot;14210861&quot;
&quot;PlayStation 3&quot;,&quot;Action&quot;,&quot;14210761&quot;
&quot;Action&quot;,&quot;Fighting&quot;,&quot;14210981&quot;
&quot;Action&quot;,&quot;Military &amp; Espionage&quot;,&quot;14210821&quot;
&quot;Action&quot;,&quot;Shooter&quot;,&quot;14210841&quot;
&quot;PlayStation 3&quot;,&quot;Adventure&quot;,&quot;14210851&quot;
&quot;PlayStation 3&quot;,&quot;Arcade&quot;,&quot;14210911&quot;
&quot;PlayStation 3&quot;,&quot;Board Games&quot;,&quot;14210921&quot;</pre></td></tr></table></div>

<h4>Changing Locales</h4>
<p>Amazon being a world-wide venture the Product Advertising API operates in the following locales:<br />
    CA<br />
    CN<br />
    DE<br />
    ES<br />
    FR<br />
    IT<br />
    JP<br />
    UK<br />
    US</p>
<p>You can use different locale code to get the BrowseNodes for different regions. You set the locale parameter using the &#8216;$region&#8217; variable; for example:</p>

<div class="wp_codebox"><table><tr id="p297887"><td class="code" id="p2978code87"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$region</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span></pre></td></tr></table></div>

<h4>Getting the parent node details</h4>
<p>You can also get information about a nodes parent node using the &#8216;getParentNode&#8217; method.</p>

<div class="wp_codebox"><table><tr id="p297888"><td class="code" id="p2978code88"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParentNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;14210821&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>One other useful method you can use it &#8216;getNodeName&#8217;. This allows you to get the name of a given node id. This can be useful if you have a nodeid but need to gets it name.</p>

<div class="wp_codebox"><table><tr id="p297889"><td class="code" id="p2978code89"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</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: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNodeName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;14210821&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Note</strong>: Some categories contain hundreds to thousands of BrowserNodes, retreiving them can take some time and could trigger a PHP timeout error. To avoid this set PHP timeout to a large number as given below.</p>

<div class="wp_codebox"><table><tr id="p297890"><td class="code" id="p2978code90"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div  class="download2">
<a href="http://www.codediesel.com/downloads/amazon-bnodes">Download code files</a><br />
<span>Downloads : 157  / File size : 3.1 kB</span>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create a infinite scroll page using jQuery</title>
		<link>http://www.codediesel.com/javascript/create-a-infinite-scroll-page-using-jquery/</link>
		<comments>http://www.codediesel.com/javascript/create-a-infinite-scroll-page-using-jquery/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 09:54:56 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[interface]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2972</guid>
		<description><![CDATA[Waypoints is a small jQuery plugin that makes it easy to execute a function whenever you scroll to a particular element &#8211; a &#8216;waypoint&#8217;. This can be used to highlight when a element comes into view, load additional html or throw a popup. In this post we will use it to create a infinite scroll [...]]]></description>
			<content:encoded><![CDATA[<p>Waypoints is a small jQuery plugin that makes it easy to execute a function whenever you scroll to a particular element &#8211; a &#8216;waypoint&#8217;. This can be used to highlight when a element comes into view, load additional html or throw a popup. In this post we will use it to create a infinite scroll page.</p>
<p>An infinite scroll page can be useful when you do not want to add pagination, but would rather load the content on the same page when the user requests.<br />
<span id="more-2972"></span><br />
<a href="http://imakewebthings.github.com/jquery-waypoints/" title="waypoints" target="_blank">Waypoints</a> requires jQuery, so you need to add that to your page before initializing waypoints.</p>

<div class="wp_codebox"><table><tr id="p297295"><td class="code" id="p2972code95"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;jquery-1.4.4.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;waypoints.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><a class="view_demo" target="_blank" href="http://www.codediesel.com/demos/waypoints/"></a></p>
<p>The following is how you register an element as a waypoint. When the user scrolls past that element it triggers a <em>waypoint.reached</em> event, which you can then use to run your custom code.</p>
<p>For example if you have a footer element with the following structure, you can attach a function to be executed when that element comes in view.</p>

<div class="wp_codebox"><table><tr id="p297296"><td class="code" id="p2972code96"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;footer&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span>&gt;</span>  
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Footer text<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>footer&gt;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p297297"><td class="code" id="p2972code97"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	opts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		offset<span style="color: #339933;">:</span> <span style="color: #3366CC;">'100%'</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009966; font-style: italic;">/* Execute the following when the footer comes in view */</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'footer'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">waypoint</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> direction<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You have scrolled to the end of the page.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In the above example we only displayed a alert dialog, but what we instead want is additional data to be appended to the existing content, giving us our infinite page. The following is a example code to create a infinite page by loading additional data from a php server page.</p>

<div class="wp_codebox"><table><tr id="p297298"><td class="code" id="p2972code98"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$footer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'footer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	opts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		offset<span style="color: #339933;">:</span> <span style="color: #3366CC;">'100%'</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/* When the 'footer' element comes in view, 
           the following code is executed */</span>
	$footer.<span style="color: #660066;">waypoint</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> direction<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$footer.<span style="color: #660066;">waypoint</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'remove'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">/* We get additional data from a php page and 
                   append the same to the end of the page. */</span>
		$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'more-data.php'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#main'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$footer.<span style="color: #660066;">waypoint</span><span style="color: #009900;">&#40;</span>opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can find additional options at the waypoints page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/create-a-infinite-scroll-page-using-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparing programming languages</title>
		<link>http://www.codediesel.com/software/comparing-programming-languages/</link>
		<comments>http://www.codediesel.com/software/comparing-programming-languages/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 04:27:42 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[languages]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2970</guid>
		<description><![CDATA[Programming language comparisons raise the most flame wars on blogs and forums. Each developer rooting for his favorite language and deriding all the others. Most comparisons are like comparing apples-to-oranges, devoid of any logic, and arguments frequently hinging on ad-hoc rationalization and technical naïveté. Seldom do we see any empirical evidence backing up the claims. [...]]]></description>
			<content:encoded><![CDATA[<p>Programming language comparisons raise the most flame wars on blogs and forums. Each developer rooting for his favorite language and deriding all the others. Most comparisons are like comparing apples-to-oranges, devoid of any logic, and arguments frequently hinging on ad-hoc rationalization and technical naïveté.<br />
<span id="more-2970"></span><br />
Seldom do we see any empirical evidence backing up the claims. This is one of the reasons I rarely become part of the ‘my-language-is-better’ rigmarole. The most comprehensive empirical study on programming language comparisons I found was at <a href="http://www.plat-forms.org/" title="Plat_Forms - The web development platform comparison" target="_blank">http://www.plat-forms.org/</a>. </p>
<p>&#8220;Plat_Forms&#8221; is a contest in which teams of three programmers compete to implement the same requirements for a web-based system using different technology platforms (e.g. Java, .NET, Perl, PHP, Python, Ruby, Scala, Smalltalk, JavaScript or what-have-you). The main contenders for 2011 were Ruby, PHP, Perl and Java. The results for the 2011 are given <a href="http://www.plat-forms.org/results-2011" title="Plat_Forms - The web development platform comparison" target="_blank">here</a>. Compared to the 2007 study, the results differ markedly.</p>
<p>The detailed results are yet to come, but you can draw your conclusions from whatever data is currently available.</p>
<p><strong>Further reading:</strong><br />
Chapter 14 &#8211; Two comparisons of Programming Language from <a href="http://www.amazon.com/Making-Software-Really-Works-Believe/dp/0596808321/ref=sr_1_sc_1?ie=UTF8&#038;qid=1323837444&#038;sr=8-1-spell" title="making software" target="_blank">Making software</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/software/comparing-programming-languages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing WordPress admin login error message</title>
		<link>http://www.codediesel.com/security/changing-wordpress-admin-login-error-message/</link>
		<comments>http://www.codediesel.com/security/changing-wordpress-admin-login-error-message/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 05:13:15 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2965</guid>
		<description><![CDATA[It can be surprising how easy it is to overlook security issues in software design. WordPress for example, after all the versions, still displays login error information that can be informative for a potential hacker. Rather than displaying a generic login error message, WordPress admin specifically displays whether a &#8216;username&#8217; was entered wrong or a [...]]]></description>
			<content:encoded><![CDATA[<p>It can be surprising how easy it is to overlook security issues in software design. WordPress for example, after all the versions, still displays login error information that can be informative for a potential hacker. Rather than displaying a generic login error message, WordPress admin specifically displays whether a &#8216;username&#8217; was entered wrong or a &#8216;password&#8217; as the following screenshot shows.<br />
<span id="more-2965"></span><br />
<a href="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login1.gif" alt="" title="wp-login1" width="330" height="293" class="aligncenter size-full wp-image-2966" /></a></p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login3.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login3.gif" alt="" title="wp-login3" width="333" height="307" class="aligncenter size-full wp-image-2969" /></a></p>
<p>This can be useful for a hacker to get  the username right. A good idea is to display a generic message as given below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login2.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login2.gif" alt="" title="wp-login2" width="331" height="291" class="aligncenter size-full wp-image-2967" /></a></p>
<p>You can easily achieve that by adding the following line to your themes &#8216;functions.php&#8217; file.</p>

<div class="wp_codebox"><table><tr id="p2965101"><td class="code" id="p2965code101"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login_errors'</span><span style="color: #339933;">,</span>
            <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$no_login_error'</span><span style="color: #339933;">,</span> 
                            <span style="color: #0000ff;">&quot;return 'Oops! Wrong Credentials.';&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The second argument to the &#8216;add_filter&#8217; creates a anonymous callback function. This is equivalent to the following.</p>

<div class="wp_codebox"><table><tr id="p2965102"><td class="code" id="p2965code102"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> no_login_error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">'Oops! Wrong Credentials.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login_errors'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'no_login_error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/changing-wordpress-admin-login-error-message/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress plugins to check for security threats</title>
		<link>http://www.codediesel.com/security/wordpress-plugins-to-check-for-security-threats/</link>
		<comments>http://www.codediesel.com/security/wordpress-plugins-to-check-for-security-threats/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 05:47:53 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2952</guid>
		<description><![CDATA[With WordPress installations on the rise, security threats are a common concern for users. Below is a small collection of WordPress plugins that I found adequate for use on most WordPress installations to keep security in check. As always security is not 100% achievable, but the following plugins will help you find any loopholes in [...]]]></description>
			<content:encoded><![CDATA[<p>With WordPress installations on the rise, security threats are a common concern for users. Below is a small collection of WordPress plugins that I found adequate for use on most WordPress installations to keep security in check. As always security is not 100% achievable, but the following plugins will help you find any loopholes in your site and help you monitor for any security breaches.<br />
<span id="more-2952"></span><br />
<strong>BulletProof Security</strong><br />
<a href="http://wordpress.org/extend/plugins/bulletproof-security/" title="BulletProof Security" target="_blank">BulletProof Security</a> protects your website from XSS, RFI, CSRF, Base64, Code Injection and SQL Injection hacking attempts. The plugin also Protects wp-config.php, bb-config.php, php.ini, php5.ini, install.php and readme.html with .htaccess security protection. BPS has built-in Backup and Restore, a File Editor, a File Uploader and a File Downloader to make managing the BPS master .htaccess files and your website security fast and simple.</p>
<p><strong>WordPress File Monitor</strong><br />
<a href="http://wordpress.org/extend/plugins/wordpress-file-monitor/" title="WordPress File Monitor" target="_blank">WFM</a> monitors your WordPress installation for any added, deleted or changed files. When a change is detected an email is sent to a specified address. Currently does not support multi-site installations.</p>
<p><strong>Ultimate Security Checker</strong><br />
<a href="http://wordpress.org/extend/plugins/ultimate-security-checker/" title="Ultimate Security Checker" target="_blank">Ultimate Security Checker</a> helps you identify security problems with your WordPress installation. It scans your WordPress blog for hundreds of known threats, then gives you a security &#8220;grade&#8221; based on how well you have protected yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/wordpress-plugins-to-check-for-security-threats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 4.273 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-02-04 02:57:45 -->

