Skip to content

Instantly share code, notes, and snippets.

@shi3z
Created April 26, 2020 02:35
Show Gist options
  • Save shi3z/3411f2e9bf5096e57ee5467c0754f655 to your computer and use it in GitHub Desktop.
Save shi3z/3411f2e9bf5096e57ee5467c0754f655 to your computer and use it in GitHub Desktop.
#高速化のためのトライアル
import numpy as np
from PIL import Image
import moviepy.editor as mp
from pydub import AudioSegment
import cv2
from tqdm import tqdm
import time
video_file = 'ito.mp4'
video = cv2.VideoCapture(video_file)
clip_input = mp.VideoFileClip(video_file)
#clip_input.audio.write_audiofile('audio.mp3')
sound_in = AudioSegment.from_file('audio.mp3', format="mp3")
frame_count = int(video.get(7))
frame_rate = int(video.get(5))
audio_rate = 1000 / frame_rate
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
out_videofilename ='output.mp4'
# Set output video infomation.
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#fourcc = cv2.VideoWriter_fourcc(*'MP4S')
vw = cv2.VideoWriter('result.mp4', fourcc, frame_rate, (width, height))
images_dir='img'
print(frame_count,frame_rate,audio_rate)
frame_count= frame_rate*50
#sounddata = sound.get_array_of_samples()
#print(sounddata)
sound_out = AudioSegment.empty()
volume=0
cnt=0
t1 = time.time()
buf = np.zeros(frame_count)
for i in tqdm(range(0, frame_count)):
#is_read, frame = video.read()
#cv2.imwrite(images_dir+ "/" + str(i // frame_rate).zfill(6) + ".png", frame)
flagment = sound_in[i*audio_rate:(i+1)*audio_rate]
buf[i]=flagment.max
if flagment.max > 500:
cnt=2
if cnt>0:
cnt-=1
sound_out += flagment
#vw.write(frame)
t2 = time.time()
elapsed_time = t2-t1
print("elapsed time %04.04f"%elapsed_time)
#multi thread counting
import threading
t1 = time.time()
buf = np.zeros(frame_count)
flm = dict()
print(buf)
workers = 10
end = 0
def counter(idx,len):
global buf,end
for idx in tqdm(range(i*len,i*len+len)):
f = sound_in[idx*audio_rate:(idx+1)*audio_rate]
buf[idx] = f.max
flm[idx] = f
end += 1
for i in tqdm(range(0, workers)):
threading.Thread(target=counter, args=(i, frame_count//workers)).start()
t2 = time.time()
elapsed_time = t2-t1
print("-----")
print(buf)
while end<workers:
print(end)
time.sleep(1)
print("elapsed time %04.04f"%elapsed_time)
for i in tqdm(range(0, frame_count)):
is_read, frame = video.read()
#cv2.imwrite(images_dir+ "/" + str(i // frame_rate).zfill(6) + ".png", frame)
flagment = sound_in[i*audio_rate:(i+1)*audio_rate]
if buf[i] > 500:
cnt=2
if cnt>0:
cnt-=1
sound_out += flm[i]
vw.write(frame)
sound_out.export("result.wav", format="wav")
import subprocess
subprocess.call("ffmpeg -i result.mp4 -i result.wav -c:v copy -c:a aac output.mp4".split())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment