App下載

在java項目中如何調(diào)整視頻大?。吭贘ava項目中調(diào)整視頻大小的方法分析!

偷得浮生 2021-09-17 16:24:52 瀏覽數(shù) (3605)
反饋

當(dāng)您的受眾分散在當(dāng)前和新興的在線媒體平臺上時,優(yōu)化您的品牌形象可能是一項艱巨的任務(wù)。實現(xiàn)目標(biāo)和建立品牌知名度的一種方法是共享視頻內(nèi)容。這可以成為分享視覺效果、交流信息和接觸不同受眾的一種引人入勝的方法。不巧的是,由于不同平臺的大小要求不同,管理視頻格式可能會變得有點困難。

如果需要調(diào)整視頻大小,那么確定您是希望保持當(dāng)前的縱橫比還是對其進行自定義以適應(yīng)新參數(shù)非常重要。澄清一下,縱橫比是圖像的寬度與高度的比率,它會影響調(diào)整大小的視頻與原始視覺相比的外觀。如果您想改變視頻大小,但又不想改變其觀看方式,則有必要保留縱橫比。另一方面,您可能愿意稍微調(diào)整尺寸以適應(yīng)新格式。

以下 API 將為您提供兩個用于在 Java 中調(diào)整視頻大小的自動選項;一個將保持您的原始縱橫比,另一個將允許您輸入新的可自定義尺寸。這兩個功能都支持多種輸入視頻格式,包括 AVI、FLV、MP4、MOV 等。它們每 10 MB 文件大小使用 1 個 API 調(diào)用,并且在 5 分鐘內(nèi)每增加一分鐘的處理時間使用 1 個 API 調(diào)用,總處理時間最多為 25 分鐘。最大輸出文件大小為 50GB。

首先,我們將通過在 pom.xml 中添加對 jitpack 存儲庫的引用來安裝 Maven SDK:

java:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

然后,我們將添加對依賴項的引用:

java:

<dependency>
    <groupId>com.github.Cloudmersive</groupId>
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
    <version>v3.90</version>
</dependency>
</dependencies>

安裝完成后,我們可以討論我們的第一個 API。下面的功能將在保持原始縱橫比和編碼的同時調(diào)整視頻大小,這將允許您保留原始視覺并以您選擇的風(fēng)格傳達內(nèi)容。要執(zhí)行操作,我們需要將導(dǎo)入添加到控制器的頂部并調(diào)用函數(shù):

java:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.VideoApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
VideoApi apiInstance = new VideoApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
String fileUrl = "fileUrl_example"; // String | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
Integer maxWidth = 56; // Integer | Optional; Maximum width of the output video, up to the original video width. Defaults to original video width.
Integer maxHeight = 56; // Integer | Optional; Maximum height of the output video, up to the original video width. Defaults to original video height.
Integer frameRate = 56; // Integer | Optional; Specify the frame rate of the output video. Defaults to original video frame rate.
Integer quality = 56; // Integer | Optional; Specify the quality of the output video, where 100 is lossless and 1 is the lowest possible quality with highest compression. Default is 50.
String extension = "extension_example"; // String | Optional; Specify the file extension of the input video. This is recommended when inputting a file directly, without a file name. If no file name is available and no extension is provided, the extension will be inferred from the file data, which may cause a different extension to be used in the output.
try {
    byte[] result = apiInstance.videoResizeVideo(inputFile, fileUrl, maxWidth, maxHeight, frameRate, quality, extension);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling VideoApi#videoResizeVideo");
    e.printStackTrace();
}

現(xiàn)在,如果您更喜歡可自定義的編輯體驗,可以使用以下功能調(diào)整視頻大小而不保留原始縱橫比;這將允許您通過稍作改動來適應(yīng)任何視頻格式。但是,在選擇這種方法時請記住,它確實會增加圖像傾斜的可能性。 

java:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.VideoApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
VideoApi apiInstance = new VideoApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
String fileUrl = "fileUrl_example"; // String | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
Integer maxWidth = 56; // Integer | Optional; Maximum width of the output video, up to the original video width. Defaults to original video width.
Integer maxHeight = 56; // Integer | Optional; Maximum height of the output video, up to the original video width. Defaults to original video height.
Integer frameRate = 56; // Integer | Optional; Specify the frame rate of the output video. Defaults to original video frame rate.
Integer quality = 56; // Integer | Optional; Specify the quality of the output video, where 100 is lossless and 1 is the lowest possible quality with highest compression. Default is 50.
String extension = "extension_example"; // String | Optional; Specify the file extension of the input video. This is recommended when inputting a file directly, without a file name. If no file name is available and no extension is provided, the extension will be inferred from the file data, which may cause a different extension to be used in the output.
try {
    byte[] result = apiInstance.videoResizeVideoSimple(inputFile, fileUrl, maxWidth, maxHeight, frameRate, quality, extension);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling VideoApi#videoResizeVideoSimple");
    e.printStackTrace();
}

為確保兩個調(diào)整大小功能順利運行,您需要滿足幾個必需的參數(shù),并可以選擇輸入其他一些參數(shù):

  • 輸入文件(必填):您希望對其執(zhí)行操作的特定視頻文件。
  • API Key(必填):您的個人API密鑰;如果您還沒有,可以通過在 Cloudmersive 網(wǎng)站上注冊一個免費帳戶來檢索。
  • 文件 URL(可選):您要對其執(zhí)行操作的文件的 URL;推薦用于超過 2GB 的任何文件。
  • Max Width(可選):輸出視頻的最大寬度,可達原始視頻寬度;默認(rèn)為原始視頻寬度。
  • Max Height(可選):輸出視頻的最大高度,可達原始視頻寬度;默認(rèn)為原始視頻高度。
  • Frame Rate(可選):輸出視頻的幀率;默認(rèn)為原始視頻幀率。
  • 質(zhì)量(可選):輸出視頻的質(zhì)量,其中 100 是無損的,1 是具有最高壓縮率的最低質(zhì)量;默認(rèn)值為 50。
  • 擴展名(可選):輸入視頻的文件擴展名;直接輸入文件時建議使用此選項,無需文件名。如果沒有可用的文件名且未提供擴展名,則將從文件數(shù)據(jù)中推斷出擴展名,這可能會導(dǎo)致在輸出中使用不同的擴展名。

無論您選擇保持還是調(diào)整縱橫比,我們都希望本教程適合您的視頻調(diào)整大小需求。



0 人點贊