Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys, os, pexpect, subprocess, re, glob, datetime, decimal
- import urllib2, paramiko, uuid, os, urllib
- from paramiko import AuthenticationException
- from django.core.management import setup_environ
- sys.path.insert(0, os.path.realpath(os.path.dirname(__file__)))
- import settings
- setup_environ(settings)
- from django.template import loader, Context
- from encoder.models import *
- def reso(w,h,nw):
- nh = (nw*h)/(w*1.0)
- if nh%8>=4: nh = (nh-nh%8)+8
- else: nh = (nh-nh%8)
- return (nw, nh)
- def sftp_upload(host_name,user_name,password,remote_path,local_path):
- #SFTP upload
- try:
- transport = paramiko.Transport((host_name, 22))
- print host_name
- print user_name
- print password
- transport.connect(username = user_name, password = password)
- sftp = paramiko.SFTPClient.from_transport(transport)
- sftp.put(local_path, remote_path)
- sftp.close()
- transport.close()
- except IOError:
- error_data = str(sys.exc_info()[1])
- sftp.close()
- transport.close()
- log_conversion_error("Upload Encode to SFTP <br />" + error_data)
- except AuthenticationException:
- error_data = str(sys.exc_info()[1])
- transport.close()
- log_conversion_error("Upload Encode to SFTP <br />" + error_data)
- except:
- error_data = str(sys.exc_info()[1])
- sftp.close()
- transport.close()
- print sys.exc_info()
- log_conversion_error("Upload Encode to SFTP <br />" + error_data)
- ffmpeg_status = subprocess.Popen("pgrep HandBrakeCLI".split(" "), stdout=subprocess.PIPE)
- ffmpeg_status.wait()
- #Make sure that ffmpeg isn't currently running
- if len(ffmpeg_status.stdout.readlines())==0:
- for encoding_request in EncodingRequest.objects.filter(status=0):
- print str(len(EncodingRequest.objects.filter(status=0))) + " jobs pending"
- encoding_profile = EncodingProfile.objects.get(name=encoding_request.request_profile)
- encoding_request.status=1
- encoding_request.save()
- resolution = [-1, -1]
- src_ext = os.path.splitext(encoding_request.source_location)[1]
- tmp_lavf_path = os.path.join("/tmp/", uuid.uuid1().hex + src_ext)
- tmp_src_path = os.path.join("/tmp/", uuid.uuid1().hex + src_ext)
- tmp_dst_path = os.path.join("/tmp/", uuid.uuid1().hex + ".m4v")
- #Download source file to temp directory
- print "Downloading File"
- null_return = urllib.urlretrieve(encoding_request.source_location, tmp_lavf_path)
- #lavf_conv = subprocess.Popen(str("ffmpeg -i %s -vcodec copy -acodec copy -acodec copy -f mp4 %s" % (tmp_lavf_path, tmp_src_path)).split(" "), stdin=subprocess.PIPE, stdout=subprocess.PIPE)
- #null_return = lavf_conv.wait()
- tmp_src_path = tmp_lavf_path
- print null_return
- comm_info = "mediainfo %s" % (tmp_src_path)
- child = pexpect.spawn (comm_info)
- raw_media_info = child.readlines()
- raw_media_info = [info_line.strip("\r\n") for info_line in raw_media_info]
- try:
- general_info = raw_media_info[0:raw_media_info.index("Video")]
- try:
- video_info = raw_media_info[raw_media_info.index("Video"):raw_media_info.index("Audio")]
- except:
- video_info = raw_media_info[raw_media_info.index("Video"):]
- for info_line in video_info:
- if info_line.split(":")[0].strip() == "Width":
- resolution[0] = int(re.search(r"([ \d]+) pixels", info_line, re.IGNORECASE).group(1).replace(" ",""))
- if info_line.split(":")[0].strip() == "Height":
- resolution[1] = int(re.search(r"([ \d]+) pixels", info_line, re.IGNORECASE).group(1).replace(" ",""))
- out_height = str(reso(resolution[0], resolution[1], 640)[1])
- except:
- out_height = 0
- profile_context = {"source_file": tmp_src_path, "height": out_height, "output_file": tmp_dst_path, "width":encoding_profile.width}
- #comm_convert = "HandBrakeCLI -i %(source_file)s -o %(output_file)s -e x264 -b 400 -a 1 -E faac -B 112 -R 48 -6 dpl2 -f mp4 -X %(width)s -m -x level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1 -2" % profile_context
- comm_convert = encoding_profile.comm_text % profile_context
- print comm_convert
- re_resultfilter = re.compile(r"task (?P<pass>\d) of (?P<passes>\d), (?P<progress>\d+\.\d+) %", re.IGNORECASE)
- print "Converting File"
- child = pexpect.spawn (comm_convert)
- read_chunk = ""
- fps = 0
- progress = ""
- while not child.eof():
- read_chunk += child.read(100)
- match_set = [match for match in re_resultfilter.finditer(read_chunk)]
- if len(match_set)>0:
- last_match = match_set[-1]
- m_pass = float(match_set[-1].group("pass"))
- m_passes = float(match_set[-1].group("passes"))
- m_progress = float(match_set[-1].group("progress"))
- val_progress = str(round(((m_pass-1)/m_passes*100)+(m_progress/m_passes),4))
- print val_progress
- EncodingProgress.objects.create(encoding_request=encoding_request, progress=decimal.Decimal(val_progress))
- #sftp_upload(host_name,user_name,password,remote_path,local_path)
- sftp_data = {}
- sftp_data["host_name"] = encoding_request.destination_location.split("/")[2].split("@")[1]
- sftp_data["user_name"] = encoding_request.destination_location.split("/")[2].split("@")[0].split(":")[0]
- sftp_data["password"] = encoding_request.destination_location.split("/")[2].split("@")[0].split(":")[1]
- sftp_data["remote_path"] = "/" + "/".join(encoding_request.destination_location.split("/")[3:])
- sftp_data["local_path"] = profile_context["output_file"]
- sftp_upload(**sftp_data)
- notification_xml = loader.get_template("notify.xml").render(Context({"media_id":encoding_request.media_id, "task_id":465465, "destination_location":encoding_request.destination_location,"source_location":encoding_request.source_location}))
- try:
- null_return = urllib2.urlopen(urllib2.Request(encoding_request.notifier_uri, urllib.urlencode({"xml":notification_xml}))).read()
- encoding_request.status=2
- except:
- encoding_request.status=3
- pass
- encoding_request.save()
- os.remove(profile_context["source_file"])
- os.remove(profile_context["output_file"])
- os.remove(tmp_lavf_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement