All Products
Search
Document Center

ApsaraVideo Media Processing:Template transkoding

Last Updated:Jun 19, 2026

Jika template preset di ApsaraVideo Media Processing tidak memenuhi kebutuhan Anda, Anda dapat membuat template transkoding kustom dengan menentukan parameter seperti format encoding, laju frame, dan resolusi. Contoh berikut menggunakan Java SDK V2.0 untuk menunjukkan cara menambahkan, memperbarui, menghapus, dan mengkueri template transkoding.

Prasyarat

Inisialisasi client terlebih dahulu sebelum memulai. Untuk informasi selengkapnya, lihat Inisialisasi.

Tambahkan template

Panggil operasi AddTemplate untuk menambahkan template.

Catatan
  • Jika Anda menerima error The resource "Template" quota has been used up saat menambahkan template, kuota template transkoding Anda telah habis. Anda dapat submit a ticket untuk meminta peningkatan kuota.

  • Catat ID template transkoding yang dikembalikan setelah penambahan template. Anda juga dapat membuat dan mengelola template transkoding melalui Konsol. Untuk informasi selengkapnya, lihat Template transkoding.

package com.aliyun.sample;

import com.aliyun.tea.*;

public class Sample {

    /**
     * description :
     * <p>Gunakan pasangan AccessKey untuk menginisialisasi client.</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.mts20140618.Client createClient() throws Exception {

        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID telah disetel.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah disetel.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "mts.cn-qingdao.aliyuncs.com";
        return new com.aliyun.mts20140618.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.mts20140618.Client client = Sample.createClient();
        com.aliyun.mts20140618.models.AddTemplateRequest addTemplateRequest = new com.aliyun.mts20140618.models.AddTemplateRequest()
                // Nama template.
                .setName("mps-example")
                // Kontainer.
                .setContainer("{\"Format\":\"mp4\"}")
                // Konfigurasi aliran video.
                .setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10s\"}")
                // Konfigurasi aliran audio.
                .setAudio("{\"Codec\":\"H.264\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}")
                // Konfigurasi transkoding umum.
                .setTransConfig("{\"TransMode\":\"onepass\"}")
                // Bidang konfigurasi segmen.
                .setMuxConfig("{\"Segment\":{\"Duration\":\"10\"}}");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // Jika Anda menyalin kode ini untuk dijalankan, cetak nilai kembali API.
            client.addTemplateWithOptions(addTemplateRequest, runtime);
        } catch (TeaException error) {
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

Perbarui template

Panggil operasi UpdateTemplate untuk memperbarui template transkoding.

package com.aliyun.sample;

import com.aliyun.tea.*;

public class Sample {

    /**
     * description :
     * <p>Gunakan pasangan AccessKey untuk menginisialisasi client.</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.mts20140618.Client createClient() throws Exception {

        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID telah disetel.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah disetel.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "mts.cn-qingdao.aliyuncs.com";
        return new com.aliyun.mts20140618.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.mts20140618.Client client = Sample.createClient();
        com.aliyun.mts20140618.models.UpdateTemplateRequest updateTemplateRequest = new com.aliyun.mts20140618.models.UpdateTemplateRequest()
                // ID template.
                .setTemplateId("16f01ad6175e4230ac42bb5182cd****")
                // Nama template.
                .setName("MPS-example")
                // Kontainer.
                .setContainer("{\"Format\":\"mp4\"}")
                // Konfigurasi aliran video.
                .setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10\"}")
                // Konfigurasi aliran audio.
                .setAudio("{\"Codec\":\"aac\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}")
                // Konfigurasi muxing.
                .setMuxConfig("{\"Segment\":{\"Duration\":\"10\"}}")
                // Konfigurasi transkoding umum.
                .setTransConfig("{\"TransMode\":\"onepass\"}");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // Jika Anda menyalin kode ini untuk dijalankan, cetak nilai kembali API.
            client.updateTemplateWithOptions(updateTemplateRequest, runtime);
        } catch (TeaException error) {
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

Hapus template

Panggil operasi DeleteTemplate untuk menghapus template transkoding.

package com.aliyun.sample;

import com.aliyun.tea.*;

public class Sample {

    /**
     * description :
     * <p>Gunakan pasangan AccessKey untuk menginisialisasi client.</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.mts20140618.Client createClient() throws Exception {

        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID telah disetel.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah disetel.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "mts.cn-qingdao.aliyuncs.com";
        return new com.aliyun.mts20140618.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.mts20140618.Client client = Sample.createClient();
        com.aliyun.mts20140618.models.DeleteTemplateRequest deleteTemplateRequest = new com.aliyun.mts20140618.models.DeleteTemplateRequest()
                // ID template transkoding kustom yang akan dihapus.
                .setTemplateId("16f01ad6175e4230ac42bb5182cd****");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // Jika Anda menyalin kode ini untuk dijalankan, cetak nilai kembali API.
            client.deleteTemplateWithOptions(deleteTemplateRequest, runtime);
        } catch (TeaException error) {
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

Kueri template berdasarkan ID

Panggil operasi QueryTemplateList untuk mengkueri template berdasarkan ID.

package com.aliyun.sample;

import com.aliyun.tea.*;

public class Sample {

    /**
     * description :
     * <p>Gunakan pasangan AccessKey untuk menginisialisasi client.</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.mts20140618.Client createClient() throws Exception {

        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID telah disetel.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah disetel.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "mts.cn-qingdao.aliyuncs.com";
        return new com.aliyun.mts20140618.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.mts20140618.Client client = Sample.createClient();
        com.aliyun.mts20140618.models.QueryTemplateListRequest queryTemplateListRequest = new com.aliyun.mts20140618.models.QueryTemplateListRequest()
                // Daftar ID template yang akan dikueri.
                .setTemplateIds("16f01ad6175e4230ac42bb5182cd****,88c6ca184c0e424d5w5b665e2a12****");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // Jika Anda menyalin kode ini untuk dijalankan, cetak nilai kembali API.
            client.queryTemplateListWithOptions(queryTemplateListRequest, runtime);
        } catch (TeaException error) {
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

Kueri template berdasarkan status

Panggil operasi SearchTemplate untuk mengkueri template berdasarkan status.

// File ini dibuat secara otomatis, jangan edit. Terima kasih.
package com.aliyun.sample;

import com.aliyun.tea.*;

public class Sample {

    /**
     * description :
     * <p>Gunakan pasangan AccessKey untuk menginisialisasi client.</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.mts20140618.Client createClient() throws Exception {

        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID telah disetel.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah disetel.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "mts.cn-qingdao.aliyuncs.com";
        return new com.aliyun.mts20140618.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.mts20140618.Client client = Sample.createClient();
        com.aliyun.mts20140618.models.SearchTemplateRequest searchTemplateRequest = new com.aliyun.mts20140618.models.SearchTemplateRequest()
                // Status template transkoding yang akan dicari.
                .setState("Normal")
                // Awalan nama template yang akan dicari.
                .setNamePrefix("S00000001")
                // Jumlah entri per halaman untuk hasil kueri terpaginasi.
                .setPageSize(10L)
                // Nomor halaman saat ini.
                .setPageNumber(1L);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // Jika Anda menyalin kode ini untuk dijalankan, cetak nilai kembali API.
            client.searchTemplateWithOptions(searchTemplateRequest, runtime);
        } catch (TeaException error) {
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // Ini hanya untuk demonstrasi. Tangani exception dengan hati-hati. Jangan abaikan exception dalam proyek Anda.
            // Pesan error
            System.out.println(error.getMessage());
            // Alamat diagnostik
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

Referensi