Python 音频视频分离

通过脚本把视频的音频和视频分离存储到指定目录文件夹下

加入多线程提高处理速度

import os
import subprocess
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm

# 定义输入和输出目录
input_directory = r'G:\day'
output_directory = r'G:\day\mp4_no_audio'
audio_output_directory = r'G:\day\audio'

# 确保输出目录存在
os.makedirs(output_directory, exist_ok=True)
os.makedirs(audio_output_directory, exist_ok=True)

# 获取所有MP4文件
files = [f for f in os.listdir(input_directory) if f.endswith('.mp4')]

def process_file(filename, pbar):
    input_file = os.path.join(input_directory, filename)
    output_video_file = os.path.join(output_directory, filename)
    output_audio_file = os.path.join(audio_output_directory, f"{os.path.splitext(filename)[0]}.mp3")

    # 构建FFmpeg命令分离音频
    command_audio = ['ffmpeg', '-i', input_file, '-q:a', '0', '-map', 'a', output_audio_file]
    subprocess.run(command_audio, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

    # 构建FFmpeg命令分离视频
    command_video = ['ffmpeg', '-i', input_file, '-an', output_video_file]
    subprocess.run(command_video, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

    # 更新进度条
    pbar.update(1)

# 使用多线程处理文件并显示进度条
with ThreadPoolExecutor(max_workers=4) as executor:
    with tqdm(total=len(files)) as pbar:
        futures = [executor.submit(process_file, filename, pbar) for filename in files]
        for future in futures:
            future.result()  # 确保捕获所有异常

print("批量处理完成!")

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ReadMe Release Version beta_1.0 index.py imageMatlab.py This is more or less a wrapper for Matplotlib imaging functions such that their behavior is equivalent, in terms of colormap, aspect and so forth, to the expected behavior of Matlab's functions. sepVocal.py This script can be used to execute the desired separation. See below for an example of use of this file. SIMM.py This script implements the actual algorithm for parameter estimation. It is mainly used by sepVocal.py. tracking.py The Viterbi decoding algorithm is implemented in this script. Requirements: These scripts have been tested with Python 2.7, The packages that are required to run the scripts are pydub,ffmepg, Numpy, Spicy, Matplotlib. One can respectively find the latest versions at the following addresses: http://pydub.com/ https://ffmpeg.org http://numpy.org/ http://scipy.org/ http://matplotlib.sourceforge.net/ Notes: Prefer recent versions of the above packages, in order to avoid compatibility issues, notably for Matplotlib. Note that this latter package is not necessary for the program to run, although you might want to watch a bit what is happening! Spicy should be version 0.8+, since we use its io.wavefile module to read the wave files. We once used the audio lab module, but it would seem that it is a bit more complicated to install (with the benefit that many more file formats are allowed). Usage: The easy way to use these scripts is to run the exec package of our release version: http://www.github.com/beata_1.0 for more develop: you can run the index.py on pycharm directly. note: the output files will create under you source wav file. ContactMe Email:xlzhang14@fudan.edu.cn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值