def makeDerivatives(task):
	task_tag = "DERIVATIVES: VIDEO"
	print "\n\n************** %s [START] ******************\n" % task_tag
	print "image preprocessing at %s" % task.doc_id
	task.setStatus(302)
		
	from lib.Worker.Models.ic_video import InformaCamVideo
	
	from conf import DEBUG
	from vars import ASSET_TAGS
	
	video = InformaCamVideo(_id=task.doc_id)
	if video is None:
		print "DOC IS NONE"
		print "\n\n************** %s [ERROR] ******************\n" % task_tag
		task.fail()
		return
	
	import os
	from fabric.api import settings, local
	from fabric.context_managers import hide

	from conf import ANNEX_DIR
	
	resolutions = {
		'high' : None,
		'med' : [0.75, 0.75],
		'low' : [0.5, 0.5]
	}
	
	video.getFile(video.file_name)
	for label, res in resolutions.iteritems():
		asset_path = video.addAsset(None, "%s_%s.mp4" % (label, video.file_name),
			tags=[ASSET_TAGS['M_DERIV'], ASSET_TAGS[label.upper()]],
			description="derivative of video in %s resolution (mp4)" % label)
			
		if asset_path is None:
			print "COULD NOT INIT THIS ASSET"
			print "\n\n************** %s [ERROR] ******************\n" % task_tag
			task.fail()
			return
			
		if res is not None:
			cmd = ["ffmpeg", "-y", "-i", os.path.join(ANNEX_DIR, video.file_name),
				"-filter:v", "scale=%d:%d" % (res[0], res[1]),
				"-acodec", "copy", os.path.join(ANNEX_DIR, asset_path)]
		else:
			cmd = ["cp", os.path.join(ANNEX_DIR, video.file_name), os.path.join(ANNEX_DIR, asset_path)]
		
		with settings(hide('everything'), warn_only=True):
			ffmpeg = local(" ".join(cmd))

		if DEBUG:
			print "FFMPEG CMD: %s" % (" ".join(cmd))
			print "failed: %s, return_code: %d" % (ffmpeg.failed, ffmpeg.return_code)

		if ffmpeg.failed or ffmpeg.return_code == 1:
			continue
		
		video.addFile(asset_path, None)
		
		ogv_asset_path = video.addAsset(None, "%s_%s.ogv" % (label, video.file_name),
			tags=[ASSET_TAGS['M_DERIV'], ASSET_TAGS[label.upper()]],
			description="derivative of video in %s resolution (ogv)" % label)
		
		if ogv_asset_path is not None:
			with settings(hide('everything'), warn_only=True):
				ffmpeg2theora = local("ffmpeg2theora %s" % os.path.join(ANNEX_DIR, asset_path))

			if ffmpeg2theora.failed or ffmpeg2theora.return_code == 1:
				continue
			
			video.addFile(ogv_asset_path, None)
	
	asset_path = video.addAsset(None, "thumb_%s.jpg" % video.file_name,
		tags=[ASSET_TAGS['M_DERIV'], ASSET_TAGS['THUMB']],
		description="derivative of video in thumb resolution")
	
	if asset_path is not None:
		cmd = ["ffmpeg", "-y", "-i", os.path.join(ANNEX_DIR, video.file_name),
			"-f", "image2", "-ss", "0.342", "-vframes", "1", os.path.join(ANNEX_DIR, asset_path)]
	
		with settings(hide('everything'), warn_only=True):
			thumb = local(" ".join(cmd))

		if DEBUG: 
			print "FFMPEG CMD: %s" % (" ".join(cmd))
			print "failed: %s, return_code: %d" % (thumb.failed, thumb.return_code)
	
	video.addCompletedTask(task.task_path)

	task.routeNext()
	task.finish()
	print "\n\n************** %s [END] ******************\n" % task_tag
