日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python】写视频的2种常用方法:write_videofile和videoWrite

發布時間:2025/3/21 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python】写视频的2种常用方法:write_videofile和videoWrite 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、使用Python自帶的write_videofile

1、函數說明如下:

def write_videofile(self, filename, fps=None, codec=None,bitrate=None, audio=True, audio_fps=44100,preset="medium",audio_nbytes=4, audio_codec=None,audio_bitrate=None, audio_bufsize=2000,temp_audiofile=None,rewrite_audio=True, remove_temp=True,write_logfile=False, verbose=True,threads=None, ffmpeg_params=None,progress_bar=True):"""Write the clip to a videofile.Parameters-----------filenameName of the video file to write in.The extension must correspond to the "codec" used (see below),or simply be '.avi' (which will work with any codec).fpsNumber of frames per second in the resulting video file. If None isprovided, and the clip has an fps attribute, this fps will be used.codecCodec to use for image encoding. Can be any codec supportedby ffmpeg. If the filename is has extension '.mp4', '.ogv', '.webm',the codec will be set accordingly, but you can still set it if youdon't like the default. For other extensions, the output filenamemust be set accordingly.Some examples of codecs are:``'libx264'`` (default codec for file extension ``.mp4``)makes well-compressed videos (quality tunable using 'bitrate').``'mpeg4'`` (other codec for extension ``.mp4``) can be an alternativeto ``'libx264'``, and produces higher quality videos by default.``'rawvideo'`` (use file extension ``.avi``) will producea video of perfect quality, of possibly very huge size.``png`` (use file extension ``.avi``) will produce a videoof perfect quality, of smaller size than with ``rawvideo``.``'libvorbis'`` (use file extension ``.ogv``) is a nice videoformat, which is completely free/ open source. However noteveryone has the codecs installed by default on their machine.``'libvpx'`` (use file extension ``.webm``) is tiny a videoformat well indicated for web videos (with HTML5). Open source.audioEither ``True``, ``False``, or a file name.If ``True`` and the clip has an audio clip attached, thisaudio clip will be incorporated as a soundtrack in the movie.If ``audio`` is the name of an audio file, this audio filewill be incorporated as a soundtrack in the movie.audiofpsframe rate to use when generating the sound.temp_audiofilethe name of the temporary audiofile to be generated andincorporated in the the movie, if any.audio_codecWhich audio codec should be used. Examples are 'libmp3lame'for '.mp3', 'libvorbis' for 'ogg', 'libfdk_aac':'m4a','pcm_s16le' for 16-bit wav and 'pcm_s32le' for 32-bit wav.Default is 'libmp3lame', unless the video extension is 'ogv'or 'webm', at which case the default is 'libvorbis'.audio_bitrateAudio bitrate, given as a string like '50k', '500k', '3000k'.Will determine the size/quality of audio in the output file.Note that it mainly an indicative goal, the bitrate won'tnecessarily be the this in the final file.presetSets the time that FFMPEG will spend optimizing the compression.Choices are: ultrafast, superfast, veryfast, faster, fast, medium,slow, slower, veryslow, placebo. Note that this does not impactthe quality of the video, only the size of the video file. Sochoose ultrafast when you are in a hurry and file size does notmatter.threadsNumber of threads to use for ffmpeg. Can speed up the writing ofthe video on multicore computers.ffmpeg_paramsAny additional ffmpeg parameters you would like to pass, as a listof terms, like ['-option1', 'value1', '-option2', 'value2'].write_logfileIf true, will write log files for the audio and the video.These will be files ending with '.log' with the name of theoutput file in them.verboseBoolean indicating whether to print infomation.progress_barBoolean indicating whether to show the progress bar.

2、使用代碼如下:

from moviepy.editor import VideoFileClipclip = VideoFileClip("myvideo.mp4").subclip(100,120)clip.write_videofile("my_new_video.mp4")clip.close()

二、加載opencv的videoWriter

1、函數說明如下:

videoWriter = cv.videoWriter(video_name, file_format, fps, isColor)

參數說明:

video_name:視頻名稱

file_format:文件格式

fps:幀率

isColor:輸出格式,等于0時輸出灰度視頻,不等于0時輸出彩色視頻

2、使用代碼如下:

import cv2 as cv# 調用攝像頭 videoCapture = cv.VideoCapture(0) # 設置幀率 fps = 30 # 獲取窗口大小 size = (int(videoCapture.get(cv.CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv.CAP_PROP_FRAME_HEIGHT)))# 設置VideoWrite的信息 videoWrite = cv.VideoWriter('MySaveVideo.avi', cv.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)# 先獲取一幀,用來判斷是否成功調用攝像頭 success, frame = videoCapture.read() # 通過設置幀數來設置時間,減一是因為上面已經獲取過一幀了 numFrameRemainling = fps * 5 - 1 # 通過循環保存幀 while success and numFrameRemainling > 0:videoWrite.write(frame)success, frame = videoCapture.read()numFrameRemainling -= 1 # 釋放攝像頭 videoCapture.release()

參考:https://blog.csdn.net/li_l_il/article/details/83616586

總結

以上是生活随笔為你收集整理的【Python】写视频的2种常用方法:write_videofile和videoWrite的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。