0

我曾尝试使用 pafy,但它从头开始播放视频,我想在视频的特定部分运行我的模型。如果这是可能的,请指导我如何做到这一点。

任何帮助表示赞赏,在此先感谢:)

4

1 回答 1

0

实际上这很容易,我想通了:)

这是代码:

import cv2
import pafy

#Ask the user for url input
url = input("Enter Youtube Video URL: ")

#Getting video id from the url string
url_data = urlparse.urlparse(url)
query = urlparse.parse_qs(url_data.query)
id = query["v"][0]
video = 'https://youtu.be/{}'.format(str(id))

#Using the pafy library for youtube videos
urlPafy = pafy.new(video)
videoplay = urlPafy.getbest(preftype="any")

cap = cv2.VideoCapture(videoplay.url)

#Asking the user for video start time and duration in seconds
milliseconds = 1000
start_time = int(input("Enter Start time: "))
end_time = int(input("Enter Length: "))
end_time = start_time + end_time

# Passing the start and end time for CV2
cap.set(cv2.CAP_PROP_POS_MSEC, start_time*milliseconds)

#Will execute till the duration specified by the user
while True and cap.get(cv2.CAP_PROP_POS_MSEC)<=end_time*milliseconds:
        success, img = cap.read()
        cv2.imshow("Image", img)
        cv2.waitKey(1)
于 2021-10-08T09:38:56.313 回答