0

我想创建一个允许用户将视频上传到 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>
  1. 我使用 QWebview 加载 Youtube 的上传页面,但 Youtube 总是返回错误。 QWebview:上传到 Youtube 返回错误

编辑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 名称:'A test video' 标签:'test, youtube' 描述:fafafa

4

1 回答 1

0

好的不要紧。我在http://code.google.com/p/gdata-python-client/source/browse/tests/gdata_tests/youtube/service_test.py修改了代码,它运行良好!

于 2012-12-13T10:02:14.477 回答