程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Ffmpeg of Python interesting code to realize video transcoding +cmd animation effect

編輯:Python

background :

This blog is a video I saw , Use python Implementation of a fun code , Then I tried to realize it myself , To record the whole process .

Content :

The main implementation is to use mmfpeg This tool , Realize the reading of video , Then extract photos from the video according to a certain frame rate , And then convert these photos into grayscale images , Set the appropriate threshold , Find the position of the gray value that meets the threshold .
First of all, let me show you the results , Then explain how to implement it step by step :
1. This is one I found on the Internet GIF chart , And turn it into MP4 video

2. The operation results are as follows ( The blog doesn't seem to be able to insert videos , I just cut a few pictures ):


  1. Preparation :
    First download ffmpeg, Just say Windows The next way (linux It's not hard to ), First go Official website , download Windows Version of the installation package

    When the download is complete , decompression , Put it where you like , Then find... In the directory bin Folder , Put this directory in your environment variable ( How to configure environment variables will not be discussed in detail here , Baidu .)

    The next step is to test whether the configuration is successful , Open your cmd, Input ffmpeg -version, Output the following , It's a success

  2. Directory structure

    Our idea is video ——> picture ——>txt file , Again “ Play ” these txt file . So put... In your working directory 2 A folder image and txt, Store images and... Separately txt file , Take a look at my catalog

    test.py The document is what we want to write python The file .

Code interpretation :

We have three functions , Respectively realize video to picture , Picture turn txt, and main function .

First, the first function get_image(video_path, image_path), The two parameters are your video path and the path to store pictures . Variable frame It is a parameter used to determine how many pictures you convert each second of video . then os.system() That sentence is what makes you Windows stay cmd It carries out the sentence inside , The general meaning is to 10 Extract the picture of the video at the frequency of frames per second , Save to your directory . Specific usage can be Baidu ffmpeg, This is a bit of a hassle , I won't go into details .

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from PIL import Image # If there is no such library , please pip install PIL
import numpy # If there is no such library , please pip install numpy
frame = 10 # Per second 10 frame , That is, ten in a second 
def get_image(video_path, image_path):
try:
os.system('ffmpeg -i {0} -r {1} -f image2 {2}\%05d.png'.format(video_path, frame, image_path))
except:
print('ERROR !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')

Then we extracted the picture , Go to image Look under the folder , You can see a lot of pictures

Next , Is to convert the picture into txt file , The function written is image_to_txt(image_path, txt_path), The two parameters here are the path where you read the image and the storage txt Path to file , The code explanation is in the comments .

def image_to_txt(image_path, txt_path):
txt_count = 1 # Used for naming txt file 
fileList = os.listdir(image_path) # Return all picture names , It's a list of strings 
for file in fileList: # Traverse every picture 
img = Image.open(image_path + '\\'+ file).convert('L')
# Here we use PIL library convert function , take RGB Convert the picture to grayscale , Parameters 'L' Represents the conversion to a grayscale image 
charWidth = 140
# This is the setting behind you cmd The size of the window in which the content is displayed , According to your own situation , Adjust the value appropriately 
img = img.resize((charWidth, 40))
target_width, target_height = img.size
data = numpy.array(img)[:target_height, :target_width]
# Use numpy library , Convert the image to an array 
with open(txt_path + '\\' + str(txt_count) + '.txt', 'w', encoding='utf-8') as f:
txt_count += 1 # A graph corresponds to a txt file , So every time I traverse a graph , Add one to this value 
for row in data:
for pixel in row:
if pixel < 127: # If the gray value is less than 127, That is, darker , Just write one character '*'
f.write('*')
else:
f.write(' ')
f.write('\n')

This is a txt Folder results :

The last is , One run(txt_path) function , be used for “ Play ” these txt file , The same parameter is txt Path to file

def run(txt_path):
fileList = os.listdir(txt_path)
for i in range(1, len(fileList)+1): # Traverse all txt file 
try:
os.system('type ' + txt_path + '\\' + str(i) + '.txt')
# here type The order is Windows Command below , I believe many people haven't used it very much , You can tell by trying ,type+ file name , You can go to cmd It shows the contents of the file 
os.system('cls')
# Clear screen , Every time txt file , Just clear the screen , Then display the next txt
# Here you can also add a delay function appropriately , If the display is too fast 
except:
print('ERROR !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')

Last, last : Let these three functions run ~~

if __name__ == '__main__':
video_path = r' Your video path '
image_path = r' The path where you store your pictures '
txt_path = r' You store txt Path to file '
get_image(video_path, image_path)
image_to_txt(image_path, txt_path)
run(txt_path)

summary :

This code is not very practical , It's just a little fun , Plus you can practice python, You can play when you are free . And the processed video should be of single color , Then the color of the characters should also be single , It's easier to find that threshold . So I directly looked for black and white pictures here , If the color and more scattered words , The final effect is not very good .

Write a blog for the first time , End .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved