使用 Rails 通过 youtube_it gem 将视频上传到 YouTube 时找出目标 YouTube URL

发布于 2024-11-07 03:28:44 字数 1381 浏览 2 评论 0原文

我已经让“youtube_it”与 Paperclip 结合使用,可以使用以下代码通过浏览器处理视频上传:

videos_controller.rb

  def create
    @video = Video.new(params[:video])
    if @video.save
      uploader = YouTubeIt::Upload::VideoUpload.new( :username => AppConfig[:youtube_user], 
                                                     :password =>  AppConfig[:youtube_pass], 
                                                     :dev_key => AppConfig[:youtube_api_key])
      uploader.upload open( params[:video][:attachment] ), :title => @video.title,
                                                           :description => @video.description,
                                                           :category => 'some category',
                                                           :keywords => ['some keyword 1', "some keyword 2"]
      @video.deliver_video_notification
      flash[:notice] = 'Your video is under review for approval.<br/> Please check back in 48 hours.'
      redirect_to videos_url
    else
      @errors = @video.errors
      @current_video = params[:v].blank? ? Video.newest : Video.find(params[:v])
      render :action => :index
    end
  end

但是,一旦视频上传,我不知道 YouTube 为视频创建了什么 URL,而无需手动记录进入 YouTube 频道并查找。我没有在日志或 response.body 中看到任何显示目的地的回调。我想以某种 after_save 方法以编程方式保存目的地。现在它正在工作,它正在保存视频对象,保存后将视频上传到 youtube。

I've gotten the 'youtube_it' working great in conjunction with Paperclip to handle video uploads through the browser using the following code:

videos_controller.rb

  def create
    @video = Video.new(params[:video])
    if @video.save
      uploader = YouTubeIt::Upload::VideoUpload.new( :username => AppConfig[:youtube_user], 
                                                     :password =>  AppConfig[:youtube_pass], 
                                                     :dev_key => AppConfig[:youtube_api_key])
      uploader.upload open( params[:video][:attachment] ), :title => @video.title,
                                                           :description => @video.description,
                                                           :category => 'some category',
                                                           :keywords => ['some keyword 1', "some keyword 2"]
      @video.deliver_video_notification
      flash[:notice] = 'Your video is under review for approval.<br/> Please check back in 48 hours.'
      redirect_to videos_url
    else
      @errors = @video.errors
      @current_video = params[:v].blank? ? Video.newest : Video.find(params[:v])
      render :action => :index
    end
  end

However once the video is uploaded, I have no idea what URL YouTube created for the video without manually logging into the YouTube channel and looking it up. I didn't see any callback in the logs or the response.body that revealed the destination. I'd like to programatically save the destination in some kind of after_save method. As it works right now, it is saving the video object, and after save it is uploading the video to youtube.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泡沫很甜 2024-11-14 03:28:44

upload 方法返回一个 YouTubeIt::Model::Video,它具有 player_url 属性。因此,只需捕获 upload 方法的返回值,然后调用 player_url 即可,瞧,您就拥有了视频的 URL。有关模型具有的其他属性的列表,请检查 源代码

例子:

def create
  @video = Video.new(params[:video])
  if @video.save
    uploader = YouTubeIt::Upload::VideoUpload.new( :username => AppConfig[:youtube_user], 
                                                   :password =>  AppConfig[:youtube_pass], 
                                                   :dev_key => AppConfig[:youtube_api_key])
    uploaded_video = uploader.upload open( params[:video][:attachment] ), :title => @video.title,
                                                         :description => @video.description,
                                                         :category => 'some category',
                                                         :keywords => ['some keyword 1', "some keyword 2"]

    puts uploaded_video.player_url # This will print the URL to the log

    @video.deliver_video_notification
    flash[:notice] = 'Your video is under review for approval.<br/> Please check back in 48 hours.'
    redirect_to videos_url
  else
    @errors = @video.errors
    @current_video = params[:v].blank? ? Video.newest : Video.find(params[:v])
    render :action => :index
  end
end

The upload method returns a YouTubeIt::Model::Video, which has a player_url attribute. So just catch the return value from the upload method, and call player_url on that, and voilà, you have the URL to the video. For a list of the other attributes that model has, check the source code.

Example:

def create
  @video = Video.new(params[:video])
  if @video.save
    uploader = YouTubeIt::Upload::VideoUpload.new( :username => AppConfig[:youtube_user], 
                                                   :password =>  AppConfig[:youtube_pass], 
                                                   :dev_key => AppConfig[:youtube_api_key])
    uploaded_video = uploader.upload open( params[:video][:attachment] ), :title => @video.title,
                                                         :description => @video.description,
                                                         :category => 'some category',
                                                         :keywords => ['some keyword 1', "some keyword 2"]

    puts uploaded_video.player_url # This will print the URL to the log

    @video.deliver_video_notification
    flash[:notice] = 'Your video is under review for approval.<br/> Please check back in 48 hours.'
    redirect_to videos_url
  else
    @errors = @video.errors
    @current_video = params[:v].blank? ? Video.newest : Video.find(params[:v])
    render :action => :index
  end
end
野の 2024-11-14 03:28:44

如何获取用户上传的视频列表并选择最新的一个?

$ client.my_videos

https://github.com/kylejginavan/youtube_it

How about getting the list of videos uploaded by the user and choosing the latest one?

$ client.my_videos

https://github.com/kylejginavan/youtube_it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文