python ffmpeg 视频转图片 视频转音频 播放音频 多张图片+音频转视频 多个视频合成一个视频 改变视频播放速度

视频转图片

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce :
@File      : video2image.py
@Time      : 2020-11-13 11:06
@Author    : xia hua dong
@Tel       : 173 179 76823
@Email     : 17317976823@163.com
@pip       : pip install 
"""

import os

def video2image(ffmpegPath, videoPath, frameNumber, imgPath, imgFormat):
    """
    视频转图片(请使用绝对路径)
    :param ffmpegPath: ffmpeg.exe路径
    :param videoPath: 视频路径,可以是mp4或avi等
    :param frameNumber: 每秒提取的图片数,即帧数
    :param imgPath: 输出图片存放的文件夹
    :param imgFormat: 输出图片格式,可以是jpg、png或jepg等
    :return: 多张图片
    """
    if not os.path.exists(imgPath):  # 如果图片文件夹不存在
        os.mkdir(imgPath)  # 创建图片文件夹路径
    command1 = "{} -i {} -r {} {}\%d .{} -y".format(ffmpegPath, videoPath, frameNumber, imgPath, imgFormat)
    os.system(command1)


if __name__ == '__main__':
    ffmpegPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_front_back_end\ffmpey安装包\ffmpeg-20200628-4cfcfb3-win64-static\ffmpeg-20200628-4cfcfb3-win64-static\bin\ffmpeg"
    imgPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\img2"  # 图片文件夹路径
    videoPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\video\1.mp4"
    frameNumber = 30
    imgFormat = "png"
    video2image(ffmpegPath, videoPath, frameNumber, imgPath, imgFormat)
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce : 视频转图片
@File      : video2audio.py
@Time      : 2020-11-13 14:13
@Author    : xia hua dong
@Tel       : 173 179 76823
@Email     : 17317976823@163.com
@pip       : pip install ffmpy
"""

from ffmpy import FFmpeg

ff = FFmpeg(
    # ffmpeg.exe的路径
    executable=r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_front_back_end\ffmpey安装包\ffmpeg-20200628-4cfcfb3-win64-static\ffmpeg-20200628-4cfcfb3-win64-static\bin\ffmpeg",
    inputs={'video/1.mp4': None},  # 视频路径
    outputs={'img2/%d.png': None}  # 音频路径 -ar 采样率 -y 覆盖同名文件
)
# print(ff.cmd)
ff.run()

-i 输入
./video/face/w1.mp4 : 输入视频路径
-r 25 :1秒25张
./w1/image-%3d.jpeg:输出图片路径

视频转音频

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce : 视频转音频
@File      : video2audio.py
@Time      : 2020-11-13 11:06
@Author    : xia hua dong
@Tel       : 173 179 76823
@Email     : 17317976823@163.com
@pip       : pip install 
"""

import os


def video2audio(ffmpegPath, videoPath, sampleRate, audioPath):
    """
    视频转音频(请使用绝对路径)
    :param ffmpegPath: ffmpeg.exe路径
    :param videoPath: 视频路径,可以是mp4或avi等
    :param audioFormat: 音频格式
    :param sampleRate: 采样率
    :param audioPath: 音频保存路径,可以是mp3或wav
    :return:
    """
    command1 = "{} -i {} -ar {} {} -y".format(ffmpegPath, videoPath, sampleRate, audioPath)
    os.system(command1)


if __name__ == '__main__':
    ffmpegPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_front_back_end\ffmpey安装包\ffmpeg-20200628-4cfcfb3-win64-static\ffmpeg-20200628-4cfcfb3-win64-static\bin\ffmpeg"
    videoPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\video\1.mp4"
    sampleRate = 16000
    audioPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\1.wav"
    video2audio(ffmpegPath, videoPath, sampleRate, audioPath)
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce : 视频转音频
@File      : video2audio.py
@Time      : 2020-11-13 14:13
@Author    : xia hua dong
@Tel       : 173 179 76823
@Email     : 17317976823@163.com
@pip       : pip install ffmpy
"""

from ffmpy import FFmpeg

ff = FFmpeg(
    # ffmpeg.exe的路径
    executable=r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_front_back_end\ffmpey安装包\ffmpeg-20200628-4cfcfb3-win64-static\ffmpeg-20200628-4cfcfb3-win64-static\bin\ffmpeg",
    inputs={'video/1.mp4': None},  # 视频路径
    outputs={'1.wav': '-ar 16000 -y'}  # 音频路径 -ar 采样率 -y 覆盖同名文件
)
# print(ff.cmd)
ff.run()

-ar 16000:采样率为16K

播放音频

