1.多视频播放器
这个脚本页面的功能:
- 多个本地视频可以同时播放
- 可以设置倍速播放,可以自定义需要的速度。(最大倍速16倍)
- 写这个页面是为了,快速浏览多个视频(累计30个小时的视频数据中),选出对自己的项目有用的数据——用来训练目标检测的数据。
- 然后从这些视频中抽出需要的帧。以方便后续进行数据标注。
类似这样子:
全部代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiple Video Player</title>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
.video-container {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
height: 100%;
}
.video-player {
width: 100%;
height: 100%;
max-width: 33.33%;
max-height: 50%;
object-fit: cover;
}
</style>
</head>
<body>
<h1>Multiple Video Player</h1>
<label for="speed">选择倍速:</label>
<select id="speed" onchange="changeSpeed()">
<option value="0.5">0.5x</option>
<option value="1.0" selected>1.0x</option>
<option value="1.5">1.5x</option>
<option value="2.0">2.0x</option>
<option value="10.0">10.0x</option>
<option value="12.0">12.0x</option>
<option value="16.0">16.0x</option>
</select>
<div class="video-container">
<video controls class="video-player" id="videoPlayer1">
<source src="path/to/your/video1.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<video controls class="video-player" id="videoPlayer1">
<source src="path/to/your/video2.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<video controls class="video-player" id="videoPlayer1">
<source src="path/to/your/video3.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<!-- 依次添加更多的视频元素 -->
</div>
<script>
function changeSpeed() {
var videos = document.getElementsByClassName("video-player");
var speedSelect = document.getElementById("speed");
for (var i = 0; i < videos.length; i++) {
videos[i].playbackRate = parseFloat(speedSelect.value);
}
}
</script>
</body>
</html>
2.从视频中抽帧
脚本功能:
- 从多个时间段抽取视频帧。
- 设置抽帧间隔为每隔n帧抽取一帧
- 按自己需求重新命名图片名字
注意:路径和名字中不要有中文,不然找不到路径。
全部代码:
import cv2
import os
from moviepy.editor import VideoFileClip
def extract_frames_from_video(input_video_path, output_frames_folder, time_ranges,frame_interval):
for i, (start_time, end_time) in enumerate(time_ranges, start=1):
# 转换 MM:SS 格式为 (minutes, seconds)
start_minutes, start_seconds = map(int, start_time.split(':'))
end_minutes, end_seconds = map(int, end_time.split(':'))
# 裁剪视频
clip = VideoFileClip(input_video_path).subclip((start_minutes, start_seconds), (end_minutes, end_seconds))
# 提取帧
frames = clip.iter_frames(fps=clip.fps)
first_fra = 25*(start_minutes*60+start_seconds)
# 保存帧为图像文件
for j, frame in enumerate(frames):
# 设置抽帧间隔为每隔2帧抽取一帧
if j % frame_interval == 0:
# 使用帧数信息命名新照片的名字
frame_path = os.path.join(output_frames_folder, f'YD2_{input_video_path[27:-4]}_{j+first_fra:06d}.jpg')
print(frame_path)
# 使用 OpenCV 保存图像
cv2.imwrite(frame_path, cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
# 多个时间段的列表,每个元素是一个元组 (开始时间, 结束时间)
# time_ranges = [("42:50", "43:31"), ("44:00", "44:15"), ("45:30", "46:00")]
time_ranges = [("58:56","59:59")]
# 输入视频路径
input_video_path = "E:/xxxx/video.mp4"
# 输出帧的文件夹路径
output_frames_folder = "E:/xxxx/frame"
# 设置抽帧间隔为每隔2帧抽取一帧
frame_interval = 5
extract_frames_from_video(input_video_path, output_frames_folder, time_ranges, frame_interval)