Modifying a Transcoding Template

You can reset template parameters to modify a transcoding template.

Core Code

  1. Create MPC configuration items.
    These configuration items are used for MPC to obtain authorization. Table 1 describes the parameters.
    1
    2
    3
    4
    5
       MpcConfig mpcConfig = new MpcConfig();
           mpcConfig.setEndPoint(endPoint);// Set the endpoint.
           mpcConfig.setProjectId(projectId);// Set the project ID.
           mpcConfig.setSk(sk);// Set the SK.
           mpcConfig.setAk(ak);// Set the AK.
    
    Table 1 MPC parameters

    Parameter

    Type

    Description

    endPoint

    String

    Endpoint. The endpoint of MPC is mts.cn-north-4.myhuaweicloud.com. Replace cn-north-4 with the value of region in the address box of the browser after you select MPC on the management console.

    ProjectId

    String

    Project ID. For details, see Obtaining a Project ID and Account Name.

    ak

    String

    Access key ID (AK). For details, see Obtaining the AK/SK Pair.

    sk

    String

    Secret Access Key (SK) used together with the AK. For details, see Obtaining the AK/SK Pair.

  2. Create an MpcClient instance.

    If no proxy server is configured, you can directly create an MpcClient instance.

    1
    2
         // Create an MpcClient instance.
         MpcClient mpcClient = new MpcClient(mpcConfig);
    

    If a proxy server needs to be configured, set proxy parameters and then transfer the proxy as a constructor to the MpcClient instance.

    1
    2
    3
    4
    5
    6
    7
    8
    // Configure the proxy.
    ClientConfig clientConfig = new ClientConfig();
           clientConfig.setProxyHost(proxyHost);// Set the IP address of the proxy server.
           clientConfig.setProxyPort(Integer.parseInt(proxyPort));// Set the port number of the proxy server.
           clientConfig.setProxyUserName(proxyUserName);// Set the username for accessing the proxy server.
           clientConfig.setProxyPassword(proxyPassword);// Set the password for accessing the proxy server.
    // Use the constructor to initialize MpcClient.
     MpcClient mpcClient =new MpcClient(mpcConfig, clientConfig);
    
  3. Send a request.
    1
    2
    3
    4
    5
    6
    // Set request parameters.
    TransTemplate transTemplate=new TransTemplate();
    // Set the template name.
    transTemplate.setTemplateName("MP4_12222ddd2122");
    // Set the template ID.
    transTemplate.setTenantId("123456");
    
  4. // Set template parameters.
    1. Video parameters
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      TransTemplate.VideoBean videoBean=new TransTemplate.VideoBean();
      
      // Set the video codec. 1 is H.264 and 2 is H.265.
      videoBean.setCodec(1);
      
      
      // Set the average output bitrate (unit: kbit/s). The value is 0 or an integer ranging from 40 to 30,000. If this is 0, the average output bit rate is an adaptive value.
      videoBean.setBitrate(6000);
      
      /*
      * Encoding profile
      * H264_BASE = 1,
      * H264_MAIN = 2,
      * H264_HIGH = 3,
      * H265_MAIN = 4,
      */
      // Set the encoding profile. The recommended value is 3.
      videoBean.setProfile(3);
      
      // Set the encoding level. The value ranges from 1 to 15. The default value is 15.
      videoBean.setLevel(15);
      
      /*
      * Coding quality level (RMS range)
      * HSPEED2 = 1,(only for h.265, h.265 default)
      * HSPEED = 2,(only for h.265)
      * NORMAL = 3,(h264 / h.265 available, h.264 default)
      */
      // Set the encoding quality.
      videoBean.setPreset(3);
      
      // Set the maximum number of reference frames (unit: frame). For H.264, the value ranges from 1 to 8. The default value is 4. For H.265, the value is fixed at 4.
      videoBean.setRefFramesCount(4);
      
      
      // Set the maximum I-frame interval (unit: second). The value ranges from 2 to 5. The default value is 5.
      videoBean.setMaxIframesInterval(5);
      
      // Set the maximum B-frame interval (unit: frame). The value ranges from 0 to 8 for H.264 and is 4 by default. The value is fixed at 7 for H.265.
      videoBean.setbramesCount(4);
      
      // Set the frame rate (unit: FPS). The value is 0 or an integer ranging from 5 to 30. If the frame rate is not in this range, the frame rate is automatically changed to 0. If you configure a frame rate higher than the frame rate of your input file, the frame rate is automatically changed to the frame rate of the input file.
      videoBean.setFrameRate(0);
      
      /*
      * Set the video width (unit: pixel).
      * H.264: The value is 0 or a multiple of 2 from 32 to 4,096.
      * H.265: The value must be a multiple of 2 from 160 to 4,096.
      */
      videoBean.setWidth(1920);
      
      /*
       * Set the video height (unit: pixel).
       * H.264: The value is 0 or a multiple of 2 from 32 to 2,880.
       * H.265: The value is 0 or a multiple of 2 from 96 to 2,880.
       * If this parameter is set to 0, the video height is an adaptive value.
      */
      videoBean.setHeight(1080);
      
      /*
       * Whether to enable black bar removal
       * 0: Disable black bar removal.
       * 1: Enable black bar removal and low-complexity algorithms for long videos (>5 minutes).
       * 2: Enable black bar removal and high-complexity algorithms for short videos (≤5 minutes).
       */
      // Set whether to enable black bar removal. The default value is 0.
      videoBean.setBlackCut(0);
      
      // Set video parameters.
      transTemplate.setVideo(videoBean);
      
    2. Audio parameters
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      // Set audio parameters.
      TransTemplate.AudioBean audioBean=new TransTemplate.AudioBean();
      
      /*
       * Audio bitrate, in kbit/s.
       * The value is 0 or ranges from 8 to 1,000.
      */
      audioBean.setBitrate(128);
      
      /*
       * Number of audio channels
       * AUDIO_CHANNELS_1=1,
       * AUDIO_CHANNELS_2=2,
       */
      audioBean.setChannels(2);
      
      /*
       * Audio codec
       * AAC : 1 (default)
       * HEAAC1 : 2
       * HEAAC2 : 3
       * MP3 : 4
      */
      audioBean.setCodec(1);
      
      /*
       * Audio sampling rate
       * AUDIO_SAMPLE_AUTO=1 (default)
       * AUDIO_SAMPLE_22050=2,
       * AUDIO_SAMPLE_32000=3,
       * AUDIO_SAMPLE_44100=4,
       * AUDIO_SAMPLE_48000=5,
       * AUDIO_SAMPLE_96000=6,
      */
      audioBean.setSampleRate(4);
      
      // Set audio parameters.
      transTemplate.setAudio(audioBean);
      
    3. Common parameters
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      // Set common parameters.
      TransTemplate.CommonBean commonBean=new TransTemplate.CommonBean();
      
      /*
       * DASH interval, in seconds
       * The value ranges from 2 to 10. The default value is 5.
       */
      commonBean.setDashInterval(5);
      
      /*
       * HLS segment interval, in seconds
       * The value ranges from 2 to 10. The default value is 5.
       */
      commonBean.setHlsInterval(5);
      
      /*
       * Packaging type.
      * Available options:
       * 1: HLS
       * 2: DASH
       * 3: HLS+DASH
       * 4: MP4
       * 5: MP3
       * 6: ADTS
       * If pack_type is set to 5 or 6, do not set video parameters.
       */
      commonBean.setPackType(1);
      
      /*
      * Whether to enable low bitrate HD
       * false: disabled (default)
       * true: enabled
       */
      commonBean.setPvc(false);
      
      
      // Set common parameters.
      transTemplate.setCommon(commonBean);
      
  5. Send a request for modifying a transcoding template and return a message.
    1
    2
    3
    4
    // Send a request.
    BaseResponse baseResponse = mpcClient.modifyTemplate(transTemplate);
    // Return a message.
    System.out.println(new Gson().toJson(baseResponse));
    

