Kas*_*ora 6 python opencv ffmpeg video-capture youtube-dl
我需要提取在线视频的特定帧来处理算法,但我不想下载整个视频,因为这会使其效率极低。
首先,我尝试使用 YouTube 视频。youtube-dl我可以使用以下方式下载整个视频:
ydl_opts = {'outtmpl': r'OUTPUT_DIRECTORY_HERE',}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
Run Code Online (Sandbox Code Playgroud)
然后我可以捕捉各个帧。
我需要避免下载整个视频。经过一些研究,我发现这ffmpeg可能会帮助我做到这一点。我发现无法仅下载帧,因此如果不可能,第二个选项是我可以下载视频的特定部分。linux 中的一个这样的例子是here,但我找不到任何适用于python 的解决方案。
只下载帧或部分视频(在 python 中)而不下载整个内容的好方法是什么?
只是为了补充当前的答案,使用多处理可以进一步增强性能。例如,如果您想将视频分割成帧并在num_cpu进程中独立处理它们:
import os
from functools import partial
from multiprocessing.pool import Pool
import cv2
import youtube_dl
def process_video_parallel(url, skip_frames, process_number):
cap = cv2.VideoCapture(url)
num_processes = os.cpu_count()
frames_per_process = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) // num_processes
cap.set(cv2.CAP_PROP_POS_FRAMES, frames_per_process * process_number)
x = 0
count = 0
while x < 10 and count < frames_per_process:
ret, frame = cap.read()
if not ret:
break
filename =r"PATH\shot"+str(x)+".png"
x += 1
cv2.imwrite(filename.format(count), frame)
count += skip_frames # Skip 300 frames i.e. 10 seconds for 30 fps
cap.set(1, count)
cap.release()
video_url = "..." # The Youtube URL
ydl_opts = {}
ydl = youtube_dl.YoutubeDL(ydl_opts)
info_dict = ydl.extract_info(video_url, download=False)
formats = info_dict.get('formats', None)
print("Obtaining frames")
for f in formats:
if f.get('format_note', None) == '144p':
url = f.get('url', None)
cpu_count = os.cpu_count()
with Pool(cpu_count) as pool:
pool.map(partial(process_video_parallel, url, 300), range(cpu_count))
Run Code Online (Sandbox Code Playgroud)
就本应用程序而言,由于仅从视频中保存图像,因此这可能不会带来巨大的改进(可能几秒钟),但如果需要在帧上应用其他算法,则可能会有所帮助。
| 归档时间: |
|
| 查看次数: |
8446 次 |
| 最近记录: |