python给视频添加声音_Python做图像处理及视频音频文件分离和合成功能

一、简介

Imageio是一个Python库,提供了一个简单的界面来读取和写入各种图像数据,包括动画图像,视频,体积数据和科学格式。它是跨平台的,运行在Python 2.7和3.4+上,易于安装。

作为用户,您只需要记住一些功能:

imread()和imwrite() - 用于单个图像

mimread()和mimwrite() - 用于图像系列(动画)

volread()和volwrite() - 用于体积图像数据

get_reader()和get_writer() - 用于更多控制(例如流式传输)

有关更多信息,请参阅文档。

通过一系列功能简单的界面。

使用conda或pip 易于安装。

几乎没有依赖(只有Numpy和Pillow)。

纯Python,运行在Python 2.7,3.4 +和Pypy上

跨平台,在Windows,Linux,OS X上运行(Raspberry Pi计划)

许多支持的格式。

可以读取文件名,文件对象,zip文件,http / ftp和原始字节。

使用插件轻松扩展。

通过许多测试和持​​续集成来维护代码质量。

Imageio有一个相对简单的核心,为不同的文件格式提供通用接口。这个核心负责从不同的源(如http)读取,并为插件公开一个简单的API来访问原始数据。所有文件格式都在插件中实现。可以轻松注册其他插件。

一些插件依赖于外部库(例如ffmpeg)。Imageio提供了一种通过一个函数调用下载这些函数的方法,并在需要时提示用户这样做。下载缓存在您的appdata目录中,这可以保持imageio轻松和可扩展。

Imageio提供各种图像格式,包括科学格式。任何有关实现更多格式的帮助都非常受欢迎!

代码库遵循PEP8样式指南的(子集)。我们力求最大的测试覆盖率(核心为100%,每个插件为> 95%)。

二、代码总结:

import subprocess

import imageio

import os

from PIL import Image

def video2mp3(file_name):

"""

将视频转为音频

:param file_name: 传入视频文件的路径

:return:

"""

outfile_name = file_name.split('.')[0] + '.mp3'

subprocess.call('ffmpeg -i ' + file_name

+ ' -f mp3 ' + outfile_name, shell=True)

def video_add_mp3(file_name, mp3_file):

"""

视频添加音频

:param file_name: 传入视频文件的路径

:param mp3_file: 传入音频文件的路径

:return:

"""

outfile_name = file_name.split('.')[0] + '-txt.mp4'

subprocess.call('ffmpeg -i ' + file_name

+ ' -i ' + mp3_file + ' -strict -2 -f mp4 '

+ outfile_name, shell=True)

def compose_gif(file_path):

"""

将静态图片转为gif动图

:param file_path: 传入图片的目录的路径

:return:

"""

img_paths = sorted([int(p[3:-4]) for p in os.listdir(file_path) if os.path.splitext(p)[1] == ".png"])

img_paths = img_paths[:int(len(img_paths) / 3.6)]

gif_images = []

for path in img_paths:

gif_images.append(imageio.imread('{0}/out{1}.png'.format(file_path, path)))

imageio.mimsave("test.gif", gif_images, fps=30)

def compress_png(file_path):

"""

将gif动图转为每张静态图片

:param file_path: 传入gif文件的路径

:return:

"""

img_paths = [p for p in os.listdir(file_path) if os.path.splitext(p)[1] == ".png"]

for filename in img_paths:

with Image.open('{0}/{1}'.format(file_path, filename)) as im:

width, height = im.size

new_width = 150

new_height = int(new_width * height * 1.0 / width)

resized_im = im.resize((new_width, new_height))

output_filename = filename

resized_im.save('{0}/{1}'.format(file_path, output_filename))

if __name__ == '__main__':

# video2mp3(file_name='data-a.mp4')

video_add_mp3(file_name='swap-data-a.mp4', mp3_file='data-a.mp3')

# compose_gif(file_path='merged')

# compress_png(file_path='merged')

到此这篇关于Python做图像处理及视频音频文件分离和合成功能的文章就介绍到这了,更多相关python视频音频分离和合成内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

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、付费专栏及课程。

余额充值