【发布时间】:2012-12-12 17:15:44
【问题描述】:
我想创建一个允许用户将视频上传到 Youtube 的应用程序。 这是我尝试过的代码(改编自http://code.google.com/p/gdata-python-client/source/browse/tests/gdata_tests/youtube/service_test.py):
my_media_group = gdata.media.Group(
title = gdata.media.Title(text=title),
description = gdata.media.Description(description_type='plain',
text=description),
keywords = gdata.media.Keywords(text= tags),
category = gdata.media.Category(
text='Autos',
scheme='http://gdata.youtube.com/schemas/2007/categories.cat',
label='Autos'),
player=None
)
# Set Geo location to 37,-122 lat, long
where = gdata.geo.Where()
where.set_location((37.0,-122.0))
video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group, geo=where)
print "Creating a video entry: done."
print "Uploading video"
new_entry = self.client.InsertVideoEntry(video_entry, filepath)
print "Done uploading video."
总是卡在倒数第二行:
new_entry = self.client.InsertVideoEntry(video_entry, filepath)
那怎么了?对文件路径是否有任何要求(例如,路径“C:\video.avi”是否有效?)
其实我只是需要一种上传方式,所以请提出任何可能的解决方案。
编辑:1. 我尝试嵌入 Youtube Upload 小部件,但它似乎不起作用:(只有一个选项可以从网络摄像头加载,这也不起作用。)
<iframe id="widget" type="text/html" width="640" height="390"
src="https://www.youtube.com/upload_embed" frameborder="0"></iframe>
- 我使用 QWebview 加载 Youtube 的上传页面,但 Youtube 总是返回错误。 QWebview: Upload to Youtube return error
编辑 2:
我把代码改成:
def getYtSession(self, email = '', password =''):
# if we do not want to reuse our session.
if email != '' and password != '':
yt_service = gdata.youtube.service.YouTubeService()
yt_service.email = email
yt_service.password = password
#login.
try:
print yt_service.ProgrammaticLogin()
self.email = email
self.password = password
self.logged_in = True
self.emit(QtCore.SIGNAL('doneLogin(QString)'), QtCore.QString(email + " " + password))
return yt_service
except:
#Display a warning dialog.
self.logged_in = False
self.emit(QtCore.SIGNAL('failedLogin()'))
return None
elif self.yt_service: # we want to reuse the session.
return self.yt_service
else:
return gdata.youtube.service.YouTubeService()
在上传功能中:
self.yt_service = self.getYtSession(self.email, self.password)
try:
new_entry = self.yt_service.InsertVideoEntry(video_entry, filepath)
print "Done uploading video."
self.emit(QtCore.SIGNAL('doneUpload(QString)'), QtCore.QString('Title: ' + title + '\nPath: ' + filepath))
except:
print "Stack trace:"
traceback.print_stack()
还是不行。还有一个问题:堆栈跟踪没有任何帮助:
Stack trace:Login successfully.
文件“D:\workplace\simple-media-player\trunk\MyPlayer\src\QtThread.py”,第 19 行,运行中 self.function(*self.args,**self.kwargs) 文件“D:\workplace\simple-media-player\trunk\MyPlayer\src\videoplayer.py”,第 490 行,在 doRealUpload traceback.print_stack()
该消息(“登录成功。”)由发出信号 doneLogin(QString) 时调用的函数打印。如果我不放
new_entry = self.yt_service.InsertVideoEntry(video_entry, filepath)
print "Done uploading video."
self.emit(QtCore.SIGNAL('doneUpload(QString)'), QtCore.QString('Title: ' + title + '\nPath: ' + filepath))
在 try-except 中,它什么也不打印。
更多信息:我将视频信息设置如下: 路径:C:/Users/huynh/Desktop/REcord1.wmv 名称:“测试视频” 标签:'测试,youtube' 说明:发发发
【问题讨论】:
-
当您说“卡住了”时,您是否有 Python 在该行返回的错误代码?另一件需要注意的事情是你在哪里创建了你的 Youtube 服务对象(以及你称之为什么,以及它的身份验证是否成功)——在进入上传方法之前,一切都与创建该对象一起工作吗?
-
InsertVideoEntry() 是实际上传文件的 API 调用。如果您的视频文件较大且上行链路连接速度较慢,则传输可能需要很长时间才能完成。您没有得到回复的事实也意味着这很可能只是一个缓慢的上传。您可以尝试通过 YouTube 网络界面上传相同的视频文件,看看需要多长时间。
-
@jlmcdonal:我看不出登录过程有任何问题。成功登录 self.yt_service 后,我尝试保存 yt_service,但没有成功,因此我(成功)按照上面的代码重新创建它。
标签: python upload youtube youtube-api