In the current app, you can see the video everywhere, for example, in social apps, users can shoot their own small video, and publish to the corresponding column, increase the opportunity to interact with friends.
The following are some of the common video processing in the background:
· Video, a picture displayed on many video lists, which is obtained by capturing a frame (usually the first frame) of a video.
· Video watermark, for the purpose of copyright protection, you need to add a watermark.
· Video transcoding allows users to upload video on their phone and convert it to the format supported by the app backend.
Video processing is a strange area for most programmers, and here is a video processing tool that is most commonly used: FFmpeg.
(1) ffmpeg introduction
FFmpeg's official website (http://ffmpeg.org/) is described in this way: Acomplete, Cross-platform solution to record, convert and stream audio and video. A set of open-source computer programs that can be used to record, convert, and transform digital audio and video into streams.
FFmpeg is a cross-platform software that can be used under Linux or under Window,mac.
The project was first initiated by Fabricebellard and is now maintained by Michael Niedermayer. Many ffmpeg developers come from the MPlayer project, and the current ffmpeg is also placed on the server in the MPlayer project group. The name of the project comes from the MPEG video encoding standard, and the preceding "FF" stands for "Fast Forward".
Famous player KMPlayer, Storm av, QQ av, have used ffmpeg code. Domestic seven Qiniu storage, the core module of audio and video processing is also ffmpeg.
The features that ffmpeg can implement are:
L Video Capture
L Audio/Video format conversion
L Video Capture Image
L Add watermark to video
FFmpeg mainly consists of a few parts:
Libavcodec: Contains a library of all FFmpeg audio and video codecs. To ensure optimal performance and high reusability, most codecs are developed from scratch.
Libavformat: Contains all the common audio-visual format of the parser and generator library.
Three instances of the program:
FFmpeg: command-line video format translator (this file is typically called directly)
Ffplay: Video playback program. (Requires SDL support)
Ffserver: Multimedia Server
(2) function of program call FFmpeg
Using FFmpeg for video conversion is simple, for example, converting AVI to MP4, using the command line:
Ffmpeg-i source.avi-f psp-r 29.97-b 768k-ar 24000-ab
64k-s Destination.mp4
So, in the background language, how should you call FFmpeg for format conversion?
A common idea is to construct the above command line by constructing the command line, and then invoke the FFmpeg execution file in the background language.
There is a Java call FFmpeg open source project Jave (home: http://www.sauronsoftware.it/projects/jave/) That's how it's done.
For example, the following example is an AVI converted into FLV:
[Java]View Plaincopy
- File Source = NewFile ("Source.avi");
- File target = NewFile ("target.flv");
- Audioattributes audio = Newaudioattributes ();
- Audio.setcodec ("Libmp3lame");
- Audio.setbitrate (Newinteger (64000));
- Audio.setchannels (new Integer (1));
- Audio.setsamplingrate (Newinteger (22050));
- Videoattributes video = Newvideoattributes ();
- Video.setcodec ("flv");
- Video.setbitrate (Newinteger (160000));
- Video.setframerate (new Integer (15));
- Video.setsize (new Videosize (300));
- Encodingattributes attrs = Newencodingattributes ();
- Attrs.setformat ("flv");
- Attrs.setaudioattributes (audio);
- Attrs.setvideoattributes (video);
- Encoder Encoder = new Encoder ();
- Encoder.encode (Source,target, attrs);
The related parameters are set by Class Audioattributes and Videoattributes, and these parameters are constructed in class encoder to execute ffmpeg related commands in the command line.
Note that video audio-related operations are generally very CPU-intensive, such as the above example, in the process of converting AVI into FLV, the relevant resources of the system are as follows:
As you can see, FFmpeg takes up almost 90% of the CPU's resources.
The backend needs a high-performance server cluster for fast video processing, but this is a very cost-effective way for start-ups. Therefore, the use of cloud services can be fully utilized, for example, the service provided by seven cattle includes the conversion of audio and video formats, which is very convenient to call.
Scenarios for app back-end processing video