用Java调整视频大小


当您的受众分散在当前和新兴的在线媒体平台上时,优化品牌形象可能是一项艰巨的任务。达成目标和树立品牌知名度的一种方法是共享视频内容。这可能是一种吸引人的方法,可以共享视觉效果,交流信息并吸引不同的受众。不幸的是,由于每个平台的尺寸要求各不相同,因此管理视频格式可能会变得有些挑战。

如果需要调整视频的大小,则重要的是确定是要保留当前的宽高比还是对其进行自定义以适应新的参数。要澄清的是,纵横比是图像的宽度与高度的比率,并且与原始视觉相比,它会影响调整大小后的视频的外观。如果要更改视频大小,但又不想更改其观看方式,则必须保留宽高比。另一方面,您可能愿意在尺寸上稍加调整以适应新格式。

以下API将为您提供两个自动选项,以用Java调整视频大小。一个将保持您的原始长宽比,另一个将允许您输入新的可自定义尺寸。两种功能均支持多种输入视频格式,包括AVI,FLV,MP4,MOV等。他们每10 MB的文件大小使用1个API调用,在5分钟内每增加一分钟的处理时间使用1个API调用,最多不超过25分钟的总处理时间。最大输出文件大小为50GB。

首先,我们将通过在pom.xml中添加对jitpack存储库的引用来安装Maven SDK:

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

然后,我们将添加对依赖项的引用:

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

安装完成后,我们可以讨论我们的第一个API。下面的功能将在保持原始宽高比和编码的同时调整视频大小,从而使您可以保留原始视觉并以您选择的样式传达内容。要执行该操作,我们需要将导入添加到控制器的顶部并调用该函数:

// 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();
}

现在,如果您希望获得更可定制的编辑体验,则可以使用以下功能来调整视频的大小,而无需保留原始的宽高比。通过稍作改动,您就可以适应任何视频格式。但是,选择此方法时请记住,它确实会增加图像歪斜的可能性。

// 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();
}

为确保两个调整大小功能都能顺利运行,您需要满足几个必需的参数,并可以选择输入其他一些参数:

输入文件(必填):您要对其执行操作的特定视频文件。 API密钥(必填):您的个人API密钥;如果您还没有,则可以通过在Cloudmersive网站上注册一个免费帐户来进行检索。 文件网址(可选):您要对其执行操作的文件的视频的网址;对于超过2GB的任何文件,建议使用此选项。 最大宽度(可选):输出视频的最大宽度,最大为原始视频宽度;默认为原始视频宽度。 最大高度(可选):输出视频的最大高度,不超过原始视频宽度;默认为原始视频高度。 帧速率(可选):输出视频的帧速率;默认为原始视频帧速率。 质量(可选):输出视频的质量,其中100是无损的,1是具有最高压缩率的最低质量;默认值为50。 扩展名(可选):输入视频的文件扩展名;建议在不带文件名的情况下直接输入文件时使用。如果没有可用的文件名并且没有提供扩展名,则将从文件数据中推断出该扩展名,这可能会导致在输出中使用其他扩展名。 无论您选择保持还是调整宽高比,我们都希望本教程能够满足您调整视频大小的需求。

如果您想查看更多视频API,可以在Cloudmersive网站上找到它们以及许多其他功能。或者,如果您有任何疑问,请随时与我们的团队联系;他们总是很乐意为您提供帮助。


原文链接:https://codingdict.com/