本文整理了我在学习和工作中用到的实用python脚本,希望也能帮助到需要的小伙伴~
文章目录
视频格式转换
- 安装视频处理库moviepy
pip install moviepy
- 安装FFmpeg(FFmpeg是一个开源的多媒体框架,moviepy库依赖于它来处理视频文件),下载参考流程:Mac版 下载安装FFmpeg
- 设置IMAGEIO_FFMPEG_EXE环境变量:如果您已经安装了FFmpeg,可以设置IMAGEIO_FFMPEG_EXE环境变量来指定FFmpeg可执行文件的路径。在命令行中执行以下命令:
对于Windows用户:set IMAGEIO_FFMPEG_EXE=path/to/ffmpeg.exe
对于Mac或Linux用户:export IMAGEIO_FFMPEG_EXE=/path/to/ffmpeg
请确保将"path/to/ffmpeg.exe"或"/path/to/ffmpeg"替换为您实际安装的FFmpeg可执行文件的路径。
- 编写代码(avi → mp4 为例)
from moviepy.editor import VideoFileClip
def convert_avi_to_mp4(input_file, output_file):
video = VideoFileClip(input_file)
video.write_videofile(output_file, codec='libx264')
input_file = 'input.avi'
output_file = 'output.mp4'
convert_avi_to_mp4(input_file, output_file)
- 运行以上脚本,即可将avi格式的视频转换为mp4格式的视频