当前位置: 首页 > 工具软件 > MoviePy > 使用案例 >

Python合成音视频(分别使用ffmpeg和moviepy)

莘睿
2023-12-01

前言

1.合成音视频的应用场景

小伙伴们都知道,在许多时候,视频会以视频和音频两种格式分开存储,例如某站上面的视频,视频和音频是存储在一个window.__playinfo__中,对其作解析操作可取出url。这时候的视频和音频是分开的,这就需要对其进行合成操作。

"video": [{
				"id": 80,
				"baseUrl": "https://xy39x164x115x227xy.mcdn.bilivideo.cn:4483/upgcxcode/63/57/870665763/870665763_nb3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1667745950&gen=playurlv2&os=mcdn&oi=1863631061&trid=000033b695572c3240b58d854b2e9218b7dbu&mid=399048528&platform=pc&upsig=01bf329f48aff2db74478e56bbb2bd12&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=14000249&bvc=vod&nettype=0&orderid=0,3&agrr=0&bw=220064&logo=A0002000",
				"base_url": "https://xy39x164x115x227xy.mcdn.bilivideo.cn:4483/upgcxcode/63/57/870665763/870665763_nb3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1667745950&gen=playurlv2&os=mcdn&oi=1863631061&trid=000033b695572c3240b58d854b2e9218b7dbu&mid=399048528&platform=pc&upsig=01bf329f48aff2db74478e56bbb2bd12&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=14000249&bvc=vod&nettype=0&orderid=0,3&agrr=0&bw=220064&logo=A0002000",
				"backupUrl": ["https://upos-sz-mirrorali.bilivideo.com/upgcxcode/63/57/870665763/870665763_nb3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1667745950&gen=playurlv2&os=alibv&oi=1863631061&trid=33b695572c3240b58d854b2e9218b7dbu&mid=399048528&platform=pc&upsig=d70107a169ffc736b2735efb631638d9&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&agrr=0&bw=220064&logo=40000000", "https://upos-sz-mirrorali.bilivideo.com/upgcxcode/63/57/870665763/870665763_nb3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1667745950&gen=playurlv2&os=alibv&oi=1863631061&trid=33b695572c3240b58d854b2e9218b7dbu&mid=399048528&platform=pc&upsig=d70107a169ffc736b2735efb631638d9&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=2,3&agrr=0&bw=220064&logo=40000000"],
				"backup_url": ["https://upos-sz-mirrorali.bilivideo.com/upgcxcode/63/57/870665763/870665763_nb3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1667745950&gen=playurlv2&os=alibv&oi=1863631061&trid=33b695572c3240b58d854b2e9218b7dbu&mid=399048528&platform=pc&upsig=d70107a169ffc736b2735efb631638d9&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&agrr=0&bw=220064&logo=40000000", "https://upos-sz-mirrorali.bilivideo.com/upgcxcode/63/57/870665763/870665763_nb3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1667745950&gen=playurlv2&os=alibv&oi=1863631061&trid=33b695572c3240b58d854b2e9218b7dbu&mid=399048528&platform=pc&upsig=d70107a169ffc736b2735efb631638d9&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=2,3&agrr=0&bw=220064&logo=40000000"],
				

2.ffmpeg简介

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。它提供了录制、转换以及流化音视频的完整解决方案。

3.moviepy简介

moviePy是一个用于视频编辑Python库:切割、拼接、标题插入,视频合成,视频处理和自定义效果的创造。


一、使用ffmpeg

1.下载

直接在官网下载即可:官网链接

2.配置环境变量

这个大家应该都懂,在电脑属性中选择高级系统设置,选择环境变量,并将下载好的文件的bin目录路径添加到path中,就算配置成功了。

3.使用

ad = 'D:/'+title+'.aac'
vd = 'D:/'+title+'.mp4'
out = 'D:/video/'+title+'.mp4'
os.system(f'ffmpeg -i {vd} -i {ad} -c:v copy -c:a copy -bsf:a aac_adtstoasc {out}')


这里使用绝对路径,调用os中的方法执行cmd命令。
比较奇怪的是,相似功能的cmd命令语句还有许多,例如:

audio = ffmpeg.input(f'{title}.mp3')
     video = ffmpeg.input(f'{title}.mp4')
     print("合并视音频")
     out = ffmpeg.output(video, audio, f'下载目录/{title}.mp4')
     out.run()

但不知道什么原因,我在尝试的时候其他的方法都会失败,只能使用第一种,我猜测是因为ffmpeg版本问题。
合成成功:

开始合成视音频...
ffmpeg version N-108782-gff3c708686-20221022 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 12.1.0 (crosstool-NG 1.25.0.55_3defb7b)
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-shared --disable-static --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libkvazaar --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20221022
  libavutil      57. 39.101 / 57. 39.101
  libavcodec     59. 51.100 / 59. 51.100
  libavformat    59. 34.101 / 59. 34.101
  libavdevice    59.  8.101 / 59.  8.101
  libavfilter     8. 49.101 /  8. 49.101
  libswscale      6.  8.112 /  6.  8.112
  libswresample   4.  9.100 /  4.  9.100
  libpostproc    56.  7.100 / 56.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:/zhiboji/videoDownload/一杯拿铁赔一千块,没有铁怎么叫拿铁?【慧小媛】.mp4':
  Metadata:
    major_brand     : iso5
    minor_version   : 1
    compatible_brands: avc1iso5dsmsmsixdash
    encoder         : Lavf58.29.100
    description     : Packed by Bilibili XCoder v2.0.2
  Duration: 00:02:54.93, start: 0.000000, bitrate: 438 kb/s
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 852x480, 7 kb/s, 30 fps, 30 tbr, 16k tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'D:/zhiboji/videoDownload/一杯拿铁赔一千块,没有铁怎么叫拿铁?【慧小媛】.aac':
  Metadata:
    major_brand     : iso5
    minor_version   : 1
    compatible_brands: avc1iso5dsmsmsixdash
    encoder         : Lavf58.29.100
    description     : Packed by Bilibili XCoder v2.0.2
  Duration: 00:02:55.00, start: 0.000000, bitrate: 319 kb/s
  Stream #1:0[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 9 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
Output #0, mp4, to 'D:/zhiboji/videoDownload/video/一杯拿铁赔一千块,没有铁怎么叫拿铁?【慧小媛】.mp4':
  Metadata:
    major_brand     : iso5
    minor_version   : 1
    compatible_brands: avc1iso5dsmsmsixdash
    description     : Packed by Bilibili XCoder v2.0.2
    encoder         : Lavf59.34.101
  Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 852x480, q=2-31, 7 kb/s, 30 fps, 30 tbr, 16k tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 9 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 5248 fps=0.0 q=-1.0 Lsize=   16297kB time=00:02:54.97 bitrate= 763.0kbits/s speed=5.22e+03x    
video:9301kB audio:6780kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.350735%
合成成功!

如果不希望打印出日志信息,可以将os.system()改写为

import subprocess
subprocess.getoutput(cmd)

二、使用moviepy

1.导包

from moviepy.editor import *

2.使用

 # 获取下载好的音频和视频文件
 ad = AudioFileClip('D:/zhiboji/videoDownload/a.mp3')
 vd = VideoFileClip('D:/zhiboji/videoDownload/a.mp4')

 vd2 = vd.set_audio(ad)  # 将提取到的音频和视频文件进行合成
 vd2.write_videofile('D:/zhiboji/videoDownload/合唱视屏.mp4')  # 输出新的视频文件

注意:使用moviepy时可以发现,其效率与ffmpeg相比慢的不只是一点半点,用moviepy合成一个两分钟的视频可能需要耗时十分钟,在这里并不推荐。

三、总结

本文只介绍了ffmpeg和moviepy的一种用途,单纯用于合成视音频时前者的效率要远高于后者,当然这只是冰山一角,在接下来的时间里我也会努力学习更多相关知识。

 类似资料: