The short video SDK provides tools to parse video files and obtain video thumbnails to help users edit and record videos with ease.
Supported editions
Edition | Supported |
---|---|
Professional | Yes |
Standard | Yes |
Basic | Yes |
Video file parsing
The core class of video file parsing is NativeParser, which is used to parse the information about a video, such as the video width, height, format, duration, bitrate, and channel.
You can use keys to obtain the information about a video. The following table describes the keys.
Key | Description |
---|---|
VIDEO_STREAM_INDEX | The subscript of the video stream. |
VIDEO_CODEC | The type of the encoder. |
VIDEO_DURATION | The duration of the video stream. |
VIDEO_FRAME_COUNT | The total number of frames. |
VIDEO_BIT_RATE | The average bitrate of the video stream. |
VIDEO_WIDTH | The width of the video. |
VIDEO_HEIGHT | The height of the video. |
VIDEO_FORMAT | The format of the video. |
VIDEO_GOP | The keyframe interval. |
AUDIO_CODEC | The subscript of the audio stream. |
AUDIO_DURATION | The duration of an audio stream. |
AUDIO_CHANNELS | The bitrate of the audio stream. |
AUDIO_BIT_RATE | The number of audio channels. |
AUDIO_SAMPLE_RATE | The audio sampling rate. |
AUDIO_FORAMT | The audio format. |
//1. Initialize an instance.
NativeParser parser = NativeParser();
//2. Parse videos.
// Parse Video 1.
parser.init(path1);
// Use the keys to obtain the information about the video.
String videoWidth1 = parser.getValue(NativeParser.VIDEO_WIDTH);
// Call the release method after the parsing is complete.
parser.release();
// Parse Video 2.
parser.init(path2);
// Use the keys to obtain the information about the video.
String videoWidth2 = parser.getValue(NativeParser.VIDEO_WIDTH);
// Call the release method after the parsing is complete.
parser.release();
//3. Destroy the instance.
parser.dispose();
Video thumbnails
The core class of video thumbnails is AliyunIThumbnailFetcher. You can display the thumbnail of each frame on the video track. Video thumbnails are used when you edit or crop videos.
// 1. Create an instance.
// AlivcSdkCore.register(getApplicationContext()): Before you create a thumbnail, you must initialize the short video SDK.
AliyunIThumbnailFetcher fetcher = AliyunThumbnailFetcherFactory.createThumbnailFetcher();
//2. Add a video or an image source.
// 2.1 Method 1: Specify a video or an image source by using the configuration file. This method is applicable to the project.json file generated when you use SDKs.
fetcher.fromConfigJson(xxxx.json);
// 2.2 Method 2: Add a video or an image source.
// Add a video.
fetcher.addVideoSource(path1, startTimeMills1, endTimeMills1, overlapDurationMills1);
// Add an image.
fetcher.addImageSource(path2, startTimeMills2, endTimeMills2, overlapDurationMills2);
//3. Set the information about the thumbnail such as output size.
//cropMode
CropMode cropMode=CropMode.Mediate; // The cropping mode of the image.
VideoDisplayMode videoDisplayMode=VideoDisplayMode.Scale; // The padding mode of the image.
int cacheSize=10; // The number of cached thumbnails.
fetcher.setParameters(width, height,cropMode, videoDisplayMode, cacheSize);
// If you want to request a series of thumbnails, we recommend that you set fastMode to true. If you want to request a thumbnail or thumbnails at a specific point in time, set fastMode to false.
fetcher.setFastMode(true);
//4. The final thumbnail is returned in the callback.
long[] times = {1000, 2000, 3000}; // The timestamp of the thumbnail.
fetcher.requestThumbnailImage(long[] time, new AliyunIThumbnailFetcher.OnThumbnailCompletion() {
@Override
public void onThumbnailReady(Bitmap frameBitmap, long time,int index) {
// The thumbnail returned.
}
@Override
public void onError(int errorCode) {
// The information about error code.
}
});
// You can also use requestThumbnailImage(count,callback) to evenly divide the number of thumbnails requested in a period of time.
fetcher.requestThumbnailImage(10, new AliyunIThumbnailFetcher.OnThumbnailCompletion() {
@Override
public void onThumbnailReady(Bitmap frameBitmap, long time, int index) {
// The time parameter indicates a timestamp, which corresponds to the value in the times parameter. The value of the index parameter indicates the subscript of the time slice array.
}
@Override
public void onError(int errorCode) {
}
});
// 5. Destroy the instance.
fetcher.release()
If you set setFastMode() to true, the fast mode is used. In this mode, the thumbnail can be quickly obtained by taking the most recent key frame of the requested time slice. If you set setFastMode() to false, the precise mode is used. In this mode, the specific frame that is accurate to the requested time slice is used as the thumbnail. For example, if you want to obtain 10 thumbnails from a 1080P video that lasts 30 seconds, it takes about 1 second in fast mode and 5 seconds in precise mode.