Releases: cainmagi/FFmpeg-Encoder-Decoder-for-Python
FFmpeg mpegCoder for Python (Linux)
Instruction of this version
This is the first release version that contains all python built files. The details of the compilation are shown in the following table:
File | FFMpeg | Numpy | Python | GCC/G++ | OS |
---|---|---|---|---|---|
mpegCoder_3_1_0_Linux_py39.tar.xz |
4.4 |
1.21.1 |
3.9.6 |
8.3.0 |
Debian 10 |
mpegCoder_3_1_0_Linux_py38.tar.xz |
4.4 |
1.21.1 |
3.8.11 |
8.3.0 |
Debian 10 |
mpegCoder_3_1_0_Linux_py37.tar.xz |
4.4 |
1.21.1 |
3.7.11 |
8.3.0 |
Debian 10 |
mpegCoder_3_1_0_Linux_py36.tar.xz |
4.4 |
1.19.5 |
3.6.14 |
8.3.0 |
Debian 10 |
mpegCoder_3_1_0_Linux_py35.tar.xz |
4.4 |
1.18.5 |
3.5.10 |
8.3.0 |
Debian 10 |
Note that the above versions only show the environment when building mpegCoder
. It does not mean that they are the dependencies of running mpegCoder
. For example, users could use python 3.9.5
and numpy 1.19.5
to run mpegCoder
.
Running the Linux version may be a little bit tricky. Users need to follow the step 5 and 6 in the instructions of the source code 📄. We copy the instructions here:
-
Extract the built module as
mpegCoder.so
, then you could import it in the same directory. If you have built FFMpeg by our script, you do not need any other dependencies when importing the libs. However, if not, you may need to download the lib dependencies 📦 and add thelib
folder to yourLD_LIBRARY_PATH
:mkdir -p /apps/ffmpeg-4.4 cd /apps/ffmpeg-4.4 wget -O- https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/so-linux-ffmpeg_4_4.tar.xz | tar xJ -C "." echo "export LD_LIBRARY_PATH=/apps/ffmpeg-4.4/lib:\$LD_LIBRARY_PATH" >> ~/.bashrc export LD_LIBRARY_PATH=/apps/ffmpeg-4.4/lib:$LD_LIBRARY_PATH
-
Running
mpegCoder
requiresGLIBC>=2.29
. This requirement is not satisfied in some cases. However, if you have built FFMpeg by our script, the requirement would be fulfilled (i.e. you could skip this step). If users are using our pre-built dependencies, users may need to solve this problem byln -sf /apps/ffmpeg-4.4/lib-fix/libm-2.31.so /lib/x86_64-linux-gnu/libm.so.6
Then, users could import the package by
import mpegCoder
FFmpeg mpegCoder for Python
Instruction of this version
Until now, the newest release of FFMpeg is 4.4
. We have bumped into this version.
From 3.0.0
, the MpegServer
has been implemented, which means all the basic modules are finished now. This is a pre-release branch, where we only provide limited implementations for mpegCoder
:
- Windows 10, Python
3.9.4
&numpy 1.20.1
. - Windows 10, Python
3.6.13
&numpy 1.19.5
. - Windows 10, Python
3.5.2
&numpy 1.13
.
The dynamic libraries could be downloaded from dll-win-ffmpeg_4_4.tar.xz. To make mpegCoder
works, users need to place the dependencies in the same folder of mpegCoder
, for example:
.
|---mpegCoder.pyd
|---avcodec-58.dll
|---avformat-58.dll
|---avutil-56.dll
|---swresample-3.dll
`---swscale-5.dll
Then, users could import the package by
import mpegCoder
Although there is a folder MpegCoder_LinuxVer
in this folder, this folder should not be used for building the Linux mpegCoder
. In the next version, we will separate the Linux implementation to another independent branch.
Dependencies for FFMpeg 4.0 ~ 4.4
This release is used for storing the pre-compiled dependencies, include files and libs for FFMpeg. The version includes:
FFMpeg | include | libs | dynamic libs |
---|---|---|---|
4.4 (windows) | ✔️ | ✔️ | ✔️ |
4.4 (linux) | ✔️ | ✔️ | ✔️ |
There are also some patches for using FFMpeg:
Patch | Description |
---|---|
patch-ffmpeg_4_4.tar.xz |
Used for fixing the NVCC bug when configuring FFMpeg 4.4. |
Some of the dependencies may be automatically downloaded by some scripts.
Backup for pre-built ffmpeg
The following archives are the pre-built ffmpeg on Linux. Because the FFMpeg does not release the shared library version for Linux. We have to use the scripts to build them. The following files are not actually used for building mpegCoder
, but the dependencies are extracted from them.
File name | FFMpeg ver. | GCC ver. | OS |
---|---|---|---|
linux-ffmpeg-shared-4.4.tar.xz |
4.4 |
9.3.0 |
Ubuntu 20.04.2 |
FFmpeg mpegCoder for Python
Instruction of this version
An important notice is that this version is based on FFmpeg 4.0. This version is released recently, some practices show that the Ubuntu lib could not be imported by python3.5 after building the newest version (Have been fixed).
In this release we provide three versions.
- Windows 10, Python 3.6.4 & numpy 1.14.
- Windows 10, Python 3.5.2 & numpy 1.13.
- Ubuntu 16.04, Python 3.5.2 & numpy 1.14.
We believe that this release would help users who use tensorflow 1.5~1.7. Also we have no more special reports for this version. If you want to know how to use this release, you could still refer the instructions of ver 1.8 and ver 2.0.
Noted that in ver 2.0 we firstly provide the MpegClient for reading remote rtsp stream. But now we find that a major bug exists in ver 2.0 and ver 2.01. This bug would cause the memory leak with extracting more and more frames. Once you call ExtractFrame(), the memory leak happens. Fortunately, in this version we have fixed it. So indeed this is the first version that could be used in practice.
Instructions for Windows version
Users need to confirm that all .dll
files are placed in the same location of mpegCoder.pyd
. After that, users could enter that folder, and launch the module with
import mpegCoder
Instructions for Linux version
The required dynamic libraries have been packed with the Linux package. After extracting the file, users should get
.mpegCoder_2_05_Linux_py35.tar.xz
|---mpegCoder.so
`---dependencies
`---...
The files in ./dependencies
are all required dynamic libraries for importing mpegCoder
. We recommend users move this folder to a proper path, and add this folder in LD_LIBRARY_PATH
. Users could also move these dynamic libs to their /usr/libs
or /usr/local/libs
to make them work.
After that, users could place mpegCoder.so
to their working directory, and use
import mpegCoder
to load the module.
FFmpeg mpegCoder for Python
Instruction of this version
An important notice is that this version is based on FFmpeg 3.4.2, Python 3.6.4 and numpy 1.14. The prior versions of Python could not use this version (including Python 3.5, which is supported until ver 1.8). And we have no more special reports for this version. If you want to know how to use this release, you could still refer the instructions of ver 1.8 and ver 2.0.
Noted that we does not provide the Linux version in this update, and the Linux source code of this version is still for ver 2.0. In the future we may add the Linux release package.
FFmpeg mpegCoder for Python
Instruction of this version
We still need to separate the Win and Linux versions of this project. The comments of how to compile this project in different environments have been shown in the introduction of v1.8 release.
You could still find source codes, dependency for win and compiled libraries here. If you are just an user of this project, you just need to download the binary (compiled) library.
Update Report
ver 2.0 creation report
-
Revise the bug of the encoder which may cause the stream duration is shorter than the real duration of the video in some not advanced media players.
-
Improve the structure of the code and remove some unnecessary codes.
-
Provide a complete version of client, which could demux the video stream from a server in any network protocol.
An example of demuxing the video streamer from a server:
d = mpegCoder.MpegClient() # create the handle d.setParameter(dstFrameRate=(5,1), readSize=5, cacheSize=12) # normalize the frame rate to 5 FPS, and use a cache which size is 12 frames. Read 5 frames each time. success = d.FFmpegSetup(b'rtsp://localhost:8554/video') if not success: # exit if fail to connect with the server exit() d.start() # start the sub-thread for demuxing the stream. for i in range(10): # processing loop time.sleep(5) p = d.ExtractFrame() # every 5 seconds, read 5 frames (1 sec.) # do some processing d.terminate() # shut down the current thread. You could call start() and let it restart. d.clear() # Disconnect with the stream.
FFmpeg mpegCoder for Python
Instruction of this version
In the master
branch, we provide both the codes of VS (Win ver.) and those for g++ (Linux ver.). You need extra libraries to compile this project:
Windows
We use Visual Studio (VS) Community 2017 to compile this project.
After extracting the source-code
, you need to extract the attached package ffmpeg-dependency-win
in the root folder. The arranged file struct should be like this:
.
|-- docs # doc files of master branch
|-- include # included .h files of ffmpeg
|-- lib # libraries of ffmpeg
|-- MpegCoder # source code folder of Windows
|-- MpegCoder_LinuxVer # source code folder of Linux (not necessary in this platform)
| |-- MpegCoder # source code folder
| `-- setup.py # compile script
`-- MpegCoder.sln # the solution file of VS 2017
Then you may open the .sln
project and redirect the path of libraries of Python 3.5 & numpy 1.13 in settings. After that, could could compile this project successfully.
Linux
You only need the sub-folder MpegCoder_LinuxVer
in the source codes as what we describe in Win compile instructions. It should contain:
MpegCoder_LinuxVer # source code folder of Linux
|-- MpegCoder # source code folder
`-- setup.py # compile script based on python3
Because I would not provide dependency here (you could use newer version of ffmpeg to compile this project), you need to clone and compile the ffmpeg as the follow instructions:
-
Check every pack which ffmpeg needs here: Dependency of FFmpeg
-
Use these steps to install ffmpeg instead of provided commands on the above site.
$ git clone https://git.ffmpeg.org/ffmpeg.git $ cd ffmpeg $ ./configure --prefix=host --enable-gpl --enable-libx264 --enable-libx265 --enable-shared --disable-static --disable-doc $ make $ make install
-
Revise the paths defined in
setup.py
, especially that ofFFMPEG_DIR
. You need to redirect the path to where your codes are built (generally inffmpeg/host
). Certainly, you also need the revise the other paths like that ofPython3.5
. -
You could find the
.so
files inffmpeg/host/lib
. These files are depending dynamic libraries of our project. Now you could compile this project.
Update Report
ver 1.8 update report
Add source codes and instructions of compile this project.
ver 1.8 creation report
In this version, we have such improvements:
-
Provide options (widthDst, heightDst) to let MpegDecoder could control the output size manually. To ensure the option is valid, we must use the method
setParameter
before 'FFmpegSetup'. Now you could use this options to get a rescaled output directly:d = mpegCoder.MpegDecoder() # initialize d.setParameter(widthDst=400, heightDst=300) # noted that these options must be set before 'FFmpegSetup'! d.FFmpegSetup(b'i.avi') # the original video size would not influence the output print(d) # examine the parameters. You could also get the original video size by 'getParameter' d.ExtractFrame(0, 100) # get 100 frames with 400x300
In another example, the set optional parameters could be inherited by encoder, too:
d.setParameter(widthDst=400, heightDst=300) # set optional parameters ... e.setParameter(decoder=d) # the width/height would inherit from widthDst/heightDst rather than original width/height of the decoder.
Noted that we do not provide
widthDst
/heightDst
ingetParameter
, because these 2 options are all set by users. There is no need to get them from the video metadata. -
Optimize some realization of Decoder so that its efficiency could be improved.
FFmpeg mpegCoder for Python
mpegCoder
This is the first release version of FFmpeg mpegCoder for python, just download it and use it in your video processing script!
__ _ _ _ _ ,___
( / / / o ( / ) ) / / /
(__/ , , _, /_ _ _ _' ( / / / ,_ _ _, / __ __/ _ _
_/_(_/_(__/ /_(/_/ / /_/_)_ / / (__/|_)_(/_(_)_(___/(_)(_/_(/_/ (_
// /| /|
(/ (/ (/
Yuchen's Mpeg Coder - Readme
This is a mpegcoder adapted from FFmpeg & Python-c-api.Using it you could get access to processing video easily. Just use it as a common module in python like this.
import mpegCoder
Noted that this API need you to install numpy.
An example of decoding a video in an arbitrary format:
d = mpegCoder.MpegDecoder()
d.FFmpegSetup(b'inputVideo.mp4')
p = d.ExtractGOP(10) # Get a gop of current video by setting the start position of 10th frame.
p = d.ExtractGOP() # Get a gop of current video, using the current position after the last ExtractGOP.
d.ExtractFrame(100, 100) # Extract 100 frames from the begining of 100th frame.
An example of transfer the coding of a video with an assigned codec:
d = mpegCoder.MpegDecoder()
d.FFmpegSetup(b'i.avi')
e = mpegCoder.MpegEncoder()
e.setParameter(decoder=d, codecName=b'libx264', videoPath=b'o.mp4') # inherit most of parameters from the decoder.
opened = e.FFmpegSetup() # Load the encoder.
if opened: # If encoder is not loaded successfully, do not continue.
p = True
while p is not None:
p = d.ExtractGOP() # Extract current GOP.
for i in p: # Select every frame.
e.EncodeFrame(i) # Encode current frame.
e.FFmpegClose() # End encoding, and flush all frames in cache.
d.clear() # Close the input video.
For more instructions, you could tap help(mpegCoder)
.