Scripting re-encode of Archiware P5 previews

Often times I see customers of Archiware P5 that are using the internal Quicktime preview generator in the hope that it will work well for their proxies. Unfortunately, when working in short form production video, the preview created by this system often times is actually the same size as the source file. The way Archiware’s preview engine works was discussed in my first article on Preview Generation way back in 2011.

That said, since it is the default to have preview generation on, sometimes P5 users start archiving and generate short previews that may want to be retained, but re-encoded into a more proxy friendly format.  Since previews are dynamically looked up by the P5 UI when the index is browsed, it was relatively simple to look at an existing index tree with large files and then encode those files into a smaller format.  This script does require ffmpeg to be installed on your system, but should work on Mac, Linux, and FreeBSD systems with python installed.

Download the script here.

Lets break down the script so you can see what it does and how it works.

First we import the libraries that are needed. os and sys are basic tools that allow us to get information about the system and filesystem. argparse is a library that lets us built easy options lists for command line tools. subprocess is the tool that allows us to run other command line tools inside a pythion script (in this case, ffmpeg).

Next up we have our only variable, simply asking where ffmpeg lives on the system.

Last in this block is the actual command line arguments that are possible for this script, which is basically where your existing p5 installation lives and where you want to put the newly encoded files (the script doesn’t automatically replace the files, it just builds the trees for manual replacement and safety)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env /usr/bin/python
 
import os
import sys
import argparse
import subprocess
 
#where is ffmpeg on your system
ffmpeg = "/usr/local/bin/ffmpeg"
 
parser = argparse.ArgumentParser(description='Get a list of all files archived by P5')
parser.add_argument('-r','--root',dest='p5root',metavar="PATH",type=str,nargs=1,help="Path of P5 root",default="None")
parser.add_argument('-d','--destination',dest='dest',metavar="PATH",type=str,nargs=1,help="Path of destination encode",required=True)
args = parser.parse_args()

Next thing we do are some checks. First is to see if we set a p5 root location. If not, we assume the default install location is where it is.
After that, we check to see if the destination is a valid location to put our files/folders. Last thing in this block is to make a new empty list called “indexes”

15
16
17
18
19
20
21
22
23
24
25
26
if args.p5root == "None":
	p5root = "/usr/local/aw"
else:
	p5root = args.p5root[0]
 
if not os.path.isdir(args.dest[0]):
	print args.dest[0] + " isn't a valid destination"
	exit()
else:
	dest = args.dest[0]
 
indexes=[]

Now we are in to the meat of the script. The first thing the script does is looks up all the indexes in the system and adds it to our list we just created.

28
29
30
31
#get all the indexes
for item in os.listdir(p5root + "/config/index/archive"):
	if os.path.isdir(p5root + "/config/index/archive/" + item):
		indexes.append(item)

Great! Now we know all of the indexes on this P5 system. Next thing we need to do is walk through all of the files/folders and make the folders at our destination and re-encode our files with the same filenames in those folders.

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#find all the indexes
for index in indexes:
	#find the proxy locations of the indexes
	for path, dirs, files in os.walk(p5root + "/config/index/archive/" + index + "/clips"):
		#create a destination path
		new_path = dest + path[len(p5root):]
		#loop through every file in the every index
		for f in files:
			#check to see if the exact same path exists in our destination
			if not os.path.isdir(new_path):
				os.makedirs(new_path)
			#make sure destination file has mp4 extension
			filename, file_extension = os.path.splitext(f)
			#encode the proxy
			cmd = [ffmpeg,'-y','-i',path + "/" + f,'-strict','-2','-pass','1','-passlogfile','/tmp/passlog','-vcodec','libx264','-pix_fmt','yuv420p','-vf','scale=192:-1','-b:a','16k','-b:v','64k',new_path + "/" + filename + ".mp4"]
			try:
				subprocess.Popen(cmd).wait()
			except:
				e = sys.exc_info()[0]
				print e

To run the script is relatively easy. For a default P5 install, simply create a directory where you’d like the destination structure to go and run it.

[szumlins@trashcan:~]: mkdir -p ~/Desktop/p5_encode_destination
[szumlins@trashcan:~]: ./p5-proxy-reencodey.py -d ~/Desktop/p5_encode_destination/

Depending on the number of files you have in the archive, this can take quite a while, so make sure your machine can stay up and running.

It is also important to note that the proxies this creates will be only as long as your default Preview duration (30 seconds or less).