Monthly Archives: July 2010

Perl script to get video details

This is a command line Perl script I made a while back to simply show details about a specified video. It is tested on Ubuntu.

You can modify it for your specific requirements or grab some code for another use.

#!/usr/bin/perl -w
use strict;
use warnings;
 
use IPC::Open3;
 
# example
my $filename  = $ARGV[0];
my %videoInfo = videoInfo($filename);
print "duration: " . $videoInfo{'duration'} . "\n";
print "durationsecs: " . $videoInfo{'durationsecs'} . "\n";
print "bitrate: " . $videoInfo{'bitrate'} . "\n";
print "vcodec: " . $videoInfo{'vcodec'} . "\n";
print "vformat: " . $videoInfo{'vformat'} . "\n";
print "acodec: " . $videoInfo{'acodec'} . "\n";
print "asamplerate: " . $videoInfo{'asamplerate'} . "\n";
print "achannels: " . $videoInfo{'achannels'} . "\n";
 
#
# returns media information in a hash
sub videoInfo {
	# ffmpeg command
	my $ffmpeg = '/usr/local/bin/ffmpeg';
 
	my %finfo = (
				  'duration'     => "00:00:00.00",
				  'durationsecs' => "0",
				  'bitrate'      => "0",
				  'vcodec'       => "",
				  'vformat'      => "",
				  'acodec'       => "",
				  'asamplerate'   => "0",
				  'achannels'       => "0", 
	);
 
	my $file = shift;
 
	# escaping characters
	$file =~ s/(\W)/\\$1/g;
 
	open3( "/dev/null", \*ERPH, "$ffmpeg -i $file" ) or die "can't run $ffmpeg\n";
	my @res = ;
 
	# parse ffmpeg output
	foreach (@res) {
        print;
 
		# duration
		if (m!Duration: ([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])!) {
			$finfo{'duration'} = $1;
		}
 
		# bitrate
		if (m!bitrate: (\d*) kb/s!) {
			$finfo{'bitrate'} = $1;
		}
 
		# vcodec and vformat
		if (/Video: (\w*), (\w*),/) {
			$finfo{'vcodec'}  = $1;
			$finfo{'vformat'} = $2;
		}
 
        # Stream #0.1(und): Audio: aac, 48000 Hz, 1 channels, s16, 64 kb/s
 
		# acodec, samplerate, stereo and audiorate
		if (m!Audio: (\w*), (\d*) Hz, (\d*)!) {
			$finfo{'acodec'}     = $1;
			$finfo{'asamplerate'} = $2;
			$finfo{'achannels'}     = $3;
		}
	}
 
	my $tenths  = substr( $finfo{'duration'}, 9, 2 );
	my $seconds = substr( $finfo{'duration'}, 6, 2 );
	my $minutes = substr( $finfo{'duration'}, 3, 2 );
	my $hours   = substr( $finfo{'duration'}, 0, 2 );
	$finfo{'durationsecs'} = ( $tenths * .01 ) + $seconds + ( $minutes * 60 ) + ( $hours * 360 );
 
	return %finfo;
}

Create a Drupal site on an Ubuntu Server

This will help you quickly install a Drupal site on an Ubuntu server.

This is my script for this purpose. Use it as you will. This procedure assumes that you have already installed Apache, MySQL, phpMyAdmin and PHP. It also assumes that you are familiar with everything. This is probably not a beginner’s guide, but an aid to someone more experienced that wants to install Drupal quickly.

Create and run a shell script to create your site

Use this template to create a shell script that will create your site. Create a text file from the following template called “make_dru.sh” (in your home directory is fine). Modify lines 7 to 17 for your installation. Then use “chmod +x make_dru.sh” and execute the script with “sudo ./make_dru.sh”.

#!/bin/bash
#
# make drupal project
#

# 
OWNER="andrew" 			# change to your username
GROUP="staff"			# change to your username or the group that will work on the site
DRUPAL_URL="http://ftp.drupal.org/files/projects/drupal-6.17.tar.gz" # update to the current version, if there is one
SITESROOT="/usr/local/var/www"  # change to the directory where you put website directories
URL="www.sitename.com"		# change to your site's address

