Openshot speed improvement: Use tmpfs

When editing the Braunlage Singletrail Video, I once again ran into openshot becoming slower and slower, just like I had discovered when I first looked at it. So I had the idea to temporarily store the files necessary to put the video together into a tmpfs. Here’s how I did that.

The idea was to copy the original video files into the computer’s memory, which is waaay faster than your harddisk and thereby reduce loading and waiting times.

Use the following script at your own risk, but know I used this a couple of times now (on Ubuntu 12.04 btw). It has a couple of sanity checks to not screw your system, but who knows what could happen ;) hth

#!/bin/bash

# where to mount the tmpfs
tmpfsmountpoint=/tmp/ramdisk

usage() {
  echo "To add files to the tmpfs" 
  echo "Usage: $0 [tmpfs size in bytes] videofile1 videofile2"
  echo "To unmount the tmpfs"
  echo "Usage: $0 cleanup"
  exit 1
}

mounted() {
  grep -qw "$tmpfsmountpoint" /proc/mounts
}

if [ $# -le 0 ]; then
  usage
else
  if [ $# -eq 1 ]; then
    if echo $1|grep -qE "^[0-9]+"; then
      # the user has only given a tmpfs size, but no video files
      usage
    fi
    if [ $1 = "cleanup" ]; then
      # the user wants to cleanup the previously created tmpfs"
      echo "Trying to unmount $tmpfsmountpoint with sudo."
      sudo umount $tmpfsmountpoint
      exit $?
    fi
  fi
fi

if mounted; then
  # df is not able to print bytes, so we have to use kb*1024
  tmpfsfree=$(echo $(df -Bk|grep $tmpfsmountpoint | awk '{ print $4; }' | \
  grep -Eo "^[0-9]+")*1024|bc)
  tmpfssize=$(echo $(df -Bk|grep $tmpfsmountpoint | awk '{ print $2; }' | \
  grep -Eo "^[0-9]+")*1024|bc)
else
  if echo $1|grep -qE "^[0-9]+"; then
    # the user gave a tmpfs size as $1
    tmpfssize=$1
    shift
    echo "Will use $tmpfssize bytes of memory for tmpfs since you said so."
  else
    # the user didn't tell how much ram to use, so use 70% of free memory
    freemem=$(free -b | grep ^Mem | awk '{ print $4; }')
    tmpfssize=$(echo "${freemem}*0.7" | bc | grep -Eo "^[0-9]+")
    echo "Will use $tmpfssize bytes of memory for tmpfs."
    echo "That is 70% of your currently free memory."
  fi
fi

vidfilesize=$(du -bc $*|tail -n 1|cut -f 1)

if [ $vidfilesize -gt $tmpfssize ]; then
  echo "Files are larger ($vidfilesize) than your tmpfs ($tmpfssize)."
  echo "This won't work."
  echo "No changes made yet. Exit."
  exit 1
fi

if [ -n "$tmpfsfree" ]; then
  if [ $vidfilesize -gt $tmpfsfree ]; then
    echo "Files are larger than what's free on your tmpfs."
    echo "Video files' size: $vidfilesize"
    echo "Free space tmpfs : $tmpfsfree"
    echo "This won't work."
    echo "No changes made yet. Exit."
    exit 1
  fi
fi

if mounted; then
  echo "Adding $* to existing tmpfs"
else
  mkdir $tmpfsmountpoint &>/dev/null
  echo "Trying to create a tmpfs with sudo."
  sudo mount -t tmpfs -o size=$tmpfssize none $tmpfsmountpoint
fi
if ! mounted; then
  echo "Mounting the tmpfs failed. Exit."
  exit 1
fi
cp -v $* $tmpfsmountpoint

if which openshot &>/dev/null; then
  openshot $tmpfsmountpoint/* &>/dev/null &
else
  echo "You forgot to install openshot"
fi

After editing and saving the project file, if you ever happen to want to edit that project again, you can either put the source video files into a tmpfs again or just correct paths in the .osp file. Should be as easy as

sed 's/\/tmp\/ramdisk/$where_the_videos_actually_are/g' \
$project.osp > $project_no_tmpfs.osp

I have not yet benchmarked the results of source files being and not being in a tmpfs, but it sure feels a LOT faster to me.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s