Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add Audio tools to Video by Python

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces Python how to add audio tools to the video, the article is very detailed, has a certain reference value, interested friends must read it!

Environmental dependence

For ffmpeg environment installation, please refer to: windows ffmpeg installation and deployment

Ffmpy installation:

Pip install ffmpy-I https://pypi.douban.com/simple main code

No nonsense, go to the code.

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2021-12-5 1V 4V @ Author: swordsman A Liang ALiang# @ Site: # @ File: video_add_audio_tool.py import osimport uuidfrom ffmpy import FFmpeg # Video add audio def video_add_audio (video_path: str, audio_path: str Output_dir: str): _ ext_video = os.path.basename (video_path). Strip (). Split ('.') [- 1] _ ext_audio = os.path.basename (audio_path). Strip (). Split ('.) [- 1] if _ ext_audio not in ['mp3' 'wav']: raise Exception (' audio format not support') _ codec = 'copy' if _ ext_audio = =' wav': _ codec = 'aac' result = os.path.join (output_dir,' {}. {} '.format (uuid.uuid4 (), _ ext_video)) ff = FFmpeg (inputs= {video_path: None, audio_path: None}) Outputs= {result:'- map 0 map v-map 1Parade a-c map v copy-c v v {}-shortest'.format (_ codec)}) print (ff.cmd) ff.run () return result

Code description

1. The parameters of video_add_audio method include video address, audio address and output directory.

2. Audio supports mp3 and wav format, in which the wav format ffmpeg command will be different.

Verify it.

If _ _ name__ ='_ _ main__': print (video_add_audio ('data/999.mp4',' data/2dc9fa4f-802d-4076-b2c3-b1da886a7cc0.wav') 'data/')) E:\ ProgramData\ Anaconda3\ envs\ pytorch\ python.exe C:/Users/yi/PycharmProjects/test/video_add_audio_tool.pyffmpeg-I data/999.mp4-I data/2dc9fa4f-802d-4076-b2c3-b1da886a7cc0.wav-map 0VV-map 1VOV-cLV copy-CLAV an aac-shortest data/6854281e-19ac-4edc-9638-4124a2a22e52.mp4ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers built with Gcc 9.3-win32 (GCC) 20200320 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-iconv-- enable-zlib-- enable-libxml2-- enable-libfreetype-- enable-libfribidi-enable-gmp-- enable-lzma-- enable-fontconfig-- enable-libvmaf -- disable-vulkan-- enable-libvorbis-- enable-amf-- enable-libaom-- enable-avisynth-- enable-libdav1d-- enable-ffnvcodec-- enable-cuda-llvm-- disable-libglslang-- enable-libass-- enable-libbluray-- enable-libmp3lame-- enable-libopus-- enable-libtheora-- enable-libvpx-- enable-libwebp-- enable-libmfx-enable-libopencore-amrnb-- enable-libopencore-amrwb-- enable-libopenjpeg-- enable-librav1e-- enable-schannel-enable- Sdl2-enable-libsoxr-enable-libsrt-enable-libtwolame-enable-libvidstab-enable-libx264-enable-libx265-enable-libxvid-enable-libzimg-extra-cflags=-DLIBTWOLAME_STATIC-extra-cxxflags= extra-ldflags=-pthread extra-libs=-lgomp libavutil 56. 51.100 / 56. 51.100 libavcodec 58. 91.100 / 58. 91.100 libavformat 58. 45.100 / 58. 45.100 libavdevice 58. 10.100 / 58. 10.100 libavfilter 7. 85.100 / 7. 85.100 libswscale 5. 7.100 / 5. 7.100 libswresample 3. 7.100 / 3. 7.100 libpostproc 55. 7.100 / 55. 7.100Input # 0, mov,mp4,m4a,3gp,3g2,mj2, from 'data/999.mp4': Metadata: major_brand: isom minor_version: 512 compatible_brands: isomiso2mp41 encoder: Lavf58.76.100 Duration: 8420 kb/s Stream 04.88, start: 0.000000, bitrate: 8420 kb/s Stream # 0:0 (und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1280x688 [SAR 1:1 DAR 80:43], 8417 kb/s, 25 fps 25 tbr, 12800 tbn, 25 tbc (default) Metadata: handler_name: VideoHandlerGuessed Channel Layout for Input Stream # 1.0: stereoInput # 1, wav From 'data/2dc9fa4f-802d-4076-b2c3-b1da886a7cc0.wav': Metadata: copyright: bbs.cnxp.com (C) 2009 title: Leon.1994.Directors.Cut.720p.Bluray encoder: Lavf58.45.100 Duration: 05.02 Bitrate: 1411 kb/sStream # 1:0: Audio: pcm_s16le ([1] [0] [0] [0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/sStream mapping: Stream # 0:0-> # 0:0 (copy) Stream # 1:0-> # 0:1 (pcm_s16le (native)-> aac (native)) Press [Q] to stop, [?] For helpOutput # 0, mp4, to 'data/6854281e-19ac-4edc-9638-4124a2a22e52.mp4Austria: Metadata: major_brand: isom minor_version: 512compatible_brands: isomiso2mp41 encoder: Lavf58.45.100 Stream # 0:0 (und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1280x688 [SAR 1:1 DAR 80:43], Qroom2-31, 8417 kb/s, 25 fps, 25 tbr, 12800 tbn 12800 tbc (default) Metadata: handler_name: VideoHandler Stream # 0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp 128 kb/s Metadata: encoder: Lavc58.91.100 aacframe= 122 fps=0.0 qulimuri 1.0 Lsize= 5096kB time=00:00:04.87 bitrate=8561.0kbits/s speed=72.6x video:5015kB audio:77kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.087828% [aac @ 00000192e5913d40] Qavg: 181.360data/6854281e-19ac-4edc-9638-4124a2a22e52.mp4 Process finished with exit code 0

OK, and finally output the synthesized video address.

The above is all the content of the article "how to add audio tools to video in Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report