import os
def playAudio(path):
    """ playAudio:播放音频
        path:音频路径"""
    os.system("ffplay %s" % path)

playAudio("./w1/w1.mp3")

图片+音频 转 视频

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce : 图片+音频 转 视频 (请使用绝对路径)
@File      : imageAudio2video.py
@Time      : 2020-11-13 15:33
@Author    : xia hua dong
@Tel       : 173 179 76823
@Email     : 17317976823@163.com
@pip       : pip install 
"""

import os


def imageAudio2video(ffmpegPath, frameNumber, imgPath, audioPath, videoPath):
    """
    图片+音频 转 视频 (请使用绝对路径)
    :param ffmpegPath: ffmpeg.exe路径
    :param frameNumber: 每秒提取的图片数,即帧数
    :param imgPath: 图片路径
    :param audioPath: 音频路径
    :param videoPath: 视频路径
    :return: 视频
    """
    "ffmpeg - r 30 - i img2 / % d.png - i 1.wav test.mp4 - y"
    command1 = "{} -r {} -i {} -i {} {} -y".format(ffmpegPath, frameNumber, imgPath, audioPath, videoPath)
    os.system(command1)


if __name__ == '__main__':
    ffmpegPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_front_back_end\ffmpey安装包\ffmpeg-20200628-4cfcfb3-win64-static\ffmpeg-20200628-4cfcfb3-win64-static\bin\ffmpeg"
    frameNumber = 30
    imgPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\img2\%d.png"  # 图片文件夹路径
    audioPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\1.wav"
    videoPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\video\output.mp4"
    imageAudio2video(ffmpegPath, frameNumber, imgPath, audioPath, videoPath)

多个视频合成一个视频

FFmpeg concat 分离器
注意 : FFmpeg 1.1 以上版本。先创建一个文本文件filelist.txt:

file ‘input.mp4’
file ‘1.mp4’
file ‘2.mp4’

然后

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce : 多视频合并成同一视频 (请使用绝对路径)
@File      : mergeVideo.py
@Time      : 2020-11-13 15:33
@Author    : xia hua dong
@Tel       : 173 179 76823
@Email     : 17317976823@163.com
@pip       : pip install 
"""

import os


def mergeVideo(ffmpegPath, txtPath, videoPath):
    """
    多视频合并成同一视频 (请使用绝对路径)
    :param ffmpegPath: ffmpeg.exe路径V
    :param txtPath: 多视频输入路径写入txt
    :param videoPath: 视频路径
    :return: 视频
    """
    command1 = "{} -f concat -i {} -c copy {} -y".format(ffmpegPath, txtPath, videoPath)
    os.system(command1)


if __name__ == '__main__':
    import time

    # 时间测试
    start = time.time()
    ffmpegPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_front_back_end\ffmpey安装包\ffmpeg-20200628-4cfcfb3-win64-static\ffmpeg-20200628-4cfcfb3-win64-static\bin\ffmpeg"
    txtPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\video\filelist.txt"
    videoPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\video\merge.mp4"
    mergeVideo(ffmpegPath, txtPath, videoPath)
    end = time.time()
    print('运行时间: {} 秒'.format(end - start))

改变视频播放速度

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Introduce : 改变视频播放速度(注意:如果视频中有声音,声音速度不会改变)
@File      : speedVideo.py
@Time      : 2020-11-13 15:33
@Author    : xia hua dong
@Tel       : 173 179 76823
@Email     : 17317976823@163.com
@pip       : pip install 
"""

import os


def speedVideo(ffmpegPath, inputVideoPath, speed, outputVideoPath):
    """
    改变视频播放速度(注意:如果视频中有声音,声音速度不会改变)
    :param ffmpegPath:  ffmpeg.exe的路径
    :param inputVideoPath: 输入视频路径
    :param speed: 输出速度:输入速度
    :param outputVideoPath: 输出视频速度
    :return:
    """
    command1 = '{} -i {} -filter:v "setpts= {}*PTS" {} -y'.format(ffmpegPath, inputVideoPath, speed, outputVideoPath)
    os.system(command1)


if __name__ == '__main__':
    ffmpegPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_front_back_end\ffmpey安装包\ffmpeg-20200628-4cfcfb3-win64-static\ffmpeg-20200628-4cfcfb3-win64-static\bin\ffmpeg"
    inputVideoPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\video\merge.mp4"
    speed = 0.5
    outputVideoPath = r"C:\Users\xiahuadong\Desktop\xiahuadongCode\number_humen_web\number_humen_web\video\speed.mp4"
    speedVideo(ffmpegPath, inputVideoPath, speed, outputVideoPath)


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏华东的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值