Sample Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Service process:
* 1. Import the SDK package MPCSDK.jar.
* 2. Set configuration items, including the endpoint, AK, SK, and project ID, for accessing MPC and authorization.
* 3. Set request parameters for modifying a transcoding template, including the video and audio parameters.
* 4. Send a request.
* 5. Return the result.
*/
import com.google.gson.Gson;
import com.huawei.mpc.client.ClientConfig;
import com.huawei.mpc.client.MpcClient;
import com.huawei.mpc.client.MpcConfig;
import com.huawei.mpc.model.BaseResponse;
import com.huawei.mpc.model.template.TransTemplate;


// Set the method for constructing MPC configuration items.
MpcConfig mpcconfig = new MpcConfig();

// Mandatory. Set the endpoint of MPC. For details, see section "Obtaining Key Parameters."
mpcConfig.setEndPoint(endPoint);
// Mandatory. Set the user's project ID. For details, see section "Obtaining Key Parameters."
mpcConfig.setProjectId(projectId);

// Mandatory. Set the SK. For details, see section "Obtaining Key Parameters."
mpcConfig.setSk(sk);

// Mandatory. Set the AK. For details, see section "Obtaining Key Parameters."
mpcConfig.setAk(ak);
       
/*if you need proxy*/
// Optional. Set the proxy if necessary.
/*
ClientConfig clientConfig = new ClientConfig();
// Set the IP address of the proxy server.
clientConfig.setProxyHost(proxyHost);
// Set the port number of the proxy server.
clientConfig.setProxyPort(Integer.parseInt(proxyPort));
// Set the username for accessing the proxy server.
clientConfig.setProxyUserName(proxyUserName);
// Set the password for accessing the proxy server.
clientConfig.setProxyPassword(proxyPassword);
*/


// MPC construction method
MpcClient mpcClient = new MpcClient(mpcconfig);
// Set template parameters.
TransTemplate transTemplate=new TransTemplate();

/*
* Transcoding template name(Length <= 256)
*/
// Set the transcoding template name.
transTemplate.setTemplateName("MP4_12222ddd2122");

/*
* Transcoding template id(Integer type)
*/
// Set the transcoding template ID.
transTemplate.setTemplateId(551);

