python+ffmpeg video transcoding

Posted by cavey5 on Fri, 04 Mar 2022 14:51:19 +0100

This article is forwarded from: blog.csdn.net/KH_FC/article/detail...

crap

At present, python is also learning, and I am not particularly proficient in python. I also use video transcoding, so I wrote such a simple tool after consulting some materials. This tool can be used by myself. There are still many deficiencies when using the formal environment. This is not perfect, I also hope there is a great God who knows python to improve it. No more nonsense, open the whole.

method

At first, when I was thinking about writing a video transcoding tool myself, I was thinking about whether to write in PHP or python. Finally, I chose Python because I ran into a wall in PHP. Up to now, I can't solve that problem. The BUG after writing PHP is that there will be a request timeout problem during large video transcoding. Up to now, I can't solve this BUG, I tried to modify the request time, cache and other methods in the PHP configuration file, but they didn't solve them. I'm really going crazy. I'll find a way to do it later. I thought about whether Python would have this problem. As a result, it really didn't have this problem.
Since the idea is to write, of course, we should consider how to transcode the video. At first, we think about using the flash framework. Finally, we think about the trouble of accessing and using the flash framework on the web page. It's better to transcode directly by using the windows frame and double-click the code directly. The final preliminary decision is to use the tkinter module in python to get it and write a windows GUI directly. It's not fragrant. With the display mode, there is transcoding. How to transcode? After consulting many websites, I finally decided to use ffmpeg transcoding tool. After selecting the tool, I will write code. In the process of writing later, there are some problems, that is, choose ffmpeg module or call ffmpeg program? I am a network engineer, so I am very sensitive to the code, and I will have a headache if it is difficult. I checked the use methods of modules and programs respectively. What I refer to is: [Python] ffmpeg module processes video and audio information To tell you the truth, this article is really good, but I can't understand it. It's too profound. It's estimated that I can understand it in a few more years of python. Bloggers wait for me. Don't delete the article. I went to the official website to see the document ffmpeg official website , this is quite simple. I can understand it and find that video transcoding can indeed be carried out. Well, with a method, that's the idea behind it.

thinking

In fact, this idea is also quite simple. What I want is to double-click the python code I wrote to directly pop up a windows window, and then I enter the original video address or path, then enter a transcoded name, and then get a transcoding button, so I can transcode directly. No problem, clear thinking. Here is the direct opening and adjustment.

python+tkinter production window

First of all, since I want to get this window, of course, we need to know how to use tkinter module. Below, I summarize some simple usage
[example]

import tkinter        #Import Tkubter module
test = tkinter.Tk()
test.mainloop()

Execution result:

There is such a simple windows window. There are many uses of tkinter module. I won't elaborate here. Let's continue to talk about our transcoding.

The first is to make a Windows window, and then let me input some information. Here is the windows window I wrote.

import tkinter as tk    #Import tkinter module and alias it tk

wind=tk.Tk()    #Create top-level control wind
wind.geometry("800x400")    #Set window size
wind.title("Video transcoding")    #Set window title

title_lab=tk.Label(wind,text="Video transcoding",font="Imitation song 20 bold")    #Create pane title, content, font, color
title_lab.place(x=350,y=30)    #Set title position

textlab=tk.Label(wind, text="Please enter the video address:", font="Imitation song 20 bold", fg="blue", width=20) #Create the form name and set the font, color and size
textlab.place(x=0,y=100)    #Set form name and location
text_entry=tk.Entry(wind, width=30,font="Imitation song 20 bold")    #Create the input control entry, that is, the form
text_entry.place(x=300,y=100)    #Set control location

mzlab=tk.Label(wind, text="Please enter a new video name:", font="Imitation song 20 bold",fg="blue", width=18)    #Create the form name and set the font, color and size
mzlab.place(x=0,y=200)    #Set form name and location
mz_entry=tk.Entry(wind, width=30,bg="white",font="Imitation song 20 bold")    #The entry control is the input of the form
mz_entry.place(x=300,y=200)    #Set control location

btn=tk.Button(wind,text="Submit", font="Imitation song 20 bold", fg="blue", width=8)    #Set the submit button, and set the font style and size
btn.place(x=350,y=300)    #Set button position
wind.mainloop()    #Message loop for window

Final rendering:

The display is written out. It's time to get the function later

ffmpeg transcoding

Before writing the function, you must first learn how to use ffmpeg, otherwise the function cannot be written

