我正在尝试编写一个 lambda 函数(最终)将 的输出youtube-dl直接写入 S3。我必须对此进行原型设计的方法是将输出转储stdout到 S3 中,然后将其重定向到 S3 中的文件,但这似乎是一个巨大的黑客攻击。
import youtube_dl, sys
from smart_open import open
from contextlib import redirect_stdout
ydl_opts = { 'outtmpl': '-', 'cachedir': False, 'logtostderr': True }
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
with open('s3://<test-bucket>/test.mp4','wb') as f:
with redirect_stdout(f):
ydl.download(['https://www.youtube.com/watch?v=zzEYsi_P0RU'])
Run Code Online (Sandbox Code Playgroud)
还有其他人有什么神奇的想法吗?
您可以使用 Nodejs 流来执行此操作,只要您可以在最多 15 分钟内完成任务,否则 lambda 任务将超时。
const stream = require('stream');
const passthrough = new stream.PassThrough();
Run Code Online (Sandbox Code Playgroud)
const youtubedl = require('youtube-dl');
const dl = youtubedl(event.videoUrl, ['--format=best[ext=mp4]'],
{maxBuffer: Infinity});
dl.pipe(passtrough); // write video to the pass-through stream
Run Code Online (Sandbox Code Playgroud)
const AWS = require('aws-sdk');
const upload = new AWS.S3.ManagedUpload({
params: {
Bucket: process.env.BUCKET_NAME,
Key: 'video.mp4',
Body: passtrough
},
partSize: 1024 * 1024 * 64 // 64 MB in bytes
});
upload.send((err) => {
if (err) {
console.log('error', err);
} else {
console.log('done');
}
});
Run Code Online (Sandbox Code Playgroud)
尝试为 lambda 提供足够的资源(例如大量内存)以获得更好的网络性能。
| 归档时间: |
|
| 查看次数: |
1198 次 |
| 最近记录: |