使用 Rails 通过 youtube_it gem 将视频上传到 YouTube 时找出目标 YouTube URL
我已经让“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
upload
方法返回一个YouTubeIt::Model::Video
,它具有player_url
属性。因此,只需捕获upload
方法的返回值,然后调用player_url
即可,瞧,您就拥有了视频的 URL。有关模型具有的其他属性的列表,请检查 源代码。例子:
The
upload
method returns aYouTubeIt::Model::Video
, which has aplayer_url
attribute. So just catch the return value from theupload
method, and callplayer_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:
如何获取用户上传的视频列表并选择最新的一个?
$ 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