原文件结构
solo11
└─videos
├─accordion
│ ├─0N26WnKiCIg.mp4
│ ├─1LnKd5cuqIc.mp4
│ ├─1mCi6eWXVbg.mp4
│ └─_jPFkOkNjuo.mp4
└─acoustic_guitar
├─1OsOEt5Qpfc.mp4
├─1TBxNBIUx3o.mp4
├─1XfdURNeY6s.mp4
└─_IcBMTI8PEQ.mp4
分割视频,存储到video文件夹下
import glob
import os
import subprocess
def split_video(input_file, output_folder, duration):
"""
input_file | input video location
output_folder | output video location
"""
output_folder = os.path.splitext(output_folder)[0]
parts = output_folder.split('/')
output_folder = '/'.join(parts[:-1])
print(output_folder)
# print('parts',parts)
id = parts[-1]
if not os.path.exists(output_folder):
os.makedirs(output_folder)
command = ['ffmpeg', '-i', input_file, '-c', 'copy', '-f', 'segment', '-reset_timestamps', '1', '-segment_time',
str(duration), '-map', '0', os.path.join(output_folder , id + '_%02d.mp4')]
subprocess.call(command)
################################
# cut mp4 files into 10s clip #
################################
if __name__ == '__main__':
# input_file = r'E:\video\0yR5s-CSw4E.mp4'
duration = 10 # 单位是秒,这里设置每个片段的时长为10秒钟
input_folder = r"E:/MUSIC/solo11/videos"
# output_folder = r"E:/MUSIC/solo11/video"
lst = os.listdir(input_folder)
for file in lst:
#for input_video in glob.glob(os.path.join(input_folder+'/'+file, "*.mp4")):
for input_video in glob.glob(input_folder+'/'+file+'/'+ "*.mp4"):
input_video = input_video.replace('\\', '/')
output_folder = input_video.replace('videos', 'video')
# print(input_video)
split_video(input_video,output_folder, duration)
抽帧并提取音频,删除10s的视频片段
import glob
import os
import subprocess
def getframes(input_file):
"""
output_folder | output_frames' location, each video clip has a folder named with their name
input_file | the location of input videos
"""
output_folder = os.path.splitext(input_file)[0] # 删掉.mp4后缀
output_folder = output_folder.replace('video', 'frame')
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# print(output_folder)
# 抽帧并保存为图像
command = ['ffmpeg', '-i', input_file, '-vf', 'fps=8', os.path.join(output_folder, '%06d.png')]
subprocess.check_output(command)
def getmusic(input_file):
output_folder = os.path.splitext(input_file)[0]
parts = output_folder.split('/')
id = parts[-1]
print(id)
output_folder = '/'.join(parts[:-1])
output_folder = output_folder.replace('video', 'audio')
if not os.path.exists(output_folder):
os.makedirs(output_folder)
print(output_folder)
mp3_files = []
# 提取音频文件
command = ['ffmpeg', '-i', input_file, '-vn', '-f', 'mp3', os.path.join(output_folder, id + '.mp3')]
subprocess.check_output(command)
# 删除 MP4 文件
# os.remove(input_file)
########################################
# extract audios and get video frames #
########################################
if __name__ == '__main__':
input_folder = r"E:/MUSIC/solo11/video"
# mp4_files = glob.glob(os.path.join(input_folder, "*.mp4"))
lst = os.listdir(input_folder)
print(lst)
mp3_files = []
for file in lst:
# print(file)
mp4_files = glob.glob(os.path.join(input_folder + '/' + file, "*.mp4"))
# print(mp4_files)
for mp4_file in mp4_files:
mp4_file = mp4_file.replace('\\', '/')
# print(mp4_file)
getframes(mp4_file)
getmusic(mp4_file)
从文件中提取.mp4文件到指定文件夹
import os
import json
import shutil
if __name__ == '__main__':
################################
# move video to des path #
################################
solo_11classes_file = 'E:/study/bishe/Sound-of-Pixels-master/MUSIC_dataset-master/MUSIC_solo_videos.json'
with open(solo_11classes_file, "r") as f:
load_solo11_dict = json.load(f)
# move video to des path
for i, instr_name in enumerate(load_solo11_dict['videos'].keys()):
for j, yt_id in enumerate(load_solo11_dict['videos'][instr_name]):
loc_video = 'E:/MUSIC/solo11/' \
'frames_all/' + instr_name + '/' + yt_id + '.mp4/' + yt_id
des_video = 'E:/MUSIC/solo11/' \
'video/' + instr_name + '/'
if not os.path.exists(des_video):
os.makedirs(des_video)
print(des_video+'创建成功')
else:
print(des_video+'已经存在')
if os.path.exists(loc_video + '.mp4'):
shutil.move(loc_video + '.mp4', des_video) # 移动文件到目标路径
print('Videos have been moved!')
得到的文件结构
solo11
├─audio
│ ├─acoustic_guitar
│ │ ├─1OsOEt5Qpfc_00.mp3
│ │ ├─1OsOEt5Qpfc_01.mp3
│ │ ├─ ...
│ │ ├─_IcBMTI8PEQ_01.mp3
│ │ ├─...
│ │ └─_IcBMTI8PEQ_05.mp3
│ └─accordion
│ ├─0N26WnKiCIg_00.mp3
│ └─...
├─frame
│ ├─acoustic_guitar
│ │ ├─1OsOEt5Qpfc_00(下面是图片,命名为00000x.jpg)
│ │ ├─1OsOEt5Qpfc_01
│ │ ├─...
│ │ ├─_IcBMTI8PEQ_01
│ │ ├─...
│ │ └─_IcBMTI8PEQ_05
│ └─accordion
│ ├─0N26WnKiCIg_00
│ └─...
├─video(10s视频片段后被删除)
│ ├─acoustic_guitar
│ │ ├─1OsOEt5Qpfc_00.mp4
│ │ ├─1OsOEt5Qpfc_01.mp4
│ │ ├─ ...
│ │ ├─_IcBMTI8PEQ_01.mp4
│ │ ├─...
│ │ └─_IcBMTI8PEQ_05.mp4
│ └─accordion
│ ├─0N26WnKiCIg_00.mp4
│ └─...
└─videos
├─accordion
└─acoustic_guitar
以上代码在Windows下测试通过,但是Linux下报错
提取音频代码报错:
subprocess.CalledProcessError: Command '['ffmpeg', '-i', '/home/zy/mydata/MUSIC/MUSIC/videos/solo_11classes/violin/85KcVDCQLfc.mp4', '-vn', '-f', 'mp3', '/home/zy/mydata/MUSIC/MUSIC/audioss/solo_11classes/violin/85KcVDCQLfc.mp3']' returned non-zero exit status 1.
command后使用subprocess.call(command),如下图所示
修改后还是有问题,音频文件未生成
Automatic encoder selection failed for output stream #0:0. Default encoder for format mp3 (codec mp3) is probably disabled. Please choose an encoder manually. Error selecting an encoder for stream 0:0
断电了,明天再看