1. ffmpeg tool software

First of all, we need to download the ffmpeg tool. Here is the download address. The download speed is relatively fast. If you feel that the download is slow, you can chat with me privately. I can send the software package to you or download it directly in my resources
FFmpeg official website address: www.ffmpeg.org/
FFmpeg download address: www.ffmpeg.org/download.html

2. Usage

Syntax:

ffmpeg -i Source file path and file name file path and name after transcoding

Example:

ffmpeg -i D:\test\abc.flv    D:\test\abc.mp4

The implementation is so simple. I suggest you test this tool on the system command line to see if it can meet your requirements. If you want to know the full usage of ffmpeg, you can check it on the official website document

python calls ffmpeg

In fact, it's quite simple. You only need to import the OS module in python. The following code is written directly

import os
dir = os.getcwd()    
#Get the current file path, because I put the ffmpeg tool in the code path here, so I need to get the current path, which is written according to your actual situation
dir2 = '/windows-ffmpeg/x64/ffmpeg.exe'    #Specific location of ffmpeg
ff = dir+dir    #Combined path
result = eval(repr(ff).replace('\\','/'))
#Convert backslash to slash. Because the obtained path is backslash, it needs to be converted to slash. After conversion, it will be found that it is double slash, so it needs to be converted next
ff = result.replace('//',' / '# double slash to but slash
cmd = ff +' -i '+'D:\test\abc.flv' +' -c copy ' + 'D:\test\abc.mp4'
#Write the command to be executed
os.system(cmd)    #Execute system commands, that is, transcoding

OK, execute the above code to transcode directly. Note that the path location must be correct!
With windows and functions, the back is splicing

achievements

If you don't talk too much nonsense, you'll get the result directly:

import tkinter as tk
import os

wind=tk.Tk()    
wind.geometry("800x400")    
wind.title("Video transcoding")    

title_lab=tk.Label(wind,text="Video transcoding",font="Imitation song 20 bold")
title_lab.place(x=350,y=30)    

textlab=tk.Label(wind, text="Please enter the video address:", font="Imitation song 20 bold", fg="blue", width=20)
textlab.place(x=0,y=100)
text_entry=tk.Entry(wind, width=30,font="Imitation song 20 bold")    
text_entry.place(x=300,y=100)    

mzlab=tk.Label(wind, text="Please enter a new video name:", font="Imitation song 20 bold",fg="blue", width=18)
mzlab.place(x=0,y=200)
mz_entry=tk.Entry(wind, width=30,bg="white",font="Imitation song 20 bold")
mz_entry.place(x=300,y=200)

#The following refers to a method of transcoding and declares the execution result
def getTextInput():    
    lianjie = text_entry.get()
    mz = mz_entry.get()
    mz = mz + '.mp4'
    ff = '/windows-ffmpeg/x64/ffmpeg.exe'
    ff1 = os.getcwd()
    ff = ff1+ff
    result = eval(repr(ff).replace('\\','/'))
    ff = result.replace('//','/')
    cmd = ff +' -i '+lianjie+' -c copy '+mz

    if os.system(cmd) == 0:     #Judge the execution result and transcode
        test1=tk.Tk()    
        test1.geometry("200x100")    
        test1.title("")    
        title_lab=tk.Label(test1,text="Video transcoding succeeded",font="Imitation song 20 bold")    
        title_lab.place(x=0,y=0)    
        test1.mainloop()
    else:
        test2=tk.Tk()    
        test2.geometry("200x100")    
        test2.title("")    
        title_lab=tk.Label(test2,text="Video transcoding failed",font="Imitation song 20 bold")    
        title_lab.place(x=0,y=0)    
        test2.mainloop()

btn=tk.Button(wind,text="Submit", font="Imitation song 20 bold", fg="blue", width=8,command=getTextInput)    #Submit the information to the getTextInput method above
btn.place(x=350,y=300)
wind.mainloop()

Double click the python file and two windows will appear. One is for inputting information and the other is for displaying error messages. This tool needs to be improved. I hope the great God of Python can improve it


Note: This tool needs to be noted that if you don't want to change the code and use it directly, you can directly put the ffmpeg tool and your code file together. In addition, if you don't write transcoding, the file path will be saved to the place where the code file is stored by default
The source code has been uploaded to my resource. Resource address: download.csdn.net/download/KH_FC/1...

Topics: Python