Combining an audio file with video file in python - Stack Overflow https://stackoverflow.com/questions/28219049/combining-an-audio-file-with-video-file-in-python
import os os_sep = os.sep this_file_abspath = os.path.abspath(__file__) this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[ -1] f_mp3 = '{}{}{}'.format(this_file_dirname, os_sep, 'auido.mp3') from playsound import playsound import time import math this_time = time.time() # playsound(f_mp3) # t_spend = time.time() - this_time t_spend = 58.777058839797974 # 音频的秒数 t_spend = math.ceil(t_spend) import cv2 import glob # 每秒传输帧数(Frames Per Second) fps = 100 # 保存视频的FPS,可以适当调整 FPS是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数。FPS是测量用于保存、显示动态视频的信息数量。每秒钟帧数愈多,所显示的动作就会愈流畅。通常,要避免动作不流畅的最低是30。某些计算机视频格式,每秒只能提供15帧。 fps = 15 fps = 5 fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') # opencv3.0 f_v = '{}{}'.format(int(time.time()), 'saveVideo.avi') f_img_d = '{}{}{}{}{}'.format(this_file_dirname, os_sep, 'mypng', os_sep, '*.jpg') imgs = glob.glob(f_img_d) img = cv2.imread(imgs[0]) img_size = (img.shape[1], img.shape[0]) videoWriter = cv2.VideoWriter(f_v, fourcc, fps, img_size) # frame_min = cv2.imread(imgs[-1]) """ 用图片总数均分音频时间 """ os_delay_factor=0.14 os_delay_factor=0.05 myinterval = t_spend / len(imgs)*os_delay_factor for imgname in imgs: this_time = time.time() frame = cv2.imread(imgname) videoWriter.write(frame) while time.time() - this_time < myinterval: print(imgname) videoWriter.write(frame) #cv2.waitKey(10 * 1000) #time.sleep(0.5) ##单位时间,写入帧数越大,视频空间越大, # time.sleep(0.5) # for i in range(fps): # print(imgname) # videoWriter.write(frame) videoWriter.release()
音频视频 合成
import time import os import os, os.path, sys import win32process, win32event ''' ffmpeg 命令 - wangkangluo1 - 博客园 http://www.cnblogs.com/wangkangluo1/archive/2012/07/10/2585095.html ''' d='C:\Users\sas\PycharmProjects\mypygame\ffmpeg-20180331-be502ec-win64-static\bin\ffmpeg.exe -i son.wav -i video_origine.avi video_finale.mpg' d='C:\Users\sas\PycharmProjects\mypygame\ffmpeg-20180331-be502ec-win64-static\bin\ffmpeg.exe -i auido.mp3 -i 1523174854saveVideo.avi video_finale.mp4' os.system(d)
C:UserssasPycharmProjectsmypygameffmpeg-20180331-be502ec-win64-staticin>ffmpeg.exe -i auido.mp3 -i 1523174854saveVideo.avi video_finale.mp4 ffmpeg version N-90553-gbe502ec6cd Copyright (c) 2000-2018 the FFmpeg developers built with gcc 7.3.0 (GCC) configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth libavutil 56. 12.100 / 56. 12.100 libavcodec 58. 16.100 / 58. 16.100 libavformat 58. 10.100 / 58. 10.100 libavdevice 58. 2.100 / 58. 2.100 libavfilter 7. 13.100 / 7. 13.100 libswscale 5. 0.102 / 5. 0.102 libswresample 3. 0.101 / 3. 0.101 libpostproc 55. 0.100 / 55. 0.100 [mp3 @ 000001be707ea140] Estimating duration from bitrate, this may be inaccurate Input #0, mp3, from 'auido.mp3': Duration: 00:00:58.64, start: 0.000000, bitrate: 16 kb/s Stream #0:0: Audio: mp3, 16000 Hz, mono, s16p, 16 kb/s Input #1, avi, from '1523174854saveVideo.avi': Duration: 00:01:05.80, start: 0.000000, bitrate: 5720 kb/s Stream #1:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj420p(pc, bt470bg/unknown/unknown), 1080x1920 [SAR 1:1 DAR 9:16], 5736 kb/s, 5 fps, 5 tbr, 5 tbn, 5 tbc Stream mapping: Stream #1:0 -> #0:0 (mjpeg (native) -> h264 (libx264)) Stream #0:0 -> #0:1 (mp3 (native) -> aac (native)) Press [q] to stop, [?] for help [libx264 @ 000001be7080d7c0] using SAR=1/1 [libx264 @ 000001be7080d7c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 [libx264 @ 000001be7080d7c0] profile High, level 4.0 [libx264 @ 000001be7080d7c0] 264 - core 155 r2901 7d0ff22 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=5 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00 Output #0, mp4, to 'video_finale.mp4': Metadata: encoder : Lavf58.10.100 Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuvj420p(pc, progressive), 1080x1920 [SAR 1:1 DAR 9:16], q=-1--1, 5 fps, 10240 tbn, 5 tbc Metadata: encoder : Lavc58.16.100 libx264 Side data: cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1 Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 69 kb/s Metadata: encoder : Lavc58.16.100 aac frame= 329 fps= 87 q=-1.0 Lsize= 1068kB time=00:01:05.20 bitrate= 134.2kbits/s speed=17.3x video:492kB audio:564kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.136881% [libx264 @ 000001be7080d7c0] frame I:4 Avg QP: 4.35 size:118982 [libx264 @ 000001be7080d7c0] frame P:83 Avg QP:10.52 size: 119 [libx264 @ 000001be7080d7c0] frame B:242 Avg QP: 7.67 size: 71 [libx264 @ 000001be7080d7c0] consecutive B-frames: 1.8% 0.0% 0.9% 97.3% [libx264 @ 000001be7080d7c0] mb I I16..4: 67.7% 26.8% 5.6% [libx264 @ 000001be7080d7c0] mb P I16..4: 0.5% 0.0% 0.0% P16..4: 0.1% 0.0% 0.0% 0.0% 0.0% skip:99.3% [libx264 @ 000001be7080d7c0] mb B I16..4: 0.0% 0.0% 0.0% B16..8: 0.0% 0.0% 0.0% direct: 0.0% skip:100.0% L0:94.7% L1: 5.3% BI: 0.0% [libx264 @ 000001be7080d7c0] 8x8 transform intra:24.1% inter:90.7% [libx264 @ 000001be7080d7c0] coded y,uvDC,uvAC intra: 20.5% 23.5% 20.4% inter: 0.0% 0.0% 0.0% [libx264 @ 000001be7080d7c0] i16 v,h,dc,p: 92% 6% 2% 0% [libx264 @ 000001be7080d7c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 33% 17% 22% 4% 4% 4% 5% 7% 4% [libx264 @ 000001be7080d7c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 41% 25% 10% 3% 5% 5% 5% 4% 3% [libx264 @ 000001be7080d7c0] i8c dc,h,v,p: 73% 11% 13% 3% [libx264 @ 000001be7080d7c0] Weighted P-Frames: Y:1.2% UV:1.2% [libx264 @ 000001be7080d7c0] ref P L0: 98.3% 0.0% 1.1% 0.6% [libx264 @ 000001be7080d7c0] ref B L0: 94.5% 5.5% [libx264 @ 000001be7080d7c0] kb/s:61.15 [aac @ 000001be7080f3c0] Qavg: 32863.445 C:UserssasPycharmProjectsmypygameffmpeg-20180331-be502ec-win64-staticin>