preface

Friends who have done shop more or less will use the main picture video, because it can improve the exposure rate of shop goods, in order to improve the turnover rate of shop goods, today, xiaobian to take you to do such a artifact, it is said that on the Internet similar software is charging software, we can use it for free after doing a good job. So you can contact taobao store! Sell it at a low price! I’m sure the average person won’t refuse!

I. Project preparation

Editor: Sublime Text 3

Audio and video program: FFmpeg

Modules: Tkinter, OS, PIL, time

Ii. Project objectives

Combine multiple pictures into a video with background music

3. Experimental steps

1. Prepare pictures

This we casually go online search ok, need not I said more.

2. Import the module

import time 
from  tkinter import  filedialog 
import tkinter as t 
from PIL import Image 
from tkinter import messagebox 
import os 
Copy the code

3. Create software interfaces

Using the Tkinter module, which comes with Python, we add everything we need.

Self.fg =os.path.join(os.path.expandUser ("~"), self.fg= os.path.expandUser ("~"), Self.root = t.tk () self.root.title(' v1.0')# add self.root.iconbitmap('1.ico') # add self.root.iconbitmap('1.ico') # add self.root.iconbitmap('1.ico') # add self.root= t.tk () # add self.root Self. Root. Attributes ('-alpha',0.9) # m_attributes('-topmost',1) # self. Self. tt=t. ext(self.root,width=75,height=30) self.b1=t. tton(self.root,text=' open image file ',font =("宋体",13,'bold'),command=self.open_file) self.b3= t.botton (self.root,text=' open audio file ',font) = (" tahoma ", 13, 'bold'), the command = self. Adio) self. B7 = t.B utton (self. Root, text = 'open the file and batch modification, the font = (" tahoma ", 13,' bold '), the command = self. The xg) Self. b8= t.button (self.root,text=' check the photo ',font =("宋体",13,'bold'),command=self.ck) self.l1= t.button (self.root,text=' check the photo ') ') self.e= t.try (self.root) self.b2= t.botton (self.root,text=' create a video ',font =("宋体",13,'bold'),command=self.video) Self.b4 =t. dutton (self.root,text=' play audio file ',font =("宋体",13,'bold'),command=self.play) Self.b5 =t. tton(self.root,text=' empty the text box ',font =("宋体",13,'bold'),command=self.close) Self.b6 =t. dutton (self.root,text=' preview ',font =("宋体",13,'bold'),command=self.player) Self. l2= t.abel (self.root,text=' ') self.l2= t.abel (self.root,text=' ') ') self.e1=t. try(self.root,width=5) self.1 =t. try(self.root,width=5) self.1 =t. try(self.root,width=5) self.1 =t. try(self.root,width=5) ') self.e2=t.Entry(self.root,width=5) self.tt.place(x=10,y=50) self.b1.place(x=100,y=10) self.l1.place(x=120,y=460) self.e.place(x=200,y=460) self.b2.place(x=200,y=550) self.b3.place(x=240,y=10) self.b4.place(x=550,y=120) self.b5.place(x=550,y=200) self.b6.place(x=550,y=300) self.l2.place(x=120,y=500) self.l3.place(x=200,y=500) self.l4.place(x=280,y=500) self.e1.place(x=230,y=500) self.e2.place(x=310,y=500) self.b7.place(x=380,y=10) self.b8.place(x=580,y=10) self.root.mainloop()Copy the code

Now we can generate a main interface with widgets. Look at this:

Does it look feature-rich? Let’s implement its functions one by one.

Does it look feature-rich? Let’s implement its functions one by one.

4. Specific realization process of software functions

1) Open the picture file and open the audio file

The two implementation steps are similar, just change the type of punch file, we need to use the FileDialog module in Tkinter to open multiple files and open a single file function. Currently, we only support two suffixes: PNG and JPG.

# option for the main figure video images aa = filedialog. Askopenfilenames (title = 'select All main photo Files, filetypes = [(' PNG', 'the PNG'), (' JPG ', 'JPG'), (' All Files, '*')]) # extract the suffix for the next step aa=self.tl aa[0] # file name and its path # Select the audio file Filedialog. Askopenfilename (title = 'open audio file, filetypes = [(' WAV', 'WAV), (' MP3', 'MP3), ('Copy the code

2) Print the log output during video generation to the text box

Ffmpeg generates the video with a string of logs, but this log cannot be captured with popen, so we use DOS capture to redirect it to a file, and then we are reading the contents of the file into the text box, so we need to do this:

If os.path.exists('1.txt'): try: f=open(os.path.join(self.fg,'1.txt'),'r',encoding=' utF-8 ') Messagebox.showinfo (title=' prompt ',message='----- failed to generate main image video file ----') # prompt finally: self.tt.insert('insert',f.read()+'\n') self.tt.insert('insert','\n') Messagebox.showinfo (title=' prompt ',message='----- ') ----\n file name: %s.mp4'% self.lldb ()) f.close() # disable file stream else: Af =open(os.path.join(self.fg,'1.txt'),'w') # create af.close() # close the file streamCopy the code

3) Use FFMPEG to generate video

This step depends on how well you master the operation steps of FFMPEG generation video. The specific steps are as follows:

OS. Popen (' ffmpeg - r - 300 - t 0.3 I I {} {} / % d.p ng - - strict - 2 - f mp4 - s x {} {} {} > {} 2 > &1 'Copy the code

Seeing such a long parameter you must be confused, let me explain one by one.

-r: video frame -t video end event -I Generally followed by picture or audio video name -F MP4 2 indicates the standard set for using AAC audio encoding -F MP4 video output format -S Video resolutionCopy the code

The last step is to output all the information to a text file. You can use pipe 2>&1 to output the result to a standard stream and then to a text file.

5. Preview the video

After the video is generated, you can see the effect of its video playback. Here, we still use OS popen method, which can be very convenient to call system video player to play:

os.popen(self.e.get()+'.mp4') 
Copy the code

So that’s what it looks like to preview the video

Iv. Project Summary

Through the use of FFMPEG, we can make a lot of interesting things, this article aims to teach you ideas, so that you can have a deeper understanding of FFMPEG and use it to make more valuable software.

The end of the

If there is still a small partner through the article feeling did not learn! Need a complete video tutorial or source can click on the blue font: click here to get or add Q group: 754370353 can be taken, for this fast doubt, there will not be a small partner can directly go in to ask ha! Xiaobian will be the first time to reply to you! Or use the script directly, there is no package yet!

If you find this article helpful, please give it a thumbs up and a comment! Can forward a better pull! Thank you!