def preprocessVideo(uv_task):
	task_tag = "PREPROCESSING VIDEO"
	print "\n\n************** %s [START] ******************\n" % task_tag
	print "image preprocessing at %s" % uv_task.doc_id
	uv_task.setStatus(302)
		
	from lib.Worker.Models.ic_video import InformaCamVideo
	
	from conf import DEBUG
	from vars import ASSET_TAGS
	
	video = InformaCamVideo(_id=uv_task.doc_id)
	if video is None:
		print "DOC IS NONE"
		print "\n\n************** %s [ERROR] ******************\n" % task_tag
		uv_task.fail()
		return
		
	asset_path = video.addAsset(None, "j3m_raw.txt")
	
	if asset_path is None:
		print "COULD NOT MAKE ASSET"
		print "\n\n************** %s [ERROR] ******************\n" % task_tag
		uv_task.fail()
		return
	
	was_encrypted = False
	obscura_marker_found = False
	
	import os
	from fabric.api import settings, local
	
	from lib.Core.Utils.funcs import b64decode
	from conf import ANNEX_DIR
	
	j3m_attachment = os.path.join(ANNEX_DIR, asset_path)
	cmd = ["ffmpeg", "-y", "-dump_attachment:t", j3m_attachment, "-i",
		os.path.join(ANNEX_DIR, video.file_name)]

	with settings(warn_only=True):
		ffmpeg = local(" ".join(cmd))

	from vars import UPLOAD_RESTRICTION

	if os.path.exists(j3m_attachment):
		uv_task.put_next("J3M.locate_j3m.locate_j3m")

		try:
			upload_restriction = video.getFileMetadata('uv_restriction')
		except Exception as e:
			print "could not get metadata for uv_restriction"
			print e
		
	else:
		print "NO IC J3M TEXT FOUND???"
		print "\n\n************** %s [WARN] ******************\n" % task_tag
		upload_restriction = UPLOAD_RESTRICTION['for_local_use_only']
	
	if upload_restriction is None or upload_restriction == UPLOAD_RESTRICTION['no_restriction']:
		uv_task.put_next("Video.make_derivatives.makeDerivatives")
	
	video.addCompletedTask(uv_task.task_path)	
	
	uv_task.routeNext()
	uv_task.finish()
	print "\n\n************** %s [END] ******************\n" % task_tag
Beispiel #3
0
def preprocessVideo(uv_task):
    task_tag = "PREPROCESSING VIDEO"
    print "\n\n************** %s [START] ******************\n" % task_tag
    print "image preprocessing at %s" % uv_task.doc_id
    uv_task.setStatus(302)

    from lib.Worker.Models.ic_video import InformaCamVideo

    from conf import DEBUG
    from vars import ASSET_TAGS

    video = InformaCamVideo(_id=uv_task.doc_id)
    if video is None:
        print "DOC IS NONE"
        print "\n\n************** %s [ERROR] ******************\n" % task_tag
        uv_task.fail()
        return

    asset_path = video.addAsset(None, "j3m_raw.txt")

    if asset_path is None:
        print "COULD NOT MAKE ASSET"
        print "\n\n************** %s [ERROR] ******************\n" % task_tag
        uv_task.fail()
        return

    was_encrypted = False
    obscura_marker_found = False

    import os
    from fabric.api import settings, local

    from lib.Core.Utils.funcs import b64decode
    from conf import ANNEX_DIR

    j3m_attachment = os.path.join(ANNEX_DIR, asset_path)
    cmd = ["ffmpeg", "-y", "-dump_attachment:t", j3m_attachment, "-i", os.path.join(ANNEX_DIR, video.file_name)]

    with settings(warn_only=True):
        ffmpeg = local(" ".join(cmd))

    if not os.path.exists(j3m_attachment):
        print "FFMPEG COULD NOT DO THE THING"
        print "\n\n************** %s [ERROR] ******************\n" % task_tag
        uv_task.fail()
        return

    from lib.Worker.Utils.funcs import getFileType
    from vars import MIME_TYPES, MIME_TYPE_MAP

    next_tasks = []
    inflate = {}

    j3m_content = video.loadAsset("j3m_raw.txt")
    print j3m_content

    j3m_content_mime_type = getFileType(j3m_content, as_buffer=True)

    if j3m_content_mime_type not in [MIME_TYPES["pgp"], MIME_TYPES["gzip"]]:
        j3m_content = b64decode(j3m_content)
        if j3m_content is not None:
            j3m_content_mime_type = getFileType(j3m_content, as_buffer=True)

    if j3m_content_mime_type in [MIME_TYPES["pgp"], MIME_TYPES["gzip"]]:
        asset_path = "j3m_raw.%s" % MIME_TYPE_MAP[j3m_content_mime_type]
        video.addAsset(un_b64, asset_path)

        if j3m_content_mime_type == MIME_TYPES["pgp"]:
            next_tasks.append("PGP.request_decrypt.requestDecrypt")
            inflate["pgp_file"] = asset_path
        elif j3m_content_mime_type == MIME_TYPES["gzip"]:
            next_tasks.append("J3M.j3mify.j3mify")
            video.addAsset(
                None, "j3m_raw.gz", tags=[ASSET_TAGS["OB_M"]], description="j3m data extracted from mkv stream"
            )

        video.addCompletedTask(task.task_path)

    from vars import UPLOAD_RESTRICTION

    try:
        upload_restriction = video.getFileMetadata("uv_restriction")
    except Exception as e:
        print "could not get metadata for uv_restriction"
        print e

    if upload_restriction is None or upload_restriction == UPLOAD_RESTRICTION["no_restriction"]:
        next_tasks.append("Video.make_derivatives.makeDerivatives")

    if len(next_tasks) > 0:
        uv_task.put_next(next_tasks)
        uv_task.routeNext(inflate=inflate)

    uv_task.finish()
    print "\n\n************** %s [END] ******************\n" % task_tag