N-line Python code series: two lines of code to achieve video files into a series of pictures output

LaoYuanPython 2021-04-08 08:47:35
n-line line python code series

* ░ Old ape Python Blog Directory

 Insert picture description here

One 、 introduction

I've seen several similar articles recently “n That's ok Python Code …” 's blog post , It looks pretty good , concise 、 practical , Spread knowledge 、 It brings the amount of reading , It stirred the heart of the old ape , Decided to follow suit , Push one “n That's ok Python Code series ” article .

For the wonderful pictures in the video , Sometimes you want to save the screenshots , It can be realized through screen capture software , But sometimes it is necessary to output the pictures in the video into picture files at regular intervals .

This paper introduces two lines of code to achieve the video output screen to a file at a specified interval . more “n That's ok Python Code series ” Please refer to the free column 《n That's ok Python Code series 》.

Two 、 Output video images at equal intervals

To do video editing , The old ape used Moviepy. The following code uses the video 《 Fan Notepad 》, Let's 10 Second output a video picture . The code implementation is as follows :

from moviepy.editor import *
clip = VideoFileClip(r"F:\video\fansNote1M_crop.mp4")

The above three lines of code are implemented first Moviepy Module loading 、 Then read in the video file 、 Output the screenshot to the specified series of files . The above three lines of code can also be reduced to the following two lines :

from moviepy.editor import *

Let's see the results :

The video is only 1 minute , You can see the output 7 File , front 3 The content of the output image file is as follows :

3、 ... and 、 Background knowledge

3.1、moviepy brief introduction

To achieve video editing , The old ape used it moviepy library .

MoviePy It's a video editor Python modular , Can be used for basic video operations ( Like cutting 、 Connect 、 Title insertion )、 Video synthesis ( Also known as nonlinear editing )、 Video processing or creating advanced effects .

It can read and write the most common video formats , Include GIF.MoviePy The video that can be processed is ffmpeg Format , The old ape understands that the supported file types include at least :*.mp4 *.wmv *.rm *.avi *.flv *.webm *.wav *rmvb.

MoviePy Very simple installation , Use pip When installing , Please point the site to the domestic mirror site , Otherwise, the download will be slow or unable to download , The old ape uses the mirror image of Tsinghua University , Instruction is :

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple moviepy

3.3、 Correlation function

The related functions involved in the above code include VideoFileClip、write_images_sequence,VideoFileClip stay 《n That's ok Python Code series https://blog.csdn.net/laoyuanpython/category_10858655.html》 It has been introduced in the previous blog posts of the column , I will not repeat it here .

write_images_sequence function

write_images_sequence The function is used to t The frame at the specified time and position is saved to the specified image file . The call syntax is as follows :
write_images_sequence(self, nameformat, fps=None, verbose=True,withmask=True, logger='bar')

  • nameformat: Output format and file name rules , Specifies the number format of the serial number of the file and the file name of the image type extension , For example, the name of the file “frame%03d.png” Indicates that the file name starts with “frame”, Back “%03d” Indicates the sequence number of the file with 3 Number them in order of numbers , File name suffix “png” Indicates that the encoding format is PNG. The file name format can also take path mode , Such as “ Directory name \f%04d.jpeg” etc. . But the directory name must already exist , In addition, if the number of output files is more than the set serial number range , It will automatically add up 1 Expand , Until all the output is complete
  • fps: Output frames per second , If it is not specified, press the fps For the output , It can be a decimal
  • withmask: If withmask by True, The mask of the corresponding frame will be written into the image alpha Channel layer , Only on PNG Image valid
  • verbose: Whether to output processing information
  • logger: String type ,"bar" Means progress bar 、None Indicates no setting 、 Or the name of any program logger

Four 、 Summary

This paper introduces the use of Python+Moviepy Two lines of code to achieve the video output into a series of pictures to multiple files , And introduced moviepy The function and installation of, as well as the key functions and syntax of related processing .

It's not easy to write , Please support :

If you can get something from reading this article , Please give me some compliments 、 Comment on 、 Collection , Thank you for your support !

If you have questions about the content of the article , You can leave a message in the comments section of the blog , Or pay attention to : Old ape Python Wechat public number sends message for consultation .

More about moviepy Please refer to Python Audio and video clip Library MoviePy1.0.3 Chinese course guide and executable tool download 》 A guided tour of .