/*VideoBean*/
// Set video parameters.
TransTemplate.VideoBean videoBean=new TransTemplate.VideoBean();

/*
* Video encoding format (RMS range) [1,2] The default 1
* H264 = 1,H265 = 2
*/
// Set the video codec.
videoBean.setCodec(1);

/*
* Video Bit rate is within [40,30000]
*/
// Set the average output bitrate (unit: kbit/s).
videoBean.setBitrate(6000);

/*
* Coding grade (RMS range)
* H264_BASE = 1,
* H264_MAIN = 2,
* H264_HIGH = 3,
* H265_MAIN = 4,
*/
// Set the encoding profile.
videoBean.setProfile(3);

/*
* The encoding level (Valid Range) [1,15], default 15
*/
// Set the encoding level.
videoBean.setLevel(15);

/*
* Coding quality level (RMS range)
* HSPEED2 = 1,(only for h.265, h.265 default)
* HSPEED = 2,(only for h.265)
* NORMAL = 3,(h264 / h.265 available, h.264 default)
*/
// Set the encoding quality.
videoBean.setPreset(3);

/*
* Maximum reference frame number (unit: frame)
* H264: Range [1,8], default 4
* H265: fixed value 4
*/
// Set the maximum number of reference frames (unit: frame).
videoBean.setRefFramesCount(4);

/*
* I frame maximum interval (unit: second)
* Range [2,5], default: 5
*/
// Set the maximum I-frame interval (unit: second).
videoBean.setMaxIFramesInterval(5);

/*
* Maximum B frame interval (unit: frame)
* H264: Range [0,8], default 4
* H265: fixed value 7
*/
// Set the maximum B-frame interval (unit: frame).
videoBean.setbFramesCount(4);

/*
* Frame rate (unit: frame per second) (Valid range)
* FRAMERATE_AUTO = 1,
* FRAMERATE_10 = 2,
* FRAMERATE_15 = 3,
* FRAMERATE_2397 = 4, // 23.97 fps
* FRAMERATE_24 = 5,
* FRAMERATE_25 = 6,
* FRAMERATE_2997 = 7, // 29.97 fps
* FRAMERATE_30 = 8,
* FRAMERATE_50 = 9,
* FRAMERATE_60 = 10
*/
// Set the frame rate (unit: FPS).
videoBean.setFrameRate(1);

/*
* Video width (in pixels)
* H264: Range [32,4096], must be a multiple of 2
* H265: Range [160,4096], must be a multiple of 4
*/
// Set the video width (unit: pixel).
videoBean.setWidth(1920);

/*
* Video height (in pixels)
* H264: Range [32,2880], must be a multiple of 2
* H265: Range [96,2880], must be a multiple of 4
*/
// Set the video height (unit: pixel).
videoBean.setHeight(1080);

/*AudioBean*/
// Set audio parameters.
TransTemplate.AudioBean audioBean=new TransTemplate.AudioBean();

/*
* Audio Bit rate is within [64,320]
*/
// Set the audio bitrate (unit: kbit/s).
audioBean.setBitrate(128);

/*
* The number of channels (RMS range) is 1 or 2
*/
// Set the number of audio channels.
audioBean.setChannels(2);

/*
* Audio encoding format (RMS range) defaults to 1 Optional 2 or 3
*/
// Set the audio codec.
audioBean.setCodec(1);

/*
* Audio sampling rate (RMS range) Default is 1 Optional 2,3,4,5,6
*/
// Set the audio sampling rate.
audioBean.setSampleRate(4);

/*CommonBean*/
// Set common parameters.
TransTemplate.CommonBean commonBean=new TransTemplate.CommonBean();

/*
* DASH interval (in seconds)
* Range: [2,10], default 5
*/
// Set the DASH interval (unit: second).
commonBean.setDashInterval(5);

/*
* HLS shading interval (in seconds)
* Range: [2,10], default 5
*/
// Set the HLS segment interval (unit: second).
commonBean.setHlsInterval(5);

/*
* Package Type (DASH + MP4, HLS + TS, MP4)
* Bitmap to describe the package format, each bit represents a package format,currently supports two:
* 1 means DASH + MP4 format
* 2 indicates HLS + TS format
* 3 means HLS + TS and DASH + MP4 format
* 4 means MP4 format (note that here refers to MP4 is a video and audio Segment MP4 large files
*/
// Set the packaging type.
commonBean.setPackType(1);

/*
* PVC switch
* false: off (the current default off),true: open
*/
// Set whether to enable low bitrate HD.
commonBean.setPvc(false);

/*
* QDS switch
* false: off (the current default off),true: open
*/
// QDS switch
commonBean.setQds(false);

// Set video parameters.
transTemplate.setVideo(videoBean);
// Set audio parameters.
transTemplate.setAudio(audioBean);
// Set common parameters.
transTemplate.setCommon(commonBean);

// Send a request.
BaseResponse baseResponse = mpcClient.modifyTemplate(transTemplate);
// Return a message.
System.out.println(new Gson().toJson(baseResponse));