#!/usr/bin/env python
from pymongo import MongoClient
#from subprocess import Popen, PIPE
import FindVid as fv
from sys import argv, exit
import hashlib
import os

def hashFile(filename, blocksize):
	hash = hashlib.sha1()
	with open(filename, 'rb') as f:
		buffer = f.read(blocksize)
		while len(buffer) > 0:
			hash.update(buffer)
			buffer = f.read(blocksize)
	
	return hash.hexdigest()

# returns the configuration dictionary
def config(db="findvid", collection="videos", config={"_id": "config"}):
	client = MongoClient()
	db = client[db]
	videos = db[collection]
	return videos.find(config).next()

CONFIG = config() # abs, thumbnail, video
VIDEOPATH = CONFIG["abspath"] + CONFIG["videopath"]

# path to shotbounds program
SHOTBOUNDS = "{0}main/impl/shotbounds".format(CONFIG["abspath"])
THUMBNAILER = "{0}main/impl/thumbnails".format(CONFIG["abspath"])

#Index the given videofile (abs. path), create thumbnails in the 
def index_video(videofile, searchable=False, uploaded=True, thumbpath = None):
	#Get PyMongo client
	client = MongoClient()
	db = client["findvid"]
	videos = db["videos"]
	

	#Get Hash
	fileHash = str(hashFile(videofile, 65536))

	#Check if this exact video exists already
	video = videos.find_one({'_id': fileHash})
	if (video):
		return False

	#Use C-Lib to get cuts in the video
	cuts = fv.getCuts(videofile)

	#Heuristic approach: Suitable keyframe between 2 cuts
	keyframes = [(cuts[i-1] + cuts[i])/2 for i in range(1, len(cuts))]

	#extract features from videofile given the keyframes array, use the middle keyframe as videothumb and save to default folder
	features = fv.getFeatures(videofile, keyframes[len(keyframes)/2], keyframes, thumbpath)

	prev = 0
	scenes = [] # scenes collection
	for i, c in enumerate(cuts[1:]):
		scene = {} # scene document
		scene["_id"] = str(i)
		scene["startframe"] = prev
		scene["endframe"] = c
		# save features
		scene["colorhist"] = []
		for v in features[i][0]:
			scene["colorhist"].append(v)
		scene["edges"] = []
		for v in features[i][1]:
			scene["edges"].append(v)
		# TinyIMG
		# scene["tinyimg"]
		# for v in features[i][2]:
		# 	scene["tinyimg"].append(v)
		# GIST
		# scene["gist"]
		# for v in features[i][2]:
		# 	scene["gist"].append(v)
		scenes.append(scene)
		prev = c
	video = {}
	# TODO sequence counter
	video["_id"] = fileHash
	video["filename"] = videofile
	fps = fv.getFramerate(videofile)
	video["fps"] = fps
	video["framecount"] = cuts[-1:][0] # last entry
	video["scenes"] = scenes
	video["upload"] = uploaded
	video["searchable"] = searchable
	videos.insert(video)

	return True

if __name__ == "__main__":
	if len(argv) < 2:
		print "ERROR: file missing!"
		exit(1)
	videofile = argv[1]
	index_video(videofile)

#!/usr/bin/env python
from pymongo import MongoClient
#from subprocess import Popen, PIPE
import FindVid as fv
from sys import argv, exit
import hashlib
import os

def hashFile(filename, blocksize):
	hash = hashlib.sha1()
	with open(filename, 'rb') as f:
		buffer = f.read(blocksize)
		while len(buffer) > 0:
			hash.update(buffer)
			buffer = f.read(blocksize)
	
	return hash.hexdigest()

# returns the configuration dictionary
def config(db="findvid", collection="videos", config={"_id": "config"}):
	client = MongoClient()
	db = client[db]
	videos = db[collection]
	return videos.find(config).next()

CONFIG = config() # abs, thumbnail, video
VIDEOPATH = CONFIG["abspath"] + CONFIG["videopath"]

# path to shotbounds program
SHOTBOUNDS = "{0}main/impl/shotbounds".format(CONFIG["abspath"])
THUMBNAILER = "{0}main/impl/thumbnails".format(CONFIG["abspath"])

#Index the given videofile (abs. path), create thumbnails in the 
def index_video(videofile, searchable=False, uploaded=True, thumbpath = None):
	#Get PyMongo client
	client = MongoClient()
	db = client["findvid"]
	videos = db["videos"]
	

	#Get Hash
	fileHash = str(hashFile(videofile, 65536))

	#Check if this exact video exists already
	video = videos.find_one({'_id': fileHash})
	if (video):
		return False

	#Use C-Lib to get cuts in the video
	cuts = fv.getCuts(videofile)

	#Heuristic approach: Suitable keyframe between 2 cuts
	keyframes = [(cuts[i-1] + cuts[i])/2 for i in range(1, len(cuts))]

	#extract features from videofile given the keyframes array, use the middle keyframe as videothumb and save to default folder
	if (thumbpath == None):
		features = fv.getFeatures(videofile, keyframes[len(keyframes)/2], keyframes)
	else:
		features = fv.getFeatures(videofile, keyframes[len(keyframes)/2], keyframes, thumbpath)

	prev = 0
	scenes = [] # scenes collection
	for i, c in enumerate(cuts[1:]):
		scene = {} # scene document
		scene["_id"] = str(i)
		scene["startframe"] = prev
		scene["endframe"] = c
		# save features
		scene["colorhist"] = []
		for v in features[i][0]:
			scene["colorhist"].append(v)
		scene["edges"] = []
		for v in features[i][1]:
			scene["edges"].append(v)
		# TinyIMG
		# scene["tinyimg"]
		# for v in features[i][2]:
		# 	scene["tinyimg"].append(v)
		# GIST
		# scene["gist"]
		# for v in features[i][2]:
		# 	scene["gist"].append(v)
		scenes.append(scene)
		prev = c
	video = {}
	# TODO sequence counter
	video["_id"] = fileHash
	video["filename"] = videofile
	fps = fv.getFramerate(videofile)
	video["fps"] = fps
	video["framecount"] = cuts[-1:][0] # last entry
	video["scenes"] = scenes
	video["upload"] = uploaded
	video["searchable"] = searchable
	videos.insert(video)

	return True

if __name__ == "__main__":
	if len(argv) < 2:
		print "ERROR: file missing!"
		exit(1)
	videofile = argv[1]
	index_video(videofile)

Fix arg list for thumbnailpath override