Last active
June 17, 2020 14:09
-
-
Save shi3z/698a92a12184ebff934a227ba7a0045a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from PIL import Image | |
import moviepy.editor as mp | |
from pydub import AudioSegment | |
import cv2 | |
from tqdm import tqdm | |
video_file = 'input.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') | |
vw = cv2.VideoWriter('result.mp4', fourcc, frame_rate, (width, height)) | |
images_dir='img' | |
print(frame_count,frame_rate,audio_rate) | |
sound_out = AudioSegment.empty() | |
cnt=0 | |
for i in tqdm(range(0, frame_count)): | |
is_read, frame = video.read() | |
flagment = sound_in[i*audio_rate:(i+1)*audio_rate] | |
if flagment.max > 500: | |
cnt=2 | |
if cnt>0: | |
cnt-=1 | |
sound_out += flagment | |
vw.write(frame) | |
sound_out.export("result.wav", format="wav") | |
import subprocess | |
subprocess.call("which ffmpeg".split()) | |
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