Here is a little plug-in for Wordpress that I created to grab the USER_AGENT header string. It is very simple at best, so don’t expect to move any mountains with it. You can read more about it on The User Agent Identify page.
User Agent Identify is a plug-in for Wordpress. The plug-in doesn’t do anything special, so don’t expect it to. This plug-in simply determines if the visitor’s browser is a mobile based browser (ie. iPhone, Android…) and returns true of false. I created this plug-in because I needed a way to dynamically bypass other plug-ins depending on if the browser was mobile or not. Specifically, I wanted to bypass the SuperSlider-Login plug-in and the wp-Followme plug-in. This plug-in DOES NOT disable any other plugins! After installation of this plug-in all you need to do is make a call to the user_agent_identify() class from within whatever plug-in or page you need. This plug-in will only return true or false.
I have included a few USER_AGENT strings in the plug-in. Feel free to modify it or add to it.
Here is a screenshot of the Options page. As you can see, there aren’t any options at all, just a few lines to show that it’s working properly.
I have no plans on submitting this plug-in to Wordpress, as there are already similar plug-ins available. You can download the plug-in here. Use it at your own risk.
Okay so it’s been a year since my last post, sue me!
So lately I’ve been preoccupied with learning Ruby. Ruby is a very sweet language, I won’t go into the reasons why, that’s what google is for. This post is about my first attempt at coding in Ruby. Here’s the scenario, I have an external Hard Drive with around 1TB(1602 files) of AVI files. These files are in sub directories named by date. The AVI’s are from my security DVR system and as such are not compressed very well. Each file takes up between 500mb to 1 gig of space. I have been transcoding them with FFmpeg manually for the last few days, and knew I could write a simple Bash script to do it for me. Since I have been bored with doing things the easy way, I thought “Let’s learn a new language just for this task!”. Stupid, huh? Anyways, So I set out to learn Ruby. Now, I am certainly no expert and I know my coding will probably make Ruby guru’s gag, but it works for me and you are more than welcome to use it at your own risk. I take no responsibility in you deleting all your files or formatting your system.
GOAL: Recursively find and transcode all files matching the user inputted file extension.
Prerequisites:
Linux (I personally use CentOS 5.4)
FFmpeg compiled with h.264 and libfaac (which is beyond the scope of this post)
Ruby
ProgressBar library for ruby
Now on to the Code:
Okay, so these first four lines are pretty self explanatory. If not, then you need to start learning Ruby or any language for that matter.
Fire up your favorite editor and insert the following:
#!/usr/bin/env ruby
require 'ftools'
require 'progressbar'
require 'find'
Next:
This tells ruby to clear the screen, Let’s start with a blank screen.
system ('clear')
Let’s create some functions:
The first function simply converts a measurement of time in seconds to days, hours, minutes, and seconds are returns a string with these values.
The second one simply defines three variables, start_time, end_time. and elapsed_time. The yield statement basically tells ruby to pause this function after defining the start_time variable, continue processing the main code (which we’ll get to in a second), and come back here once we’re done below.
def secs_to_days (secs)
days = ((secs / 60) / (60 * 24)).floor
hours = (((secs / 60) % (60 *24)) / 60).floor
minutes = (secs / 60) % 60
seconds = secs % 60
"Conversion took #{days.to_s} days,
#{hours.to_s} hours
#{minutes.to_s} minutes
and #{seconds.to_s} seconds."
end
def elapsed_time
start_time = Time.now
yield
end_time = Time.now
elapsed_time = end_time.to_i - start_time.to_i
end
This function simply presents the user with some questions and gathers the responses into an array and returns the array.
def user_input
responses = Array.new
recurse, delete = nil
print "What type of file are we going to look for? "
filetype = gets.chomp!
responses.push filetype
print "What is the full path where we should begin searching? "
basedir = gets.chomp!
responses.push basedir
print "Should we search recursively? (y/n) "
recurse = gets.chomp!
if /y/i =~ recurse
recurse = true
else
recurse = false
end
responses.push recurse
print "Delete the original files after conversion? (y/n) "
delete = gets.chomp!
if /y/i =~ delete
delete = true
else
delete = false
end
responses.push delete
return responses
end
Now we’re getting to the meat and bones. The find_files function does exactly that. It is passed our array from the previous function, uses our responses to set our starting directory, the type of file we are looking for and to either search only the current directory or to search recursively.
def find_files(criteria)
puts "Beginning Filesystem Search for specified files..."
puts "We will search for all files with an extension of #{criteria[0]}"
puts "Beginning in the directory named #{criteria[1]} and will"
if criteria[2] == true
puts "Search Recursively"
else
puts "NOT Search Recursively"
end
file_stack = Array.new
if criteria[2] == true
Find.find(criteria[1]) do |filename|
if File.extname(filename) == criteria[0]
file_stack.push filename
else
next
end
end
elsif criteria[2] == false
puts Dir.glob("*#{criteria[0]}")
file_stack = Dir.glob("*#{criteria[0]}")
else
puts "Oops, I broke!"
end
return file_stack
end
Okay, now this is the main function. The first two lines setup our progress bar. Then we start a loop to process our array of found files and transcode each one in turn using FFmpeg into an h.264 video and depending on our responses, will either delete the original file or not. I am also dumping all output from FFmpeg into log files, so if a problem arises, I can see what happened. After each file is transcoded, we tell our progress bar to update. Then, when our loop has reached the end, we tell our progress bar that we are done. One thing to note… my FFmpeg command line is predetermined. Meaning, I already know how many streams my AVI files have and how I want to process them. You will need to modify the FFmpeg command line to suit your needs.
def convert_video(filename, criteria)
pbar = ProgressBar.new("Conversion",100)
pbar_step = 100 / filename.nitems
filename.each do |video|
myfile_extension = File.extname(video)
myfile_basename = File.basename(video,myfile_extension)
myfile = File.dirname(video)+"/"+myfile_basename
system "ffmpeg -y -i #{video} -map 0.0 -map 0.1 -map 0.2 -map 0.3 \
-acodec libfaac -ab 64k -ac 2 -vcodec libx264 -r 29.97 -s 640x480 \
-vpre hq -crf 22 -threads 0 -f mp4 #{myfile}.mp4 -vcodec libx264 \
-r 29.97 -s 320x240 -vpre hq -crf 22 -newvideo -acodec libfaac \
-ab 64k -ac 2 -newaudio 2>> ffmpeg.log 1>> ffmpeg.error.log"
system "rm -rf #{video}" if criteria[3] == true
pbar.inc(pbar_step)
end
pbar.finish
end
This is where the main execution of our program starts. We have a variable named criteria which is initialized with our function user_input. The same goes for the variable named file_stack, except that the find_files function is passed our criteria variable. Then we just print out a line indicating how many files our little program found.
criteria = user_input()
file_stack = find_files(criteria)
puts "Looks like we\'ve found #{file_stack.nitems}"
And this last bit of code ties it all together. If we have files to process, then let the transcoding begin, if not, We’re Done!
if file_stack.nitems > 0
print "Press Enter to begin Transcoding or CTRL-C to cancel."
gets
convert = elapsed_time {
convert_video(file_stack, criteria)
}
puts secs_to_days(convert)
else
puts "Sorry, I couldn't find any files to process... Exiting!"
end
Now save your file. You can name it anything you want with an .rb extension. chmod +x it. Now run it.
Click here to see the entire source code. Use it as you will, but at your own risk.

