Tag: Snippet

Finding 500 Errors On DreamHost

August 16, 2010 » Consume, Geek

Recently I’ve been getting 500 errors once in a while on my DH account. This is usually because a process is long running and the DH monitor kills it for consuming too many resources. Meh. The real cause is putting waaay to much stuff on a shared account.

So, to see what my culripts are I wrote a quick script that I’m hooking to a cron job. First I’ll see what domains are generating 500 errors, and then later I’ll try to locate the exact scripts and clean them up.

500-Error-Finder.sh

#!/bin/bash

cd /home/jmhobbs/logs

for i in *; do
  if [ -d "$i" ] && [ -f "$i/http/error.log.0" ]; then
    ERRS=$( grep internal_error.html $i/http/error.log.0 | wc -l )
    if [ "0" != "$ERRS" ]; then
      echo "# $i - $ERRS"
    fi
  fi
done

The output looks like this.

[pristina]$ ./500-Error-Finder.sh 
# gallery.velvetcache.org - 11
[pristina]$

Python UNIX Sockets

June 14, 2010 » Geek

I’ve been tinkering with using UNIX sockets for IPC from Python and I thought I would share my most basic experiment.

This is a super simple example of client/server usage of a socket. Essentially the server is a blocking command socket that echo’s whatever is passed through it.

Listing: server.py

# -*- coding: utf-8 -*-
import socket
import os, os.path
import time

if os.path.exists( "/tmp/python_unix_sockets_example" ):
  os.remove( "/tmp/python_unix_sockets_example" )

print "Opening socket..."
server = socket.socket( socket.AF_UNIX, socket.SOCK_DGRAM )
server.bind("/tmp/python_unix_sockets_example")

print "Listening..."
while True:
  datagram = server.recv( 1024 )
  if not datagram:
    break
  else:
    print "-" * 20
    print datagram
    if "DONE" == datagram:
      break
print "-" * 20
print "Shutting down..."
server.close()
os.remove( "/tmp/python_unix_sockets_example" )
print "Done"

Listing: client.py

# -*- coding: utf-8 -*-
import socket
import os, os.path

print "Connecting..."
if os.path.exists( "/tmp/python_unix_sockets_example" ):
  client = socket.socket( socket.AF_UNIX, socket.SOCK_DGRAM )
  client.connect( "/tmp/python_unix_sockets_example" )
  print "Ready."
  print "Ctrl-C to quit."
  print "Sending 'DONE' shuts down the server and quits."
  while True:
    try:
      x = raw_input( "> " )
      if "" != x:
        print "SEND:", x
        client.send( x )
        if "DONE" == x:
          print "Shutting down."
          break
    except KeyboardInterrupt, k:
      print "Shutting down."
  client.close()
else:
  print "Couldn't Connect!"
print "Done"

Here is the transcript of me running the client.

[email protected]:~/Desktop$ python client.py
Connecting...
Ready.
Ctrl-C to quit.
Sending 'DONE' shuts down the server and quits.
> Helo!
SEND: Helo!
> DONE
SEND: DONE
Shutting down.
Done
[email protected]:~/Desktop$

And here is the server transcript from that session.

[email protected]:~/Desktop$ python server.py
Opening socket...
Listening...
--------------------
Helo!
--------------------
DONE
--------------------
Shutting down...
Done
[email protected]:~/Desktop$

Now all you need is a protocol and you’ll be set for basic IPC.

Hard subs with mencoder, or, annotating screencasts for free.

December 2, 2009 » Geek

I was working on a screen cast for a project and I could not get anything to work the way I wanted to. I didn’t have the desire to purchase software, so I sought a way to annotate it for free. I tried a half dozen free and open source video editors, with no real luck. Either they choked on the format I captured (from CamStudio) or they didn’t have a readily available inline text tool.

What I ended up using was plain old subtitles, plus the handyman’s secret weapon, mplayer/mencoder.

Here’s what I did. I fired up the video in VLC and found the points where I wanted to put my subtitle help text. Subtitle files are usually pretty straight forward. I chose the .srt format, which is plain text. You can edit by hand, or you can use a tool, I used the aptly named subtitleeditor. Which, by the way, barfed on my video file.

Here’s a snippet of the resulting .srt file:

1
00:00:00,000 --> 00:00:13,000
Welcome to the product gallery walk through.

2
00:00:13,000 --> 00:00:23,000
Adding a product: Select image.

3
00:00:23,000 --> 00:00:32,000
Insert title and description.

Next I needed to turn those “soft” subs into “hard” ones. This is where mplayer/mencoder come in. To add soft subs to a video in mplayer, you use the -sub option. Running this in mplayer first is a good way to see how it will look at tweak it according to the many options you have. I went with the defaults, it looks pretty good that way.

Lastly, I needed a good mencoder recipe to pull it all together. After lots of searching I found a great MPEG4 one here.

It’s a two pass system, and the crucial piece is in calculating the bitrate:

bitrate = 50 * 25 * width_of_video * height_of_video / 256

My bitrate was 2540600. After you get that, you just plug it into the two passes below. This includes my subtitle options.

mencoder -o /dev/null -ovc lavc -lavcopts vcodec=msmpeg4v2:vpass=1:vbitrate=2540600:mbd=2:keyint=132:vqblur=1.0:cmp=2:subcmp=2:dia=2:mv0:last_pred=3 -nosound -sub MySubTitles.srt -subfont-text-scale 3 RawScreenCast.avi
mencoder -o FinishedScreenCast.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vpass=2:vbitrate=2540600:mbd=2:keyint=132:vqblur=1.0:cmp=2:subcmp=2:dia=2:mv0:last_pred=3 -nosound -sub MySubTitles.srt -subfont-text-scale 3 RawScreenCast.avi

It takes a while but not too long. When all was said and done I had a perfect hard-subbed version and it shrank my file size from ~550MB to 7.6MB. That is a great encoding recipe.

Average video still.
An average frame, with hard subs.