Python + ffmpeg video transcoding format

Time:2021-7-5

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

crap

At present, I’m also learning python, and I’m not very proficient in it. I can use it to write video transcoding. So I wrote such a simple tool after consulting some materials. This tool can be used by myself. There are still many shortcomings when I use it in the formal environment, and this one is not perfect, Also hope to understand Python God can give perfect oh. There’s no more nonsense.

method

At the beginning, when I wanted to write a video transcoding tool, I was thinking about whether to write in PHP or in Python. Finally, I chose Python because I ran into a wall in PHP, and I still can’t solve that problem. The bug that appears after PHP is written is that there will be a request timeout problem when transcoding big video, and I still can’t solve this bug, I tried to modify the request time and cache in the PHP configuration file, but some methods didn’t solve it. I’m really going crazy. I’ll try again later to see if Python will have this problem. As a result, I really don’t have this problem.

Since it’s about writing ideas, of course, it’s about how to transcode video. At the beginning, I wanted to use the flash framework. At last, I thought about the trouble of using the flash framework on the web page. It’s better to use the windows frame to transcode video directly and double-click the code directly. Finally, the initial decision is to use pythontkinterModule to get, directly write a Windows GUI, he does not incense. With the display mode, transcoding is behind. How to transcode? After consulting a lot of websites, I finally decided to use themFFmpegTranscoding tools, tools selected, is the matter behind the code. There are some problems in the process of writing later, which is to choose ffmpeg module or call ffmpeg program? I’m a network engineer, so I’m very sensitive to code. I have a headache when it’s difficult. I checked the usage of the module and the program respectively[Python] ffmpeg module processes video and audio informationThis article, to be honest, is really good. It’s just that I can’t understand it. It’s too abstruse. It’s estimated that I’ll be able to understand it after a few years of studying python. The blogger will wait for me. Don’t delete the article. I went to the official website to see the documentFfmpeg official websiteThis is quite simple. I can understand it. I also found that video transcoding can be carried out. Well, when there is a method, that’s the idea behind.

thinking

In fact, this idea is quite simple, but what I want is to double-click the Python code I wrote to pop up a Windows window directly, and then I enter the original video address or path, and then enter a transcoding name, and then make a transcoding button to transcode directly. No problem, clear thinking, the following is the direct opening.

Making windows with Python + Tkinter

First of all, since I’m going to get this window, of course we need to knowtkinterHow to use this module, I summarize some simple usage

Examples

Import Tkinter # import tkubter module

Implementation results:

Python + ffmpeg video transcoding format

There is such a simple windows window. There are many uses of Tkinter module. I won’t elaborate here. Let’s continue with our transcoding.

The first is to create 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 TK

The final effect picture:

Python + ffmpeg video transcoding format

The display has been written. It’s time to make a function later

Ffmpeg transcoding

Before writing the function, you have to learn how to use ffmpeg, otherwise the function can’t 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 from my resources

Ffmpeg official website address: www.ffmpeg.org/

Ffmpeg download address: www.ffmpeg.org/download.html

2. Usage

Grammar:

Ffmpeg - I source file path and file name

Example:

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

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

Calling ffmpeg in Python

In fact, it’s quite simple. You just need to import the OS module in Python. The following is the code

import os

OK, execute the above code directly transcoding, here we need to pay attention to the location of the path must be right Oh!

With the windows and functions, the splicing is behind

achievements

If you don’t talk too much nonsense, you can get the result directly

import tkinter as tk

Double click the python file, there will be two windows, one is used to input information window, one is used to display error information window, this tool needs to be improved, hope to have the great God of Python can be improved

Python + ffmpeg video transcoding format

Python + ffmpeg video transcoding format

be carefulNote that if you don’t want to change the code and use it directly, you can directly put ffmpeg tool and your code file together, and if you don’t write transcoding, the file path will be saved to the place where the code file is stored by default

Source code uploaded to my resources, resource address:download.csdn.net/download/KH_FC/1…

This work adoptsCC agreementReprint must indicate the author and the link of this article