【问题标题】:How to get the duration of a video file that's in Google Drive with Python?如何使用 Python 获取 Google Drive 中视频文件的持续时间?
【发布时间】:2021-05-24 07:39:25
【问题描述】:

我的视频文件 ID 和 Google Drive API 访问都正确,我希望我的代码返回视频文件的持续时间。我知道有“videoMediaMetadata.durationMillis”文件属性,但我不知道如何使用它。谢谢!

SCgenericQ = service.files().list(q="mimeType = 'application/vnd.google-apps.folder' and starred=true",
        fields="nextPageToken, files(id, name)").execute()
    starredSCfolders = SCgenericQ.get('files', [])

    if not starredSCfolders:
        print('No files found.')
    else:
        counter = 18
        for _ in starredSCfolders:
            folderId = starredSCfolders[counter].get('id')
            exactCourseQ = service.files().list(q="'" + folderId + "' in parents", fields="nextPageToken, files(id, name)").execute()
            courseFiles = exactCourseQ.get('files', [])  
            courseUnitIds = []
            videos = []
            
            for element in courseFiles:
                if element['name'].startswith('UNI'):
                    courseUnitIds.append(element['id'])
            for Id in courseUnitIds:
                openLessons = service.files().list(q="'" + Id + "' in parents and mimeType = 'application/vnd.google-apps.folder'", fields="nextPageToken, files(id, name)").execute()
                LessonIds = openLessons.get('files', [])
                
                filesExcFolder = service.files().list(q="'" + Id + "' in parents and mimeType != 'application/vnd.google-apps.folder'", fields="nextPageToken, files(id, name)").execute()
                nonFolders = filesExcFolder.get('files', [])

                if not LessonIds:
                    MOV_videoFiles = service.files().list(q="'" + Id + "' in parents and mimeType contains 'video/Quicktime'", fields="nextPageToken, files(id, name)").execute()
                    movFiles = MOV_videoFiles.get('files', [])
                    MP4_videoFiles = service.files().list(q="'" + Id + "' in parents and mimeType contains 'video/mp4'", fields="nextPageToken, files(id, name)").execute()
                    mp4Files = MP4_videoFiles.get('files', [])
                    for mov in movFiles:
                        videos.append(mov['name'])
                    for mp4 in mp4Files:
                        videos.append(mp4['id'])
                    for videoId in videos:
                        request = service.files().get(fileId="'" + videoId + "'", fields='videoMediaMetadata').execute()
                        duration_seconds = int(request.get('videoMediaMetadata')['durationMillis'])/1000

            print(videos) 
            print(duration_seconds)      
            break```

【问题讨论】:

    标签: python api drive


    【解决方案1】:

    假设您已经可以访问您的驱动器文件和您在问题中提到的元数据:durationMillis 对应于视频的持续时间(以毫秒为单位)。您只需将持续时间除以 1000 即可获得秒数。

    
    request = service.files().get(fileId=ID_OF_VIDEO, fields='videoMediaMetadata').execute()
    duration_seconds = int(request.get('videoMediaMetadata')['durationMillis'])/1000
    

    编辑

    您不需要再次获取文件,只需制作 google drive 即可首先为您提供所需的数据。只需在需要的字段部分添加videoMediaMetadata

    ### your other code above make sure you place "durations = []" below your "videos=[]"!
               
                        MP4_videoFiles = service.files().list(q="'" + Id + "' in parents and mimeType contains 'video/mp4'", fields="nextPageToken, files(id, name, videoMediaMetadata)").execute() # This gives you the videoMediaMetadata!
                        mp4Files = MP4_videoFiles.get('files', [])
                        for mov in movFiles:
                            videos.append(mov['name'])
                        for mp4 in mp4Files:
                            videos.append(mp4['id']
                            durations.append(int(mp4['videoMediaMetadata']['durationMillis'])/1000) # read the duration 
    
                print(videos) 
                print(durations)     
    

    如果需要,您可以将示例扩展到其他用例。请注意,这仍然不是一个花哨的解决方案。理想情况下,您可以通过良好的请求做更多事情,从而节省更多时间,但这应该可以工作,您可以改进。希望对您有所帮助!

    【讨论】:

    • 你的代码给了我这个错误信息:googleapis.com/drive/v3/files/… returned "File not found: 'U4 L2.mov'."。详细信息:“[{'domain': 'global', 'reason': 'notFound', 'message': "File not found: 'U4 L2.mov'.", 'locationType': 'parameter', 'location' :'fileId'}]">我做错了什么?我将分享我的其余代码,我知道它非常混乱,还有很多需要改进的地方,但我是 python 新手。非常感谢!!
    • @MaPitelli 您如何访问您的文件?你看过谷歌驱动 API 的 Python 快速入门吗?勾选here即可获取服务并访问文件。请注意,在我的代码中,文件是通过 ID 而不是名称访问的。如果您编辑答案并提供您如何访问文件的代码,这将有所帮助。如果你这样做了,请告诉我
    • 在我的代码中,我可以获得 print(videos) 以返回文件夹内的视频文件的名称。因为我知道这个文件夹 (18) 只有 mp4 视频文件格式,所以我更改了 mp4 for 循环以将视频的 id 附加到视频列表中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 2015-11-24
    • 2022-08-18
    • 2019-04-08
    相关资源
    最近更新 更多