通过 ruby 中的 API 将视频上传到 vimeo
Uploading a video to vimeo via the API in ruby
我一直在尝试通过 ruby 将 MP4 视频上传到 vimeo。起初我以为我会尝试 ruby gem,但看着它使用现在已弃用的 vimeo API。我已经能够通过自己制作的表格上传,但完全使用代码似乎还没有用。
我有以下代码可以通过流 API 上传(它主要基于 vimeo python 库):
auth = "Bearer #{ACCESS_TOKEN}"
resp = HTTParty.post "https://api.vimeo.com/me/videos", headers: { "Authorization" => auth, "Accept" => "application/vnd.vimeo.*+json;version=3.2" }, body: { type: "streaming"}
ticket = JSON.parse(resp.body)
target = ticket["upload_link"]
size = File.size("movie.mp4")
last_byte = 0
File.open("movie.mp4") do |f|
while last_byte < size do
resp = HTTParty.put target, headers: { "Authorization" => auth, "Content-Length" => size.to_s, "Content-Range" => "bytes: #{last_byte}-#{size}/#{size}" }, body: { data: a }
progress_resp = HTTParty.put target, headers: { "Content-Range" => 'bytes */*', "Authorization" => auth }
last_byte = progress_resp.headers["range"].split("-").last.to_i
puts last_byte
end
end
resp = HTTParty.delete "https://api.vimeo.com#{ticket["complete_uri"]}", headers: { "Authorization" => auth }
对于最后一行,resp 输出以下错误:
"{\"error\":\"Your video file is not valid. Either you have uploaded an invalid file format, or your upload is incomplete. Make sure you verify your upload before marking it as complete.\"}"
和 last_byte 输出: 28518622
在一个 运行 通过大于实际文件大小 (11458105) 的循环之后。
考虑通过gem vimeo上传。似乎 Video::Advanced::Upload
比 HTTParty
更符合您的需求
HTTPParty 是用于此的错误工具。将其更改为与正常的 Net::HTTP
lib 一起工作就可以了。
File.open("movie.mp4", "rb") do |f|
uri = URI(target)
while last_byte < size do
req = Net::HTTP::Put.new("#{uri.path}?#{uri.query}", initheader = { "Authorization" => auth, "Content-Length" => size.to_s, "Content-Range" => "bytes: #{last_byte}-#{size}/#{size}"} )
req.body = f.read
begin
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
rescue Errno::EPIPE
puts "error'd"
end
progress_resp = HTTParty.put target, headers: { "Content-Range" => 'bytes */*', "Authorization" => auth}
last_byte = progress_resp.headers["range"].split("-").last.to_i
puts last_byte
end
end
我一直在尝试通过 ruby 将 MP4 视频上传到 vimeo。起初我以为我会尝试 ruby gem,但看着它使用现在已弃用的 vimeo API。我已经能够通过自己制作的表格上传,但完全使用代码似乎还没有用。
我有以下代码可以通过流 API 上传(它主要基于 vimeo python 库):
auth = "Bearer #{ACCESS_TOKEN}"
resp = HTTParty.post "https://api.vimeo.com/me/videos", headers: { "Authorization" => auth, "Accept" => "application/vnd.vimeo.*+json;version=3.2" }, body: { type: "streaming"}
ticket = JSON.parse(resp.body)
target = ticket["upload_link"]
size = File.size("movie.mp4")
last_byte = 0
File.open("movie.mp4") do |f|
while last_byte < size do
resp = HTTParty.put target, headers: { "Authorization" => auth, "Content-Length" => size.to_s, "Content-Range" => "bytes: #{last_byte}-#{size}/#{size}" }, body: { data: a }
progress_resp = HTTParty.put target, headers: { "Content-Range" => 'bytes */*', "Authorization" => auth }
last_byte = progress_resp.headers["range"].split("-").last.to_i
puts last_byte
end
end
resp = HTTParty.delete "https://api.vimeo.com#{ticket["complete_uri"]}", headers: { "Authorization" => auth }
对于最后一行,resp 输出以下错误:
"{\"error\":\"Your video file is not valid. Either you have uploaded an invalid file format, or your upload is incomplete. Make sure you verify your upload before marking it as complete.\"}"
和 last_byte 输出: 28518622
在一个 运行 通过大于实际文件大小 (11458105) 的循环之后。
考虑通过gem vimeo上传。似乎 Video::Advanced::Upload
比 HTTParty
HTTPParty 是用于此的错误工具。将其更改为与正常的 Net::HTTP
lib 一起工作就可以了。
File.open("movie.mp4", "rb") do |f|
uri = URI(target)
while last_byte < size do
req = Net::HTTP::Put.new("#{uri.path}?#{uri.query}", initheader = { "Authorization" => auth, "Content-Length" => size.to_s, "Content-Range" => "bytes: #{last_byte}-#{size}/#{size}"} )
req.body = f.read
begin
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
rescue Errno::EPIPE
puts "error'd"
end
progress_resp = HTTParty.put target, headers: { "Content-Range" => 'bytes */*', "Authorization" => auth}
last_byte = progress_resp.headers["range"].split("-").last.to_i
puts last_byte
end
end