【问题标题】:Uploading a video to vimeo via the API in ruby通过 ruby​​ 中的 API 将视频上传到 vimeo
【发布时间】:2015-07-16 21:28:08
【问题描述】:

我一直在尝试通过 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)。

【问题讨论】:

    标签: ruby upload vimeo


    【解决方案1】:

    HTTParty 是用于此目的的错误工具。将其更改为与普通的 Net::HTTP 库一起工作就可以了。

      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
    

    【讨论】:

      【解决方案2】:

      考虑通过gem vimeo 上传。似乎Video::Advanced::UploadHTTParty 更适合您的需求

      【讨论】:

      • 如帖子中所述,vimeo gem 使用已弃用的 API,因此不再是有效选项。
      • 哦,我不明白你的意思是这个宝石。
      猜你喜欢
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多