import argparse
import os
import os.path
from shutil import rmtree, move
import random
from glob import glob
import time
# For parsing commandline arguments
parser = argparse.ArgumentParser()
# parser.add_argument("--ffmpeg_dir", type=str, default="/home/lcj/ffmpeg-4.1/", required=False, help='path to ffmpeg.exe')
parser.add_argument("--ffmpeg_dir", type=str, default="/home/zrway/TempNew/SDK发布/transcoding_v1.0.5/test/", required=False, help='path to ffmpeg.exe')
parser.add_argument("--dataset", type=str, default="UCF-101",
help='specify if using "UCF-101" or "custom" video dataset')
parser.add_argument("--videos_folder", type=str, default="./UCF-101/", required=False,
help='path to the folder containing videos')
parser.add_argument("--dataset_folder", type=str, default="./ucf_clips/", required=False,
help='path to the output dataset folder')
args = parser.parse_args()
def extract_frames(videos, inDir, outDir):
cnt = 0 # videos number
for video in videos[:-1]:
# os.mkdir(os.path.join(outDir, os.path.splitext(video)[0]))
cnt += 1
# print('{} -loglevel error -i {} -vsync 0 -qscale:v 2 -f image2 {}/%07d.jpg'.format(os.path.join(args.ffmpeg_dir,"ffmpeg"), video, outDir))
# i = os.path.splitext(video)
# o = os.path.splitext(video)[-2]
subdir = os.path.join(outDir, '{}'.format(cnt)) # create sub folders to store the extracted frames
if not os.path.exists(subdir):
os.mkdir(subdir)
start = time.time()
retn = os.system('{} -loglevel error -i {} -vsync 0 -qscale:v 2 -f image2 {}/%04d.png'.format(os.path.join(args.ffmpeg_dir,"ffmpeg"), video, os.path.join(outDir, '{}'.format(cnt))) )
print('processed videos: ', cnt )
cost = time.time() - start
remain_time = round((len(videos)-1-cnt)*cost/60,2)
remain_h = round(remain_time/60,2)
# print('remain time: {} min'.format(remain_time))
print('remain time: {} hours'.format(remain_h))
# 使用os.system()执行ffmpeg命令
# -vf video filter
if retn:
print("Error converting file:{}. Exiting.".format(video))
# os.system() success return 0;failed return 1
def create_clips(root, destination):
folderCounter = 0
# 初始化为0 进入循环置1 开始累加
files = os.listdir(root)
# filedirs list
# Iterate over each folder containing extracted video frames.
# for file in files:
# images = sorted(os.listdir(os.path.join(root, file)))
#
# for imageCounter, image in enumerate(images):
# # for index索引, item元素 in enumerate (list列表):
# # Bunch images in groups of 3 frames
#
# if (imageCounter % 6 == 0):
# if (imageCounter + 5 >= len(images)):
# break
# folderCounter += 1
# os.mkdir("{}/{}".format(destination, folderCounter+1))
#
# if imageCounter%2 == 0:
# image
# c = imageCounter%3 + 1
# move("{}/{}/{}".format(root, file, image), "{}/{}/frame_{}.png".format(destination, folderCounter+1, imageCounter%3+1))
# imageCounter += 1
# Iterate over each folder containing extracted video frames.
file_cnt = 0
for file in files:
images = sorted(os.listdir(os.path.join(root, file)))
# print some info
print('[*] now processing file:',file)
file_cnt += 1
if file_cnt == 10:
print('clip files:',file_cnt)
if file_cnt%100 == 0:
print('clip files:',file_cnt)
# do the clip_op
for imageCounter, image in enumerate(images):
# for index索引, item元素 in enumerate (list列表):
# Bunch images in groups of 3 frames
if (imageCounter % 3 == 0):
if (imageCounter + 2 >= len(images)):
break
folderCounter += 1
#
os.mkdir("{}/{}".format(destination, folderCounter))
"""
new_folder = "{}/{}".format(destination, folderCounter)
if os.path.exists(new_folder):
continue
else:
os.mkdir(new_folder)
"""
move("{}/{}/{}".format(root, file, image), "{}/{}/frame_{}.png".format(destination, folderCounter, imageCounter%3+1))
# rmtree(os.path.join(root, file))
def get_txt(clipPath, frameIdx): # frameIdx mean the index 1,2,3 in frame_1.png
dirs = os.listdir(clipPath)
with open("ucf101_train_files_frame{}.txt".format(frameIdx), "w") as f:
for dir in sorted(dirs):
frame_name = '{}/frame_{}.png'.format(dir,frameIdx) # 1/frame_1.png
# b = frame_name.split('.',1)[0] # 1/frame_1
f.writelines(str(frame_name) + "\n")
# f.write(str(fold) + "\n")
def main():
"""
# Create dataset folder if it doesn't exist already.
if not os.path.isdir(args.dataset_folder):
os.mkdir(args.dataset_folder)
"""
extractPath = os.path.join(args.dataset_folder, "extracted")
clipPath = os.path.join(args.dataset_folder, "clips")
if not os.path.exists(extractPath):
os.mkdir(extractPath)
if not os.path.exists(clipPath):
os.mkdir(clipPath)
# if not os.path.exists(testPath):
# os.system("mkdir {}".format(testPath))
if (args.dataset == "UCF-101"):
"""
# f = open("video.txt", "r")
f = open("video.txt", "r")
videos = f.read().split('\n')
# videos = f.readlines()
print("videos_Number:", len(videos)-1)
print("extract frames now...")
# show the list of extracted videos
extract_frames(videos, args.videos_folder, extractPath)
"""
print("create clips now...")
create_clips(extractPath, clipPath)
print("clip done!")
else: # custom dataset
print("not UCF dataset! check the input ")
# rmtree(extractPath)
if __name__ == '__main__':
main()
clipPath = os.path.join(args.dataset_folder, "clips")
print('get txt!')
get_txt(clipPath, 1)
get_txt(clipPath, 2)
get_txt(clipPath, 3)
print('all done!')