Last active
August 29, 2015 14:00
-
-
Save britalmeida/11189796 to your computer and use it in GitHub Desktop.
Scripts for operations on the blender-cloud database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
from flask.ext.script import Manager | |
from sqlalchemy import update | |
import os | |
import subprocess | |
import json | |
from server import app | |
from server import db | |
from server.model import Asset | |
from server.model import AssetType | |
manager = Manager(app) | |
@manager.command | |
def linkstrip(): | |
print "stripping file extensions from asset links" | |
#Asset.query.update(dict( link= os.path.splitext(Asset.link)[0] )) | |
for asset in Asset.query.all(): | |
asset.link = os.path.splitext(asset.link)[0] | |
db.session.commit() | |
print "done" | |
@manager.command | |
def filldata(): | |
print "filling asset data" | |
main_folder = "/home/stitch/" # <--------------- change this! | |
#print main_folder+ Asset.query.first().direct_link # test if this is correct | |
# get size from file in human readable form | |
for asset in Asset.query.all(): | |
for file_version in asset.versions: | |
# get size in bytes | |
file_version.size = int(os.stat(main_folder+file_version.direct_link).st_size) | |
db.session.commit() | |
# get video duration and resolution | |
for asset in Asset.query.filter_by(asset_type_id=3).all(): | |
for file_version in asset.video_versions: | |
ffprobe_ouput = json.loads(subprocess.check_output(['ffprobe', '-loglevel', 'error', '-show_streams', main_folder+file_version.direct_link, '-print_format', 'json'])) | |
video_info = ffprobe_ouput['streams'][0] | |
file_version.duration = int(float(video_info['duration'])) | |
file_version.res_x = video_info['width'] | |
file_version.res_y = video_info['height'] | |
if video_info['sample_aspect_ratio'] != '1:1': | |
print "warning: Pixel aspect ration is not square!" | |
db.session.commit() | |
@manager.command | |
def djson(): | |
data = [] | |
for asset in Asset.query.filter_by(asset_type_id=3).all(): | |
for file_version in asset.versions: | |
data.append(dict(id=file_version.id, link=asset.link)) | |
with open('videolinks.json', 'w') as outfile: | |
json.dump(data, outfile) | |
@manager.command | |
def getvideodata(): | |
json_data=open('videolinks.json') | |
data = json.load(json_data) | |
main_folder = "/home/stitch/" # <--------------- change this! | |
#print main_folder+ data[0]['link'] # test if this is correct | |
outdata = dict() | |
for video in data: | |
ffprobe_ouput = json.loads(subprocess.check_output(['ffprobe', '-loglevel', 'error', '-show_streams', main_folder+video['link']+'.mp4', '-print_format', 'json'])) | |
for stream in ffprobe_ouput['streams']: | |
if stream['codec_type'] == 'video': | |
outdata[video['id']] = dict( | |
duration = int(float(video_info['duration'])), | |
res_x = video_info['width'], | |
res_y = video_info['height'] | |
) | |
if video_info['sample_aspect_ratio'] != '1:1': | |
print video['link'] | |
print " warning: Pixel aspect ratio is not square!" | |
json_data.close() | |
with open('videodata.json', 'w') as outfile: | |
json.dump(outdata, outfile) | |
@manager.command | |
def fillvideo(): | |
json_data=open('videodata.json') | |
data = json.load(json_data) | |
for asset in Asset.query.filter_by(asset_type_id=3).all(): | |
for file_version in asset.video_versions: | |
video_info = data['%d'%file_version.id] | |
file_version.duration = int(float(video_info['duration'])) | |
file_version.res_x = video_info['res_x'] | |
file_version.res_y = video_info['res_y'] | |
db.session.commit() | |
json_data.close() | |
@manager.command | |
def djson2(): | |
data = [] | |
for asset in Asset.query.all(): | |
for file_version in asset.versions: | |
data.append(dict(id=asset.id, link=asset.link, filetype=file_version.filetype)) | |
with open('assetlinks.json', 'w') as outfile: | |
json.dump(data, outfile) | |
@manager.command | |
def getsizedata(): | |
json_data=open('assetlinks.json') | |
data = json.load(json_data) | |
main_folder = "/home/stitch/" # <--------------- change this! | |
#print main_folder+ data[0]['link'] # test if this is correct | |
outdata = dict() | |
for asset in data: | |
outdata[asset['id']] = dict( | |
size = int(os.stat(main_folder+asset['link']+'.'+asset['filetype']).st_size), | |
) | |
json_data.close() | |
with open('sizedata.json', 'w') as outfile: | |
json.dump(outdata, outfile) | |
@manager.command | |
def fillsize(): | |
json_data=open('sizedata.json') | |
data = json.load(json_data) | |
for asset in Asset.query.all(): | |
for file_version in asset.versions: | |
file_version.size = data['%d'%asset.id]['size'] | |
db.session.commit() | |
json_data.close() | |
if __name__ == "__main__": | |
manager.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment