在不渲染的情况下检查moviepy中的视频长度

时间:2020-07-10 13:12:37

标签: python moviepy

我想创建一个Python脚本,该脚本将在X秒钟内显示图像(直到音频消失为止的时间),然后转到下一张图像。我希望视频长X。视频的长度将始终是音频的不同原因。我可以检查未渲染视频的长度吗?我的代码:

Option Explicit
'Tutorial link: https://youtu.be/H4-w6ULc_qs
#If VBA7 Then
   Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
     "URLDownloadToFileA" (ByVal pCaller As LongPtr, ByVal szURL As String, ByVal _
       szFileName As String, ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As LongPtr
#Else
   Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
     "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
       szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
#End If

Sub download_file()
'-----------------------------
'Thanks for downloading the code.
'Please visit our channel for a quick explainer on how to use this code.
'Feel free to update the code as per your need and also share with your friends.
'Download free codes from http://vbaa2z.blogspot.com
'Support our channel: youtube.com/vbaa2z
'Author: L Pamai (vbaa2z.team@gmail.com)
'-----------------------------

Dim downloadStatus As Variant
Dim url As String
Dim destinationFile_local As String

url = [D3]
destinationFile_local = "C:\Users\myUserName\Downloads\" & fileName([D3])

downloadStatus = URLDownloadToFile(0, url, destinationFile_local, 0, 0)

If downloadStatus = 0 Then
    MsgBox "Downloaded Succcessfully!"
    Else
    MsgBox "Download failed"
End If

End Sub

Function fileName(file_fullname) As String

    fileName = Mid(file_fullname, InStrRev(file_fullname, "/") + 1)

End Function

1 个答案:

答案 0 :(得分:0)

调用命令ffprobe比使用moviepy更快:

import subprocess

def get_length(filename):
    result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
                             "format=duration", "-of",
                             "default=noprint_wrappers=1:nokey=1", filename],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    return float(result.stdout)

duration = get_length("test.mp4")
print(duration) # 30.024000