“函数计算”的中间临时文件如何存放 无服务 架构
中间 临时文件的存放
import os, oss2, time
def handerOKMP3(event=None, context=None):
kid, ks, ep, bn = 'a', 'a', 'oss-cn-beijing.aliyuncs.com', 'mybn'
auth = oss2.Auth(kid, ks)
bucket = oss2.Bucket(auth, ep, bn)
img_l = []
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068339991800.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500066330.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500095850.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244112068340068340.jpg')
dl_img_l = []
str_0, c_ = ''.join(['0' for i in range(4)]), 1
for i in img_l:
break
oss_path = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
f_extension = '.jpg'
s_ = '{}{}'.format(str_0, c_)
s_ = s_[len(s_) - 4:]
nn = '{}{}{}'.format('/tmp/image', s_, f_extension)
c_ += 1
try:
bucket.get_object_to_file(oss_path, nn)
dl_img_l.append(nn)
except Exception as e:
print(e)
pass
# die
break
print('----'.join(dl_img_l))
mp3_l = []
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_10.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_7.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_8.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_9.mp3')
dl_mp3_l = []
str_0, c_ = ''.join(['0' for i in range(4)]), 1
for i in mp3_l:
print(i)
oss_pth = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
print(oss_pth)
f_extension = '.mp3'
s_ = '{}{}'.format(str_0, c_)
s_ = s_[len(s_) - 4:]
nn = '{}{}{}'.format('/tmp/mp3', s_, f_extension)
# nn = '{}{}{}'.format('./tmp/mp3', s_, f_extension)
c_ += 1
try:
print(nn)
bucket.get_object_to_file(oss_pth, nn)
dl_mp3_l.append(nn)
except Exception as e:
print(e)
pass
path = os.listdir('/tmp')
for p in path:
print(p)
if os.path.isdir(p): # 判断是否为文件夹,如果是输出所有文件就改成: os.path.isfile(p)
print(p)
print('----'.join(dl_mp3_l))
res_mp3 = '/tmp/res{}.mp3'.format(int(time.time()))
# res_mp3 = './tmp/res{}.mp3'.format(int(time.time()))
d = '{}{}{}{}'.format('./ffmpeg-linux64-v3.3.1 -i "concat:', '|'.join(dl_mp3_l), '" -acodec copy ',
res_mp3)
print(d)
os.system(d)
s = '{}{}'.format('testtmp/', res_mp3)
with open(res_mp3, 'rb') as fr:
res = bucket.put_object(s, fr.read())
print(res.resp.response.url)
def hander(event=None, context=None):
kid, ks, ep, bn = 'a', 'a', 'oss-cn-beijing.aliyuncs.com', 'mybn'
auth = oss2.Auth(kid, ks)
bucket = oss2.Bucket(auth, ep, bn)
img_l = []
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068339991800.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500066330.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500095850.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244112068340068340.jpg')
dl_img_l = []
str_0, c_ = ''.join(['0' for i in range(4)]), 1
for i in img_l:
# break
oss_path = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
f_extension = '.jpg'
s_ = '{}{}'.format(str_0, c_)
s_ = s_[len(s_) - 4:]
nn = '{}{}{}'.format('/tmp/image', s_, f_extension)
c_ += 1
try:
bucket.get_object_to_file(oss_path, nn)
dl_img_l.append(nn)
except Exception as e:
print(e)
pass
# die
# break
print('----'.join(dl_img_l))
mp3_l = []
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_10.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_7.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_8.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_9.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_11.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_12.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_13.mp3')
dl_mp3_l = []
str_0, c_ = ''.join(['0' for i in range(4)]), 1
for i in mp3_l:
# print(i)
oss_pth = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
# print(oss_pth)
f_extension = '.mp3'
s_ = '{}{}'.format(str_0, c_)
s_ = s_[len(s_) - 4:]
nn = '{}{}{}'.format('/tmp/mp3', s_, f_extension)
# nn = '{}{}{}'.format('./tmp/mp3', s_, f_extension)
c_ += 1
try:
# print(nn)
bucket.get_object_to_file(oss_pth, nn)
dl_mp3_l.append(nn)
except Exception as e:
print(e)
pass
# path = os.listdir('/tmp')
# for p in path:
# print(p)
# if os.path.isdir(p): # 判断是否为文件夹,如果是输出所有文件就改成: os.path.isfile(p)
# print(p)
# print('----'.join(dl_mp3_l))
res_mp3 = '/tmp/res{}.mp3'.format(int(time.time()))
# res_mp3 = './tmp/res{}.mp3'.format(int(time.time()))
d = '{}{}{}{}'.format('./ffmpeg-linux64-v3.3.1 -i "concat:', '|'.join(dl_mp3_l), '" -acodec copy ',
res_mp3)
# print(d)
# os.system(d)
# d = './ffmpeg-linux64-v3.3.1 {}'.format(res_mp3)
d = './ffmpeg-linux64-v3.3.1 -i {}'.format(dl_mp3_l[0])
print(d)
# os.system(d)
p = os.popen(d)
x = p.read()
# Duration: 00:00:12.17, start: 0.000000, bitrate: 16 kb/s
xx = x.split('Duration: ')[-1].split(', start')[0]
print(xx)
print(x)
res_mp4 = '/tmp/mymp4{}.mp4'.format(int(time.time()))
# -r 10 fps设置为10帧/秒 ok图片切换
# s = './ffmpeg-linux64-v3.3.1 -y -r 5 -loop 1 -i /tmp/image%04d.jpg -i {} -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 {}'.format(
# res_mp3, res_mp4)
# s = './ffmpeg-linux64-v3.3.1 -y -r 5 -loop 1 -i /tmp/image%04d.jpg -i {} -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 {}'.format(
# res_mp3, res_mp4)
s = './ffmpeg-linux64-v3.3.1 -y -r 1 -loop 1 -i /tmp/image%04d.jpg -i {} -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 {}'.format(
res_mp3, res_mp4)
# s = './ffmpeg-linux64-v3.3.1 -y -r 5 -loop 1 -i /tmp/image%04d.jpg -i {} {}'.format(
# res_mp3, res_mp4)
# os.system(s)
#
# bn = '{}{}'.format('testtmp/', res_mp4.split('/')[-1])
# with open(res_mp4, 'rb') as fr:
# res = bucket.put_object(bn, fr.read())
# print(res.resp.response.url)
# return s, res.resp.response.url
def handerT1(event=None, context=None):
kid, ks, ep, bn = 'a', 'a', 'oss-cn-beijing.aliyuncs.com', 'mybn'
auth = oss2.Auth(kid, ks)
bucket = oss2.Bucket(auth, ep, bn)
img_l = []
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068339991800.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500066330.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500095850.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244112068340068340.jpg')
dl_img_l = []
str_0, c_ = ''.join(['0' for i in range(4)]), 1
for i in img_l:
break
oss_path = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
f_extension = '.jpg'
s_ = '{}{}'.format(str_0, c_)
s_ = s_[len(s_) - 4:]
nn = '{}{}{}'.format('/tmp/image', s_, f_extension)
c_ += 1
try:
bucket.get_object_to_file(oss_path, nn)
dl_img_l.append(nn)
except Exception as e:
print(e)
pass
# die
break
print('----'.join(dl_img_l))
mp3_l = []
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_10.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_7.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_8.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_9.mp3')
dl_mp3_l = []
str_0, c_ = ''.join(['0' for i in range(4)]), 1
for i in mp3_l:
print(i)
oss_pth = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
print(oss_pth)
f_extension = '.mp3'
s_ = '{}{}'.format(str_0, c_)
s_ = s_[len(s_) - 4:]
nn = '{}{}{}'.format('/tmp/mp3', s_, f_extension)
# nn = '{}{}{}'.format('./tmp/mp3', s_, f_extension)
c_ += 1
try:
print(nn)
bucket.get_object_to_file(oss_pth, nn)
dl_mp3_l.append(nn)
except Exception as e:
print(e)
pass
path = os.listdir('/tmp')
for p in path:
print(p)
if os.path.isdir(p): # 判断是否为文件夹,如果是输出所有文件就改成: os.path.isfile(p)
print(p)
print('----'.join(dl_mp3_l))
res_mp3 = '/tmp/res{}.mp3'.format(int(time.time()))
# res_mp3 = './tmp/res{}.mp3'.format(int(time.time()))
d = '{}{}{}{}'.format('./ffmpeg-linux64-v3.3.1 -i "concat:', '|'.join(dl_mp3_l), '" -acodec copy ',
res_mp3)
print(d)
os.system(d)
# -r 10 fps设置为10帧/秒 ok图片切换
s = './ffmpeg-linux64-v3.3.1 -y -loop 1 -i /tmp/image%04d.jpg -i {} -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 {}'.format(
res_mp3, res_mp4)
print(s)
os.system(s)
s = '{}{}'.format('testtmp/', res_mp3)
with open(res_mp3, 'rb') as fr:
res = bucket.put_object(s, fr.read())
print(res.resp.response.url)
kid, ks, ep, bn = 'a', 'a', 'oss-cn-beijing.aliyuncs.com', 'mybn'
auth = oss2.Auth(kid, ks)
bucket = oss2.Bucket(auth, ep, bn)
img_l = []
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068339991800.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500066330.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500095850.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244112068340068340.jpg')
dl_img_l = []
str_0, c_ = ''.join(['0' for i in range(4)]), 1
for i in img_l:
# break
oss_path = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
f_extension = '.jpg'
s_ = '{}{}'.format(str_0, c_)
s_ = s_[len(s_) - 4:]
nn = '{}{}{}'.format('/tmp/image', s_, f_extension)
c_ += 1
try:
bucket.get_object_to_file(oss_path, nn)
dl_img_l.append(nn)
except Exception as e:
print(e)
pass
# die
break
print('----'.join(dl_img_l))
mp3_l = []
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_10.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_7.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_8.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_9.mp3')
dl_mp3_l = []
str_0, c_ = ''.join(['0' for i in range(4)]), 1
for i in mp3_l:
print(i)
oss_pth = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
print(oss_pth)
f_extension = '.mp3'
s_ = '{}{}'.format(str_0, c_)
s_ = s_[len(s_) - 4:]
nn = '{}{}{}'.format('/tmp/mp3', s_, f_extension)
c_ += 1
try:
print(nn)
bucket.get_object_to_file(oss_pth, nn)
dl_mp3_l.append(nn)
except Exception as e:
print(e)
pass
break
# die
print('----'.join(dl_mp3_l))
'''
./ffmpeg -y -loop 1 -i ./img/image%04d.jpg -i ./a.mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 95 a.mp4
'''
# path = os.listdir(os.getcwd())
#
# # print (path)
#
# for p in path:
# if os.path.isdir(p): # 判断是否为文件夹,如果是输出所有文件就改成: os.path.isfile(p)
# print(p)
#
# path = os.listdir('/tmp/')
#
# # print (path)
#
# for p in path:
# if os.path.isdir(p): # 判断是否为文件夹,如果是输出所有文件就改成: os.path.isfile(p)
# print(p)
#
#
# path='/tmp/'
# for p in path:
# print(p)
# if os.path.isdir(p): # 判断是否为文件夹,如果是输出所有文件就改成: os.path.isfile(p)
# print(p)
imgs = ' -i '.join(dl_img_l)
mp3 = ' -i '.join(dl_mp3_l)
res_mp4 = '/tmp/mymp4{}.mp4'.format(int(time.time()))
s = './ffmpeg-linux64-v3.3.1 -y -i {} -i {} -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 300 {}'.format(
imgs, mp3, res_mp4)
s = './ffmpeg-linux64-v3.3.1 -y -i /tmp/*g -i /tmp/*mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 300 {}'.format(
res_mp4)
s = './ffmpeg-linux64-v3.3.1 -y -loop 1 -i /tmp/image%04d.jpg -i /tmp/mp3%04d.mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 95 {}'.format(
res_mp4)
s = './ffmpeg-linux64-v3.3.1 -y -loop 1 -i /tmp/image%04d.jpg -i /tmp/*mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 95 {}'.format(
res_mp4)
# s = './ffmpeg-linux64-v3.3.1 -y -loop 1 -i /tmp/image%04d.jpg -i /tmp/mp3%04d.mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 300 {}'.format(
# res_mp4)
# 多个MP3传入?
s = './ffmpeg-linux64-v3.3.1 -y -loop 1 -i /tmp/image%04d.jpg -loop 1 -i /tmp/mp3001.mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 90 {}'.format(
res_mp4)
# -r 10 fps设置为10帧/秒(不同位置有不同含义,后面再解释)
s = './ffmpeg-linux64-v3.3.1 -y -r 5 -loop 1 -i /tmp/image%04d.jpg -i /tmp/mp30001.mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 90 {}'.format(
res_mp4)
# -r 10 fps设置为10帧/秒 ok图片切换
s = './ffmpeg-linux64-v3.3.1 -y -r 5 -loop 1 -i /tmp/image%04d.jpg -i /tmp/mp30001.mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 {}'.format(
res_mp4)
s = './ffmpeg-linux64-v3.3.1 -y -r 5 -loop 1 -i /tmp/image0001.jpg -i /tmp/mp30001.mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 {}'.format(
res_mp4)
# res_mp3 = '/tmp/res{}.mp3'.format(int(time.time()))
# d = '{}{}{}{}'.format('./ffmpeg-linux64-v3.3.1 -y -i "concat: ', '|'.join(dl_mp3_l), '" -acodec copy ',
# res_mp3)
# print(d)
# os.system(d)
#
# # 解决声音
# s = './ffmpeg-linux64-v3.3.1 -y -r 5 -loop 1 -i /tmp/image%04d.jpg -i {} -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 {}'.format(
# res_mp3, res_mp4)
print(s)
os.system(s)
s = '{}{}'.format('testtmp/', res_mp4)
with open(res_mp4, 'rb') as fr:
res = bucket.put_object(s, fr.read())
print(res.resp.response.url)
'''
待解决
./ffmpeg-linux64-v3.3.1 -y -loop 1 -i /tmp/image%04d.jpg -loop 1 -i /tmp/mp3%04d.mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 90 /tmp/mymp41525488667.mp4
'''
def handerBB(event=None, context=None):
kid, ks, ep, bn = 'a', 'a', 'oss-cn-beijing.aliyuncs.com', 'mybn'
auth = oss2.Auth(kid, ks)
bucket = oss2.Bucket(auth, ep, bn)
img_l = []
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068339991800.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500066330.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244012068500095850.jpg')
img_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/img/2018041914244112068340068340.jpg')
dl_img_l = []
for i in img_l:
ii = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
nn = i.split('/')[-1]
nn = '{}{}'.format('tmpimg/', nn)
try:
bucket.get_object_to_file(ii, nn)
dl_img_l.append(nn)
except Exception as e:
pass
# die
mp3_l = []
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_10.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_7.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_8.mp3')
mp3_l.append('https://mybn.oss-cn-beijing.aliyuncs.com/testtmp/mp3/34007463_9.mp3')
dl_mp3_l = []
for i in mp3_l:
ii = i.replace('https://mybn.oss-cn-beijing.aliyuncs.com/', '')
nn = i.split('/')[-1]
nn = '{}{}'.format('tmpmp3/', nn)
try:
bucket.get_object_to_file(ii, nn)
dl_mp3_l.append(nn)
except Exception as e:
pass
# die
'''
./ffmpeg -y -loop 1 -i ./img/image%04d.jpg -i ./a.mp3 -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 95 a.mp4
'''
#
# imgs = ' -i '.join(dl_img_l)
imgs = ' -i '.join(dl_img_l)
mp3 = ' -i '.join(dl_mp3_l)
res_mp4 = 'mymp42.mp4'
s = './ffmpeg-linux64-v3.3.1 -y -i {} -i {} -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 0.5 -t 300 {}'.format(
imgs, mp3, res_mp4)
print(s)
os.system(s)
s = '{}{}'.format('testtmp/', res_mp4)
with open(res_mp4, 'rb') as fr:
res = bucket.put_object(s, fr.read())
print(res.resp.response.url)
# hander(event=None, context=None)

浙公网安备 33010602011771号