This paper refers to: http://blog.csdn.net/jyt0551/article/details/56942795
For a bare RTSP URL, it is slightly monotonous and boring to store it on the playlist. You can see that EasyPlayer saves a picture to the list after playing the video.
So how does this function work?
If you decode by yourself, such as using ffmpeg decoding, in this case, decoding video frames and then encoding them into jpeg to save, it should not be difficult. I believe most players deal with this way.
Video stream in H264 format=> decoding=> YUV format=> compression=> jpeg=> save to local
But if we use hard decoding, it's a pity that Android's hard decoding doesn't provide the function of capturing video frame data, how can we do that?
There are two ways to implement hard-decoding screenshots
- Create a separate soft decoder for grabbing and use the above method to grab the image
- Get the content of TextureView directly and save it
Here is the second method. TextureView provides a getBitmap() method, which is explained as follows:
Returns a Bitmap representation of the content of the associated surface texture.
This method provides the rendering content of the current TextureView and returns it as a Bitbmap object. In this way, we can compress the Bitmap into jpeg, png and other formats and save it. Bitmap provides a compress method for direct compression. Here's how to get and store Bitmap objects from TextureView:
public void takePicture(final String path) {
try {
if (mWidth <= 0 || mHeight <= 0) {
return;
}
Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
mSurfaceView.getBitmap(bitmap);
saveBitmapInFile(path, bitmap);
bitmap.recycle();
} catch (OutOfMemoryError error) {
error.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
The following is the method of saving Bitmap as JPEG. Here, the thumbnails are saved in the album of Android system so that the method of selecting pictures by calling the system can be accessed:
private void saveBitmapInFile(final String path, Bitmap bitmap) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
if (mScanner == null) {
MediaScannerConnection connection = new MediaScannerConnection(getContext(),
new MediaScannerConnection.MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
mScanner.scanFile(path, null /* mimeType */);
}
public void onScanCompleted(String path1, Uri uri) {
if (path1.equals(path)) {
mScanner.disconnect();
mScanner = null;
}
}
});
try {
connection.connect();
} catch (Exception e) {
e.printStackTrace();
}
mScanner = connection;
}
} catch (IOException e) {
e.printStackTrace();
} catch (OutOfMemoryError error) {
error.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Notice here the timing of the getBitmap function call.
- First, you must call it after the TextureView is created. That is, the onSurface Texture Available method can be called only after it is dropped, otherwise the Texture has not been created and the function returns null.
- Not only that, but it also needs to be called after the video is played. Otherwise, the content displayed by TextureView is empty, so the snapshot you save may be pure black.
Introduction to EasyPlayer Project
EasyPlayer & EasyPlayer Pro is a streaming media player project developed and maintained by Easy Darwin team. It currently supports Windows (supporting multiple windows, including ActiveX, npAPI Web plug-in), Android platform, iOS platform, video support H.264, H.265, MPEG4, MJPEG, audio support G711A, G711U, G726, AAC, RTSP over TCP/UDP, RTMP, HTTP, HLS and other protocols. Hardware decoding is an excellent streaming media platform-wide playback component!
Github project address: https://github.com/EasyDarwin/EasyPlayer
https://github.com/EasyDarwin/EasyPlayer_Android
Get more information
Mail: support@easydarwin.org
WEB: www.EasyDarwin.org
Copyright © EasyDarwin.org 2012-2017