Update "gif" conversion logic
This commit is contained in:
+91
-37
@@ -1,9 +1,22 @@
|
||||
import base64
|
||||
import os
|
||||
import subprocess
|
||||
import json
|
||||
import sys
|
||||
import tempfile
|
||||
import urllib.request
|
||||
import re
|
||||
import botocore
|
||||
import boto3
|
||||
import requests
|
||||
|
||||
useBucket=False
|
||||
bucketname=os.getenv('CF_BUCKET')
|
||||
s3=None
|
||||
if bucketname is None:
|
||||
useBucket=False
|
||||
else:
|
||||
useBucket=True
|
||||
s3 = boto3.client('s3',endpoint_url=os.getenv('CF_ENDPOINT'),aws_access_key_id=os.getenv('CF_KEY'),aws_secret_access_key=os.getenv('CF_KEY_SECRET'))
|
||||
|
||||
|
||||
def extractStatus(url):
|
||||
return ""
|
||||
@@ -11,7 +24,7 @@ def extractStatus(url):
|
||||
def get_video_frame_rate(filename):
|
||||
result = subprocess.run(
|
||||
[
|
||||
"./ffprobe",
|
||||
"ffprobe",
|
||||
"-v",
|
||||
"error",
|
||||
"-select_streams",
|
||||
@@ -32,7 +45,7 @@ def get_video_frame_rate(filename):
|
||||
def get_video_length_seconds(filename):
|
||||
result = subprocess.run(
|
||||
[
|
||||
"./ffprobe",
|
||||
"ffprobe",
|
||||
"-v",
|
||||
"error",
|
||||
"-show_entries",
|
||||
@@ -47,54 +60,95 @@ def get_video_length_seconds(filename):
|
||||
result_string = result.stdout.decode('utf-8').split()[0]
|
||||
return float(result_string)
|
||||
|
||||
def calcEdits(vlen,loopTimes):
|
||||
st="r"
|
||||
for i in range(loopTimes):
|
||||
st+=f'e{str((vlen*i))}-9999,0'
|
||||
return st
|
||||
|
||||
def loop_video_until_length(filename, length):
|
||||
# use stream_loop to loop video until it's at least length seconds long
|
||||
video_length = get_video_length_seconds(filename)
|
||||
if video_length < length:
|
||||
loops = int(length/video_length)
|
||||
new_filename = tempfile.mkstemp(suffix=".mp4")[1]
|
||||
#edits = calcEdits(video_length,loops)
|
||||
out = subprocess.call(["ffmpeg","-stream_loop",str(loops),"-i",filename,"-c","copy","-y",new_filename],stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
|
||||
#subprocess.run(["./MP4Box", "-add",filename,"-edits",f'1={edits}',new_filename])
|
||||
subprocess.call(["ffmpeg","-stream_loop",str(loops),"-i",filename,"-c","copy","-y",new_filename],stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
|
||||
|
||||
return new_filename
|
||||
else:
|
||||
return filename
|
||||
|
||||
|
||||
def redir(url):
|
||||
return {
|
||||
"statusCode": 307,
|
||||
"headers": {
|
||||
"Location": url
|
||||
}
|
||||
}
|
||||
|
||||
def lambda_handler(event, context):
|
||||
if ("queryStringParameters" not in event):
|
||||
return {
|
||||
"statusCode": 400,
|
||||
"body": "Invalid request."
|
||||
"body": "Invalid request!"
|
||||
}
|
||||
|
||||
url = event["queryStringParameters"].get("url","")
|
||||
|
||||
# download video
|
||||
videoLocation = tempfile.mkstemp(suffix=".mp4")[1]
|
||||
subprocess.call(["wget","-O",videoLocation,url],stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
|
||||
|
||||
videoLocationLooped = loop_video_until_length(videoLocation, 30)
|
||||
if videoLocationLooped != videoLocation:
|
||||
os.remove(videoLocation)
|
||||
videoLocation = videoLocationLooped
|
||||
|
||||
with open(videoLocation, "rb") as image_file:
|
||||
encoded_string = base64.b64encode(image_file.read()).decode('ascii')
|
||||
os.remove(videoLocation)
|
||||
return {
|
||||
'statusCode': 200,
|
||||
"headers":
|
||||
{
|
||||
"Content-Type": "video/mp4"
|
||||
},
|
||||
'body': encoded_string,
|
||||
'isBase64Encoded': True
|
||||
}
|
||||
try:
|
||||
if url == "":
|
||||
return {
|
||||
"statusCode": 400,
|
||||
"body": "Invalid request!!"
|
||||
}
|
||||
if not url.startswith("https://video.twimg.com/tweet_video/"):
|
||||
return redir(url)
|
||||
|
||||
if useBucket:
|
||||
id=re.search(r"https:\/\/video\.twimg\.com\/tweet_video\/(.*?)\..*",url).group(1)
|
||||
bfilename = str(id)+".mp4"
|
||||
furl=f"https://gifs.vxtwitter.com/{bfilename}" #f"https://{bucketname}.s3.amazonaws.com/{bfilename}"
|
||||
print("get req for: "+url)
|
||||
try:
|
||||
s3.head_object(Bucket=bucketname, Key=bfilename)
|
||||
print("found existing already: "+bfilename)
|
||||
return redir(furl)
|
||||
except botocore.exceptions.ClientError:
|
||||
# Not found
|
||||
pass
|
||||
# download video
|
||||
print("downloading: "+url)
|
||||
videoLocation = tempfile.mkstemp(suffix=".mp4")[1]
|
||||
response = requests.get(url, stream=True)
|
||||
if response.status_code == 200:
|
||||
with open(videoLocation, 'wb') as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
else:
|
||||
print("error downloading video")
|
||||
return redir(url)
|
||||
|
||||
videoLocationLooped = loop_video_until_length(videoLocation, 30)
|
||||
if videoLocationLooped != videoLocation:
|
||||
os.remove(videoLocation)
|
||||
videoLocation = videoLocationLooped
|
||||
else:
|
||||
os.remove(videoLocation)
|
||||
return redir(url)
|
||||
|
||||
if not useBucket:
|
||||
with open(videoLocation, "rb") as image_file:
|
||||
encoded_string = base64.b64encode(image_file.read()).decode('ascii')
|
||||
os.remove(videoLocation)
|
||||
return {
|
||||
'statusCode': 200,
|
||||
"headers":
|
||||
{
|
||||
"Content-Type": "video/mp4"
|
||||
},
|
||||
'body': encoded_string,
|
||||
'isBase64Encoded': True
|
||||
}
|
||||
else:
|
||||
with open(videoLocation, "rb") as image_file:
|
||||
s3.upload_fileobj(image_file, bucketname, bfilename)
|
||||
os.remove(videoLocation)
|
||||
print("converted: "+url+" -> "+furl)
|
||||
return redir(furl)
|
||||
except Exception as e:
|
||||
print("error converting gif: ")
|
||||
print(e)
|
||||
return redir(url)
|
||||
Reference in New Issue
Block a user