Last active
April 13, 2020 14:46
-
-
Save jw910731/f200baaca335527c1004d908e325223b to your computer and use it in GitHub Desktop.
Auto Run FFmpeg
This file contains hidden or 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 os | |
| import sys | |
| import subprocess | |
| def list_dir(folder): | |
| return [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))] | |
| class Runner(object): | |
| def __init__(self, bin: str): | |
| self.bin = bin | |
| def run(self, args: list): | |
| subprocess.call([self.bin] + args) | |
| class Converter(object): | |
| def __init__(self, ffmpeg_bin, sox_bin, noise_prof, tmp_dir): | |
| self.ffmpeg = Runner(ffmpeg_bin) | |
| self.sox = Runner(sox_bin) | |
| self.tmp = tmp_dir | |
| self.noise_prof = noise_prof | |
| def split(self, fin): | |
| file = os.path.split(fin)[1] | |
| filename = os.path.splitext(file)[0] | |
| print("{} -> {}".format(fin, os.path.join(self.tmp, "tmpvid{}.mp4".format(filename)))) | |
| self.ffmpeg.run(["-i", fin, "-vcodec", "copy", "-an", os.path.join(self.tmp, "tmpvid{}.mp4".format(filename))]) | |
| self.ffmpeg.run(["-i", fin, "-acodec", "pcm_s16le", "-ar", "128k", "-vn", os.path.join(self.tmp, "tmpaud{}.wav".format(filename))]) | |
| def merge(self, fin, fout): | |
| file = os.path.split(fin)[1] | |
| filename = os.path.splitext(file)[0] | |
| self.ffmpeg.run(["-i", os.path.join(self.tmp, "tmpvid{}.mp4".format(filename)), "-i", os.path.join(self.tmp, "tmpaud-clean{}.wav".format(filename)), "-map", "0:v", "-map", "1:a", "-c:v", "copy", "-c:a", "aac", "-b:a", "128k", fout]) | |
| def denoise(self, fin): | |
| file = os.path.split(fin)[1] | |
| filename = os.path.splitext(file)[0] | |
| self.sox.run([os.path.join(self.tmp, "tmpaud{}.wav".format(filename)), os.path.join(self.tmp, "tmpaud-clean{}.wav".format(filename)), "noisered", self.noise_prof, "0.005"]) | |
| ''' | |
| args: | |
| fin: full path of in file | |
| fout: full path of out file | |
| ''' | |
| def run(self, fin, fout): | |
| self.split(fin) | |
| self.denoise(fin) | |
| self.merge(fin, fout) | |
| class Controller(object): | |
| def __init__(self, src, dest, noise_prof): | |
| self.src_folder = src | |
| self.dest_folder = dest | |
| # setup converter | |
| ffmpeg_path = os.getenv("FFMPEG_PATH") | |
| sox_path = os.getenv("SOX_PATH") | |
| if ffmpeg_path is None: | |
| ffmpeg_path = """C:\\Users\\輔\\Desktop\\模擬面試錄影\\ffmpeg-4.2.2-win64-static\\bin\\ffmpeg.exe""" | |
| if sox_path is None: | |
| sox_path = """C:\\Program Files (x86)\\sox-14-4-2\\sox.exe""" | |
| self.converter = Converter(ffmpeg_path, sox_path, noise_prof, os.path.realpath("tmp")) | |
| def init(self): | |
| # create tmp folder | |
| if not os.path.exists("tmp"): | |
| os.makedirs("tmp") | |
| if os.listdir(self.dest_folder): | |
| raise "Destination Folder Not Empty" | |
| def loop(self): | |
| self.init() | |
| file_list = list_dir(self.src_folder) | |
| for v in file_list : | |
| self.converter.run(os.path.join(self.src_folder, v), os.path.join(self.dest_folder, v)) | |
| def main(): | |
| if len(sys.argv) < 4 : | |
| print("arg list: <src folder> <dest folder> <noise prof>", file=sys.stderr) | |
| sys.exit(1) | |
| con = Controller(sys.argv[1], sys.argv[2], sys.argv[3]) | |
| con.loop() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment