Mobile native

Video Util: Transcoding Media Files

This tutorial demonstrates how you can transcode and trim video files.

This feature is supported in mobile applications only.

Warm-up

Become familiar with the Video Util API basics by learning about:

Follow-up

Once we have learned the basics of the Video Util API, we can now move on to more advanced tasks, including:

Initializing Video Utilities

Before using the video util:

  1. 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>
    
  2. Declare a variable, which identifies the video util handle used for transcoding:
    video_util_h *handle;
  3. Create the handle:

    video_util_h video_h = NULL;
    ret = video_util_create(&video_h);
    
  4. If the handle is created normally, set the input files with the video_util_set_file_path() function:

    int video_util_set_file_path(video_util_h handle, const char *path);
    
  5. Set the file format with the video_util_set_file_format() function. The video_util_file_format_e enumeration defines the available file formats.

    video_util_h video_h = NULL;
    ret = video_util_create(&video_h);
    ret = video_util_set_file_format(video_h, VIDEO_UTIL_FILE_FORMAT_3GP);
    
  6. Set the video codec with the video_util_set_video_codec() function. The video_util_video_codec_e enumeration defines the available video codecs.

    video_util_h video_h = NULL;
    ret = video_util_create(&video_h);
    ret = video_util_set_video_codec(video_h, VIDEO_UTIL_VIDEO_CODEC_MPEG4);
    
  7. Set the audio codec with the video_util_set_audio_codec() function. The video_util_audio_codec_e enumeration defines the available audio codecs.

    video_util_h video_h = NULL;
    ret = video_util_create(&video_h);
    ret = video_util_set_audio_codec(video_h, VIDEO_UTIL_AUDIO_CODEC_AAC);
    
  8. Set the seek mode with the video_util_set_accurate_mode() function.

    If the accurate mode is enabled, the user can get an accurate frame for the given duration in the video_util_start_transcoding() function.

    video_util_h video_h = NULL;
    ret = video_util_create(&video_h);
    
    ret = video_util_set_accurate_mode(video_h, 0);
    
  9. Set the resolution with the video_util_set_resolution() function:

    video_util_h video_h = NULL;
    ret = video_util_create(&video_h);
    
    ret = video_util_set_resolution(video_h, 176, 144);
    

    If transcoding is done with the original input ('trim'), both the width and height are 0.

  10. Set the FPS with the video_util_set_fps() function:

    video_util_h video_h = NULL;
    ret = video_util_create(&video_h);
    
    ret = video_util_set_fps(video_h, 10);
    
  11. When the handle is no longer needed, free the memory:

    error_code = video_util_destroy(handle);
    

Managing Transcoding

To manage the transcoding process:

  1. If the prerequisites are set normally, start the transcoding with the video_util_start_transcoding() function.

    ret = video_util_start_transcoding(video_h);
    
  2. While transcoding:

    • Call the video_util_get_progress_transcoding() function to get the current progress.
    • Call the video_util_cancel_transcoding() function to cancel the transcoding process.

      The function uses a handle created using the video_util_create() function. If you call this function after the complete transcoding callback is TRUE, you cannot cancel the transcoding.

      video_util_h video_h = NULL;
      ret = video_util_create(&video_h);
      
      ret = video_util_cancel_transcoding(video_h);
      
  3. When the transcoding returns a complete callback, destroy the handle with the video_util_destroy() function.

    This function destroys the handle of a completed transcoding. The function contains the cancel function, if it is called during the transcoding process.

    If you do not call the destroy function after completing the transcoding, new transcoding is not possible, and a memory crash can occur.

Setting Parameters

To set the basic parameters of the video util:

  1. Initialize the video utilities.
  2. Check the supported file format.
    1. Check the supported file formats:

      error_code = video_util_foreach_supported_file_format(handle, video_util_file_supported, NULL);
      

      The first parameter refers to the handle of the video util, and the second parameter to the callback function to be invoked.

    2. Define the callback function:

      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;
      }
      
  3. Check the supported codecs.
    1. Check the supported audio and video codecs:

      video_util_foreach_supported_video_codec(handle, video_util_video_supported, NULL);
      
      video_util_foreach_supported_audio_codec(handle, video_util_audio_supported, NULL);
      

      The first parameter refers to the handle of the video util, and the second parameter to the callback function to be invoked.

    2. Define the callback functions:

      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;
      }
      
      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;
      }
      

    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");
    
  4. Set codecs.

    To set the audio and video codecs:

    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);
    
  5. Set the resolution.

    To set the video resolution:

    The first parameter refers to the handle of the video util, the second to the media width (if the width is 0, it is set to the original size where the minimum value is 128), and the third to the height of the media (if the height is 0, it is set to its original size, the minimum value being 96).

    int error_code = video_util_set_resolution(handle, 640, 480);
    
  6. Set the FPS.

    To set the frames per second (FPS):

    The first parameter refers to the handle of the video util and the second to the frame rate. The frame rate minimum value is 5 and the maximum value is 30.

    int error_code = video_util_set_fps(handle, 25);
    

Monitoring the Transcoding Progress

To get the progress of the video util:

  1. Initialize the video utilities.
  2. Execute the transcoding first.

    Check whether the transcoding works normally:

    error_code = video_util_start_transcoding(video_util_h handle, unsigned long start, unsigned long duration, 
                                              const char *out_path, video_util_transcoding_progress_cb progress_cb, 
                                              video_util_transcoding_completed_cb completed_cb, void *user_data);
    

    The first parameter refers to the handle of the video util, the second and third parameter to the argument for seek, the fourth parameter to the output path, and the fifth and sixth parameter to the callback function to be invoked and its parameter.

  3. To get the transcoding progress, execute the video_util_get_progress_transcoding() function within the progress_cb callback of the video_util_start_transcoding() function:
    video_util_get_progress_transcoding(video_util_h handle, unsigned long *current_position, unsigned long *duration);
    
Go to top