我尝试使用 Arc 在 Elixir phoenix 项目上进行视频上传。我需要不同大小的视频版本。所以我使用FFmpeg来做到这一点:
def transform(:large, {file, scope}) do
{:ffmpeg, fn(input, output) -> "-i #{input} -c:a copy -s 1920x1080 -f mp4 #{output}" end, :mp4}
end
但是,即使是小视频,也需要花费大量时间来创建此版本。所以,我决定在后台进行版本创建。我希望 Arc 上传视频,然后在创建其他版本时用上传的原件给出响应。
我希望在 https://github.com/stavro/arc 找到这样的选项但我没有成功。我只发现“要禁用异步处理,请将 @async false 添加到您的上传定义”,但我不想禁用它,对吧?
我尝试使用 Exq https://github.com/akira/exq用于后台处理,但我没有在 uploader 中使用它。
谁能告诉我应该如何以适当的方式制作它,或者给我一些肮脏的黑客建议以使其工作。谢谢。
我按照评论中的建议尝试了 Task,但我不确定在这种情况下如何使用它们。当我尝试
{:ffmpeg, fn(input, output) -> Task.async(fn -> "-i #{input} -c:a copy -s 1920x1080 -f mp4 #{output}" end) end, :mp4}
或者
{:ffmpeg, Task.async(fn -> fn(input, output) -> "-i #{input} -c:a copy -s 1920x1080 -f mp4 #{output}" end end), :mp4}
我得到“没有为 %Task 实现协议(protocol) String.Chars”。
当我尝试
{:ffmpeg, Task.async(fn(input, output) -> "-i #{input} -c:a copy -s 1920x1080 -f mp4 #{output}" end), :mp4}
我得到“MyWebSite.Content.transform/2> 中的#Function<20.83953603/2> 没有参数调用 arity 2”。我试图用“&”将函数作为参数传递,但它也失败了。
我的上传者:
defmodule MyWebSite.Content do
use Arc.Definition
use Arc.Ecto.Definition
@acl :public_read
@versions [:original, :huge]
def transform(:huge, {file, scope}) do
{:ffmpeg, Task.async(fn(input, output) -> "-i #{input} -c:a copy -s 1920x1080 -f mp4 #{output}" end), :mp4}
end
def s3_object_headers(version, {file, scope}) do
[timeout: 3_000_00, content_type: Plug.MIME.path(file.file_name)]
end
end
最佳答案
来自 Arc
的自述文件(Complex Transformations 部分):
However, the transformation may be done completely outside of
Arc
. For fine-grained transformations, you should create an executable wrapper in your$PATH
(eg. bash script) which takes these proper arguments, runs your transformation, and then moves the file into the correct location**.
也就是说,如果您希望转换完全异步运行,只需在某处创建一个 bash 脚本并调用它(假设它的名称是
myffmpeg
):def transform(:huge, {file, scope}) do
{:myffmpeg,
fn ->
"-i #{input} -c:a copy -s 1920x1080 -f mp4 #{output}"
end,
:mp4}
end
脚本应生成
ffmpeg
传递参数并将结果移动到文件夹 Arc
想要。
关于ffmpeg - 使用 Arc 和 FFmpeg 在 Elixir 中创建上传视频的背景版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47596764/