# database.php parameters
DBUSER="sitename"		# change to something meaningful
DBPWD="passwordgoeshere"		# change to a long password
DBHOST="localhost"		# leave alone
DBDATABASE=$DBUSER		# leave alone
 
#
# make sure this is run as root
#
if [[ $UID -ne 0 ]]; then
    echo "Not running as root"
    exit
fi
 
if [ ! -d $SITESROOT ]; then
	echo "$SITESROOT directory does not exist"
	exit
fi
 
#
# check for existence of the specified user owner
#
/bin/egrep  -i "^$OWNER" /etc/passwd > /dev/null
if [ ! $? -eq 0 ]; then
   echo "User $OWNER, which is specified as the owner, does not exist in /etc/passwd"
   exit
fi
 
#
# check for existence of the specified group
#
/bin/egrep -i "^$GROUP" /etc/group > /dev/null
if [ ! $? -eq 0 ]; then
   echo "$GROUP, which is specified as the group to use, does not exist in /etc/group"
   exit
fi
 
#
# change to the siteroot directory
#
cd $SITESROOT
 
if [ -d $URL ]; then
	echo "$SITESROOT/$URL directory already exists"
	exit
fi 
 
mkdir $URL
chown $OWNER:$GROUP $URL
cd $URL
 
#
# get Drupal
#
echo 'get Drupal'
wget -O drupal.tgz $DRUPAL_URL
tar -xzvf drupal.tgz
find . -maxdepth 1 -type d -name 'drupal*' -exec mv {} public \;
sudo chown -R $OWNER:$GROUP public
find . -type d -exec chmod g+ws {} \;
 
#
# temporarily allow write to settings
#
cp public/sites/default/default.settings.php public/sites/default/settings.php
chmod a+w public/sites/default/settings.php
chmod a+w public/sites/default
 
#
# logs
#
echo "set up logs"
mkdir logs
sudo chown $OWNER:www-data logs
chmod g+ws logs
touch logs/error.log
touch logs/combined.log
 
#
# create Apache virtual site
#
echo creating Apache virtual site file in /etc/apache2/sites-available
echo "
	ServerName $URL
	DocumentRoot $SITESROOT/$URL/public
	DirectoryIndex index.php
	LogLevel warn
	ErrorLog $SITESROOT/$URL/logs/error.log
	CustomLog $SITESROOT/$URL/logs/combined.log combined

" > /etc/apache2/sites-available/$URL
 
#
# finish up
#
echo -e "Drupal website was created in: $SITESROOT/$URL\n"
echo -e "To finish, do the following:\n"
echo "1. Enable site with: sudo a2ensite $URL"
echo "2. Restart Apache with: sudo /etc/init.d/apache2 restart"
echo "3. Make a crontab entry ('sudo crontab -e') like: 0 * * * * wget -O - -q -t 1 http://$URL/cron.php"
echo -e "4. Create a MySQL database with:\n"
echo -e "\t     user: $DBUSER"
echo -e "\t      pwd: $DBPWD"
echo -e "\t     host: $DBHOST"
echo -e "\t database: $DBDATABASE"
echo "5. Open the site in your browser and finish the set-up."
echo "6. Remove write privileges with: sudo chmod og-w public/sites/default/settings.php"
echo "   ..and: sudo chmod og-w public/sites/default"

Create the database and user with phpMyAdmin

  • click the “Privileges” tab and add a new user
  • in the “User name” field, enter the sitename that you entered for DBUSER in the script
  • in the Host field, select “Local”
  • enter or generate a password – make it a long one, like 20 characters
  • in the Database for User list, select “Create database with same name and grant all privileges”
  • click Go

Enable the new Drupal website

This will tell Apache to enable the virtual site that was made in the script and then restart Apache.

sudo a2ensite www.sitename.com
sudo /etc/init.d/apache2 restart

Configure the site

Open site in browser and configure it

Remove temporary write privileges

During site creation, write privileges were needed, after site set-up, remove them. In a shell, while in the sites directory (where the public directory is run these commands:

sudo chmod og-w public/sites/default/settings.php
sudo chmod og-w public/sites/default