readCsv_videoAddText.py
import cv2
import csv
import re # 正则表达式模块
import time
import os
import textwrap
# 程序运行时间计时 开始
start_time = time.perf_counter()
mid_time = start_time
# 定义一个函数,将字符串转化为秒数
def convert_to_seconds(duration):
# 使用正则表达式匹配时、分、秒的数字
match = re.search(r'PT(\d+)H(\d+)M(\d+)S', duration)
# 如果匹配成功,将数字转化为整数,并计算总秒数
if match:
hours = int(match.group(1))
minutes = int(match.group(2))
seconds = int(match.group(3))
total_seconds = hours * 3600 + minutes * 60 + seconds
return total_seconds
# 如果匹配失败,返回None
else:
return 0
# 打开CSV文件
with open('results_10M_train.csv', 'r', encoding="utf-8") as csvFile:
# 创建CSV读取器
csvReader = csv.reader(csvFile)
# 行数
count_row = 0
# 有效视频数
count_video = 0
# 跳过第一行(表头)
next(csvReader)
# 读取CSV数据并进行处理
for row in csvReader: # 对于一行数据(对应一个视频)
count_row += 1
if count_row > 100000000:
# count_row -= 1
break
# 在这里对每一行数据进行处理
videoid = row[0]
duration = row[2]
page_dir = row[3]
name = row[4]
duration = convert_to_seconds(duration)
# print('videoid: ', videoid)
# print('duration:', duration)
# print('page_dir: ',page_dir)
# print('name: ',name)
# 原始文件路径
ori_file_path = './WebVid/' + page_dir
# print('ori_file_path: ' + ori_file_path)
# 原始文件名
ori_file_name = str(videoid) + '.mp4' # 原文件名
ori_file_path_name = ori_file_path + '/' + ori_file_name
# print('ori_file_path_name: ' + ori_file_path_name)
if(os.path.exists(ori_file_path_name)):
count_video += 1
# 按有效视频数,从指定行开始处理
if count_video >= 2401:
# 输出文件路径
result_file_path = './WebVid_withText/' + page_dir
# print('---------------------------------------')
# print(result_file_path)
# 运行后保存的文件路径及文件名
result_file_path_name = result_file_path + '/' + ori_file_name
# 读取视频
ori_video = cv2.VideoCapture(ori_file_path_name) # 原视频
# 获取视频帧数
video_frame_count = ori_video.get(cv2.CAP_PROP_FRAME_COUNT) # 帧数
# print('原视频帧数: ' + str(video_frame_count))
# 获取视频帧率
video_fps = ori_video.get(cv2.CAP_PROP_FPS) # 帧率
# print('原视频帧率: ' + str(video_fps))
# 获取视频时长
video_duration = duration # 时长
# 获取原视频编码格式
video_fourcc = int(ori_video.get(cv2.CAP_PROP_FOURCC))
video_fourcc_str = "".join([chr((video_fourcc >> 8 * i) & 0xFF) for i in range(4)]) # 将编码格式转换为四个字符的字符串
# print('原视频编码格式: ' + video_fourcc_str)
# 设置写入视频的编码格式
out_fourcc = cv2.VideoWriter_fourcc(*"mp4v")
#out_fourcc = cv2.VideoWriter_fourcc(*video_fourcc_str)
# 获取视频宽度
frame_width = int(ori_video.get(cv2.CAP_PROP_FRAME_WIDTH))
# 获取视频高度
frame_height = int(ori_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
text_length = len(name)
if not os.path.exists(result_file_path): # 检查路径是否存在
# print('文件夹不存在,创建')
os.makedirs(result_file_path) # 如果不存在,就创建路径
#
videoWriter = cv2.VideoWriter(result_file_path_name, # 文件名
out_fourcc, # 编码格式
video_fps, # 帧率
(frame_width, frame_height)) # (宽,高)
d_y = 25 # 纵向间距
text_content = 'duration:' + str(duration) + '\ntext_length:' + str(text_length) + '\ndescribe: ' + name # 写入内容
frame_id = 0
while (ori_video.isOpened()):
# print(frame_id)
frame_id = frame_id + 1
read_success, frame = ori_video.read() # read_success:布尔值 frame:读到的一帧图像数据
if read_success == True: # 这帧存在
# 文字坐标
word_x = int(frame_width / 40) # 文字x
word_y = int(frame_height / 20) # 文字y
for line in textwrap.wrap(text_content, width=60):
# if line in text_content.split("\n"): # 分割文本内容并遍历每一行
# word_y += d_y
cv2.putText(frame, # 帧
line, # 文本内容
(word_x, word_y), # 坐标
#cv2.FONT_HERSHEY_SIMPLEX, # 字体类型
cv2.FONT_ITALIC,
0.6, # 字体大小缩放因子
(55,255,155), # RGB
2) # 线条粗细
# cv2.putText(frame, # 帧
# line, # 文本内容
# (word_x, word_y+30), # 坐标
# #cv2.FONT_HERSHEY_SIMPLEX, # 字体类型
# cv2.FONT_ITALIC,
# 0.6, # 字体大小缩放因子
# (55,255,155), # RGB
# 1) # 线条粗细
# 更新下一行的位置
word_y += d_y
# 写入视频
videoWriter.write(frame)
else:
videoWriter.release()
# print(ori_file_name + ' processed successfully')
if count_video % 100 == 0:
print('video: ', count_video,' csv_row: ', count_row)
mid_time_new = time.perf_counter()
time_difference = mid_time_new - mid_time
mid_time = mid_time_new
mid_time = time.perf_counter()
print("Program running time: ", (mid_time - start_time) / 60, "min", '\nLast 100 Videos processing speed: ', time_difference / 100, ' s / video')
break
# #读取视频
# out_video = cv2.VideoCapture(result_file_path_name) # 输出的视频
# # 获取视频帧数
# out_video_frame_count = out_video.get(cv2.CAP_PROP_FRAME_COUNT) # 帧数
# # 获取视频帧率
# out_video_fps = out_video.get(cv2.CAP_PROP_FPS) # 帧率
# # print('输出视频帧率' + str(out_video_fps))
# # 获取视频时长
# if out_video_fps != 0:
# out_video_duration = out_video_frame_count / out_video_fps # 时长
# else:
# out_video_duration = 0
# print('输出视频', result_file_path_name, '帧率为0')
# # 获取视频编码格式
# out_fourcc = int(out_video.get(cv2.CAP_PROP_FOURCC))
# out_fourcc_str = "".join([chr((out_fourcc >> 8 * i) & 0xFF) for i in range(4)]) # 将编码格式转换为四个字符的字符串
# # print('输出视频编码格式: ' + str(out_fourcc_str))
#else:
# print('not exist')
print('Processed ', count_row, ' CSV lines in total')
print('Processed ', count_video, ' valid videos in total')
csvFile.close()
print("====================================CSV file processing complete====================================")
end_time = time.perf_counter()
print("program running time: ", (end_time - start_time)/60, "min")
# 计算csv文件中的总行数
# results = pd.read_csv('results_10M_train.csv')
# print(len(results)) #10727607 千万量级 10B
# for line in textwrap.wrap(text_content, width=80): # width参数表示每行的最大字符数
# cv2.putText(frame, line, (word_x, word_y), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255,0,0), 2)
# word_y += d_y # 更新下一行的位置