-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
70 lines (55 loc) · 2.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import glob
import os
from subprocess import call
from config import parse_opts
config = parse_opts()
def generate_segments(input_video, dest_dir, seg_len):
cmd = 'ffmpeg -i '+ input_video +' -force_key_frames "expr:gte(t,n_forced*10)" -strict -2 -c:a aac -c:v libx264 -f segment -segment_list_type m3u8 -segment_list_size 0 -segment_time '+str(seg_len)+' -segment_time_delta 0.1 -segment_list ' +dest_dir+'out.m3u8 ' +dest_dir+'out%02d.ts'
print(cmd)
call(cmd,shell=True)
def vid2wav(input_video, dest_dir):
cmd = 'ffmpeg -i ' + input_video +' -ac 2 -f wav ' + dest_dir + '/input.wav'
print(cmd)
call(cmd,shell=True)
def wav2mp3(input_dir, dest_dir):
cmd = 'ffmpeg -i '+input_dir+'/input.wav -vn -ar 44100 -ac 2 -b:a 192k '+ dest_dir+'/input.mp3'
print(cmd)
call(cmd,shell=True)
def vid2frames(input_video, dest_dir):
cmd = 'ffmpeg -i ' + input_video +' -qscale:v 2 -vf fps=1 ' + dest_dir + '/02%d.jpg'
print(cmd)
call(cmd,shell=True)
def start_process(input_path, output_path):
for file_name in [input_path]:
video_list = glob.glob(file_name + '*.mp4')
for video in video_list:
print('-'*60)
print('File Name: ', video)
video_file_name = video.split('\\')[-1].split(".")[0]
main_video_path = os.path.join(output_path, video_file_name)
seg_path = os.path.join(main_video_path, 'segments')
audio_path = os.path.join(main_video_path, 'audio')
frames_path = os.path.join(main_video_path, 'frames')
thumbnail_path = os.path.join(main_video_path, 'thumbnails')
# Split the video into HLS segments
if not os.path.exists(seg_path):
print(seg_path)
os.makedirs(seg_path)
generate_segments(video, seg_path + '/', config.seg_len)
# Extract audio
if config.audio:
if not os.path.exists(audio_path):
print(audio_path)
os.makedirs(audio_path)
vid2wav(video, audio_path)
#comment if you don't want to convert to mp3
wav2mp3(audio_path, audio_path)
# Extract frames
if config.frames:
if not os.path.exists(frames_path):
print(frames_path)
os.makedirs(frames_path)
vid2frames(video, frames_path)
if __name__ == '__main__':
start_process(config.inp_path, config.out_path)
print('All process completed')