Video Util: Transcoding Media Files
This tutorial demonstrates how you can transcode video files.
This feature is supported in mobile applications only.
Warm-up
Become familiar with the Video Util API basics by learning about:
-
Initializing the Video Utilities
Initialize the video utilities for use.
-
Manage and monitor the transcoding process.
Follow-up
Once we have learned the basics of the Video Util API, we can now move on to more advanced tasks, including:
-
Set parameters for the video utilities.
-
Monitoring the Transcoding Progress
Retrieve progress information about the transcoding process.
Initializing the Video Utilities
To initialize the video utilities for use:
-
To use the functions and data types of the Video Util API, include the <video_util.h> header file in your application:
#include <video_util.h>
-
Create a video util handle for managing the transcoding:
video_util_h video_h = NULL; ret = video_util_create(&video_h);
-
If the video util handle is successfully created, set the input file path using the video_util_set_file_path() function:
ret = video_util_set_file_path(video_h, *path);
-
Set the file format using the video_util_set_file_format() function:
ret = video_util_set_file_format(video_h, VIDEO_UTIL_FILE_FORMAT_3GP);
The video_util_file_format_e enumeration defines the available file formats.
-
Set the audio codec using the video_util_set_audio_codec() function:
ret = video_util_set_audio_codec(video_h, VIDEO_UTIL_AUDIO_CODEC_AAC);
The video_util_audio_codec_e enumeration defines the available audio codecs.
-
Set the video codec using the video_util_set_video_codec() function:
ret = video_util_set_video_codec(video_h, VIDEO_UTIL_VIDEO_CODEC_MPEG4);
The video_util_video_codec_e enumeration defines the available video codecs.
-
Enable or disable the accurate seek mode using the video_util_set_accurate_mode() function.
If you enable this mode, you can get an accurate frame for a given duration in the video_util_start_transcoding() function.
ret = video_util_set_accurate_mode(video_h, 0);
-
Set the video resolution using the video_util_set_resolution() function:
ret = video_util_set_resolution(video_h, 176, 144);
If the transcoding is performed with the original input ("trim"), both the width and height are 0.
-
Set the video frame rate using the video_util_set_fps() function:
ret = video_util_set_fps(video_h, 10);
-
When no longer needed, destroy the video util handle using the video_util_destroy() function:
ret = video_util_destroy(video_h);
Managing Transcoding
To manage and monitor the transcoding process:
-
Start the transcoding using the video_util_start_transcoding() function:
ret = video_util_start_transcoding(video_h);
-
Monitor and manage the transcoding:
-
To retrieve the current transcoding progress, use the video_util_get_progress_transcoding() function. The function returns the current transcoding position (second parameter) and the duration of the transcoding (third parameter).
unsigned long current_position; unsigned long duration; ret = video_util_get_progress_transcoding(video_h, *current_position, *duration);
-
To cancel the transcoding process, use the video_util_cancel_transcoding() function. You must call this function before the transcoding complete callback is invoked.
ret = video_util_cancel_transcoding(video_h);
-
-
After the transcoding complete callback is invoked, destroy the video util handle using the video_util_destroy() function:
ret = video_util_destroy(video_h);
The function destroys the handle of a completed transcoding. If the function is called during the transcoding process, the function also cancels the transcoding.
Note If you do not call the destroy function after the transcoding is complete, you cannot start a new transcoding process, and a memory crash can occur.
Setting Parameters
To set parameters for the video utilities (for other parameters, see Initializing the Video Utilities):
- Set the file format:
-
Check the supported file formats:
-
Check the supported file formats using the video_util_foreach_supported_file_format() function:
error_code = video_util_foreach_supported_file_format(handle, video_util_file_supported, NULL);
The second parameter specifies the callback that is invoked separately for each supported file format.
-
Define the callback:
static bool video_util_file_supported(video_util_file_format_e format, void *user_data) { dlog_print(DLOG_INFO, LOG_TAG, "format %s\n", format == VIDEO_UTIL_FILE_FORMAT_3GP? "VIDEO_UTIL_FILE_FORMAT_3GP": format == VIDEO_UTIL_FILE_FORMAT_MP4? "VIDEO_UTIL_FILE_FORMAT_MP4": "VIDEO_UTIL_FILE_FORMAT_MAX"); return 0; }
The video_util_file_format_e enumeration defines the available file formats.
-
-
Set the file format using the video_util_set_file_format() function:
ret = video_util_set_file_format(video_h, VIDEO_UTIL_FILE_FORMAT_3GP);
-
- Set the audio and video codec:
-
Check the supported audio and video codecs:
-
Check the supported audio and video codecs using the video_util_foreach_supported_audio_codec() and video_util_foreach_supported_video_codec() functions:
video_util_foreach_supported_audio_codec(handle, video_util_audio_supported, NULL); video_util_foreach_supported_video_codec(handle, video_util_video_supported, NULL);
The second parameter of both functions specifies the callback that is invoked separately for each supported codec.
-
Define the callbacks:
static bool video_util_audio_supported(video_util_audio_codec_e codec, void *user_data) { dlog_print(DLOG_INFO, LOG_TAG, "audio codec %s\n", codec==VIDEO_UTIL_AUDIO_CODEC_AAC? "VIDEO_UTIL_AUDIO_CODEC_AAC": codec==VIDEO_UTIL_AUDIO_CODEC_AMRNB? "VIDEO_UTIL_AUDIO_CODEC_AMRNB": "VIDEO_UTIL_AUDIO_CODEC_NONE"); return 0; } static bool video_util_video_supported(video_util_video_codec_e codec, void *user_data) { dlog_print(DLOG_INFO, LOG_TAG, "video codec %s\n", codec == VIDEO_UTIL_VIDEO_CODEC_MPEG4? "VIDEO_UTIL_VIDEO_CODEC_MPEG4": codec == VIDEO_UTIL_VIDEO_CODEC_H263? "VIDEO_UTIL_VIDEO_CODEC_H263": codec == VIDEO_UTIL_VIDEO_CODEC_H264? "VIDEO_UTIL_VIDEO_CODEC_H264": "VIDEO_UTIL_VIDEO_CODEC_NONE"); return 0; }
The video_util_audio_codec_e and video_util_video_codec_e enumerations define the available audio and video codecs.
To check the support for a specified codec, such as VIDEO_UTIL_VIDEO_CODEC_MPEG4:
static bool video_util_video_supported(video_util_video_codec_e codec, void *user_data) { if (codec == VIDEO_UTIL_VIDEO_CODEC_MPEG4) { user_data = true; } return 0; } bool supported = false; video_util_foreach_supported_video_codec(handle, video_util_video_supported, &supported); dlog_print(DLOG_INFO, LOG_TAG, "MPEG4 codec %s", supported?"supported":"not supported");
-
-
Set the audio and video codecs using the video_util_set_audio_codec() and video_util_set_video_codec() functions:
error_code = video_util_set_audio_codec(handle, VIDEO_UTIL_AUDIO_CODEC_AAC); error_code = video_util_set_video_codec(handle, VIDEO_UTIL_VIDEO_CODEC_MPEG4);
-
-
Set the video resolution using the video_util_set_resolution() function:
int error_code = video_util_set_resolution(handle, 640, 480);
-
Set the video frame rate using the video_util_set_fps() function:
int error_code = video_util_set_fps(handle, 25);
Monitoring the Transcoding Progress
To retrieve progress information about the transcoding process:
-
Start the transcoding using the video_util_start_transcoding() function:
error_code = video_util_start_transcoding(handle, start, duration, *out_path, progress_cb, completed_cb, *user_data);
The fifth parameter specifies the transcoding progress callback, which is invoked periodically during the transcoding process. The sixth parameter specifies the transcoding complete callback, which is invoked after the transcoding process is complete.
-
Retrieve the current transcoding progress using the video_util_get_progress_transcoding() function within the transcoding progress callback:
error_code = video_util_get_progress_transcoding(handle, *current_position, *duration);