分享

【Python】写视频的2种常用方法:write

 大傻子的文渊阁 2020-01-20

一、使用Python自带的write_videofile

1、函数说明如下:

  1. def write_videofile(self, filename, fps=None, codec=None,
  2. bitrate=None, audio=True, audio_fps=44100,
  3. preset="medium",
  4. audio_nbytes=4, audio_codec=None,
  5. audio_bitrate=None, audio_bufsize=2000,
  6. temp_audiofile=None,
  7. rewrite_audio=True, remove_temp=True,
  8. write_logfile=False, verbose=True,
  9. threads=None, ffmpeg_params=None,
  10. progress_bar=True):
  11. """Write the clip to a videofile.
  12. Parameters
  13. -----------
  14. filename
  15. Name of the video file to write in.
  16. The extension must correspond to the "codec" used (see below),
  17. or simply be '.avi' (which will work with any codec).
  18. fps
  19. Number of frames per second in the resulting video file. If None is
  20. provided, and the clip has an fps attribute, this fps will be used.
  21. codec
  22. Codec to use for image encoding. Can be any codec supported
  23. by ffmpeg. If the filename is has extension '.mp4', '.ogv', '.webm',
  24. the codec will be set accordingly, but you can still set it if you
  25. don't like the default. For other extensions, the output filename
  26. must be set accordingly.
  27. Some examples of codecs are:
  28. ``'libx264'`` (default codec for file extension ``.mp4``)
  29. makes well-compressed videos (quality tunable using 'bitrate').
  30. ``'mpeg4'`` (other codec for extension ``.mp4``) can be an alternative
  31. to ``'libx264'``, and produces higher quality videos by default.
  32. ``'rawvideo'`` (use file extension ``.avi``) will produce
  33. a video of perfect quality, of possibly very huge size.
  34. ``png`` (use file extension ``.avi``) will produce a video
  35. of perfect quality, of smaller size than with ``rawvideo``.
  36. ``'libvorbis'`` (use file extension ``.ogv``) is a nice video
  37. format, which is completely free/ open source. However not
  38. everyone has the codecs installed by default on their machine.
  39. ``'libvpx'`` (use file extension ``.webm``) is tiny a video
  40. format well indicated for web videos (with HTML5). Open source.
  41. audio
  42. Either ``True``, ``False``, or a file name.
  43. If ``True`` and the clip has an audio clip attached, this
  44. audio clip will be incorporated as a soundtrack in the movie.
  45. If ``audio`` is the name of an audio file, this audio file
  46. will be incorporated as a soundtrack in the movie.
  47. audiofps
  48. frame rate to use when generating the sound.
  49. temp_audiofile
  50. the name of the temporary audiofile to be generated and
  51. incorporated in the the movie, if any.
  52. audio_codec
  53. Which audio codec should be used. Examples are 'libmp3lame'
  54. for '.mp3', 'libvorbis' for 'ogg', 'libfdk_aac':'m4a',
  55. 'pcm_s16le' for 16-bit wav and 'pcm_s32le' for 32-bit wav.
  56. Default is 'libmp3lame', unless the video extension is 'ogv'
  57. or 'webm', at which case the default is 'libvorbis'.
  58. audio_bitrate
  59. Audio bitrate, given as a string like '50k', '500k', '3000k'.
  60. Will determine the size/quality of audio in the output file.
  61. Note that it mainly an indicative goal, the bitrate won't
  62. necessarily be the this in the final file.
  63. preset
  64. Sets the time that FFMPEG will spend optimizing the compression.
  65. Choices are: ultrafast, superfast, veryfast, faster, fast, medium,
  66. slow, slower, veryslow, placebo. Note that this does not impact
  67. the quality of the video, only the size of the video file. So
  68. choose ultrafast when you are in a hurry and file size does not
  69. matter.
  70. threads
  71. Number of threads to use for ffmpeg. Can speed up the writing of
  72. the video on multicore computers.
  73. ffmpeg_params
  74. Any additional ffmpeg parameters you would like to pass, as a list
  75. of terms, like ['-option1', 'value1', '-option2', 'value2'].
  76. write_logfile
  77. If true, will write log files for the audio and the video.
  78. These will be files ending with '.log' with the name of the
  79. output file in them.
  80. verbose
  81. Boolean indicating whether to print infomation.
  82. progress_bar
  83. Boolean indicating whether to show the progress bar.

2、使用代码如下:

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

二、加载opencv的videoWriter

1、函数说明如下:

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

参数说明:

video_name:视频名称

file_format:文件格式

fps:帧率

isColor:输出格式,等于0时输出灰度视频,不等于0时输出彩色视频

2、使用代码如下:

  1. import cv2 as cv
  2. # 调用摄像头
  3. videoCapture = cv.VideoCapture(0)
  4. # 设置帧率
  5. fps = 30
  6. # 获取窗口大小
  7. size = (int(videoCapture.get(cv.CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv.CAP_PROP_FRAME_HEIGHT)))
  8. # 设置VideoWrite的信息
  9. videoWrite = cv.VideoWriter('MySaveVideo.avi', cv.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)
  10. # 先获取一帧,用来判断是否成功调用摄像头
  11. success, frame = videoCapture.read()
  12. # 通过设置帧数来设置时间,减一是因为上面已经获取过一帧了
  13. numFrameRemainling = fps * 5 - 1
  14. # 通过循环保存帧
  15. while success and numFrameRemainling > 0:
  16. videoWrite.write(frame)
  17. success, frame = videoCapture.read()
  18. numFrameRemainling -= 1
  19. # 释放摄像头
  20. videoCapture.release()

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多