Pay column on the old ape

  1. Paid column 《 Use PyQt Developing graphical interfaces Python application 》 Special introduction based on Python Of PyQt Basic course of graphical interface development , The corresponding article directory is 《 Use PyQt Developing graphical interfaces Python Application column Directory 》;
  2. Paid column 《 moviepy Audio and video development column ) Detailed introduction moviepy The class correlation method of audio and video clip synthesis processing and the use of correlation method to process related clip synthesis scene , The corresponding article directory is 《moviepy Audio and video development column Directory 》;
  3. Paid column 《 OpenCV-Python Difficult problem set for beginners 》 by 《 OpenCV-Python Graphics and image processing 》 The accompanying column of , It is the author's opinion of OpenCV-Python Some problems encountered in the study of graphics and image processing , The relevant data are basically the results of repeated studies of old apes , be conducive to OpenCV-Python Beginners have a deeper understanding of OpenCV, The corresponding article directory is 《 OpenCV-Python Catalog of difficult problem set for beginners
  4. Paid column 《Python Introduction to reptiles 》 From the perspective of Xiaobai, an Internet front-end developer, this paper introduces the knowledge and skills of crawler development , Including the basics of getting started with crawlers , And crawling CSDN The article information 、 Blogger information 、 Like the article 、 Comments, etc .

The first two columns are suitable for a certain amount of Python Xiaobai readers who are basic but have no relevant knowledge learn , In the third column, please combine 《 OpenCV-Python Graphics and image processing 》 Learning to use .

For lack of Python Basic colleagues , Through the free column of the old ape 《 special column :Python Basic course catalog 》 Learn from scratch Python.

If you are interested and willing to support the old ape readers , Welcome to the paid column .

If you have questions about the content of the article , You can leave a message in the comments section of the blog , Or pay attention to : Old ape Python Wechat public number sends message for consultation .

Old ape Python, Learn from the old ape Python!

* ░ Go to the old ape Python Blog Directory


  1. Pandas-二进制操作
  2. python入门教程14-01 (python语法入门之python内存泄露)
  3. Web4 - get flag through Python
  4. python-web5
  5. Pandas binary operation
  6. python入门教程13-06 (python语法入门之视图、触发器、事务、存储过程、函数)
  7. python入门教程13-07 (python语法入门之ORM框架SQLAlchemy)
  8. python入门教程13-08 (python语法入门之python索引原理与慢查询优化)
  9. 定投指数到底能不能赚钱?Python 来告诉你答案
  10. Python入门学习之:10分钟1500访问量
  11. Getting started with Python 14-01
  12. 用 Python 画哆啦 A 梦
  13. Python 图表利器 pyecharts
  14. 用 Python 抓取公号文章保存成 HTML
  15. Introduction to Python 13-06 (view, trigger, transaction, stored procedure, function of introduction to Python syntax)
  16. Getting started with Python 13-07 (ORM framework Sqlalchemy for getting started with Python syntax)
  17. Introduction to Python 13-08
  18. Can fixed investment index make money? Python will tell you the answer
  19. Introduction to Python: 1500 visits in 10 minutes
  20. 用 Python 获取股市交易数据
  21. Drawing Doraemon in Python
  22. Python charts
  23. 用 Python 来了解一下《安家》
  24. 用 Python 抓取公号文章保存成 PDF
  25. 用 Python 生成炫酷二维码及解析
  26. Using Python to grab articles with public number and save them as HTML
  27. Getting stock market trading data with Python
  28. Learn about settle down in Python
  29. Using Python to grab articles with public number and save them as PDF
  30. Using Python to generate cool two dimensional code and analysis
  31. 20210225-1 Python错误与异常
  32. 20210225-1 Python errors and exceptions
  33. 使用Python拆分、合并PDF
  34. Using Python to split and merge pdf
  35. 真工程师:20块钱做了张「名片」,可以跑Linux和Python
  36. Implementation of LSB steganography based on MATLAB and python
  37. Real Engineer: 20 yuan to make a "business card", can run Linux and python
  38. python修改微信和支付宝步数
  39. Python changes WeChat and Alipay steps
  40. Python空间分析| 01 利用Python计算全局莫兰指数(Global Moran's I)
  41. Python spatial analysis | 01 using Python to calculate global Moran's index
  42. python入门教程13-05 (python语法入门之数据备份、pymysql模块)
  43. Introduction to Python 13-05 (data backup and pymysql module of introduction to Python syntax)
  44. pandas如何操作Excel?还不会的,看此一篇足矣
  45. How does panda operate excel? Not yet. This is enough
  46. 用python连接数据库模拟用户登录
  47. Using Python to connect database to simulate user login
  48. python入门教程13-04 (语法入门之记录相关操作)
  49. Introduction to Python 13-04
  50. python入门教程13-03 (python语法入门之表相关操作)
  51. Introduction to Python 13-03
  52. python的多线程的网络爬虫,待改进
  53. Python multithreaded web crawler, to be improved
  54. 常见加密算法的Python实现:
  55. Python implementation of common encryption algorithms:
  56. python刷题-核桃的数量
  57. Number of walnuts
  58. Python爬虫知乎文章,采集新闻60秒
  59. Python crawler knows articles and collects news for 60 seconds
  60. Python爬虫知乎文章,采集新闻60秒