在java项目中如何调整视频大小?在Java项目中调整视频大小的方法分析!

偷得浮生 2021-09-17 16:24:52 浏览数 (2628)
反馈

当您的受众分散在当前和新兴的在线媒体平台上时,优化您的品牌形象可能是一项艰巨的任务。实现目标和建立品牌知名度的一种方法是共享视频内容。这可以成为分享视觉效果、交流信息和接触不同受众的一种引人入胜的方法。不巧的是,由于不同平台的大小要求不同,管理视频格式可能会变得有点困难。

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

以下 API 将为您提供两个用于在 Java 中调整视频大小的自动选项;一个将保持您的原始纵横比,另一个将允许您输入新的可自定义尺寸。这两个功能都支持多种输入视频格式,包括 AVI、FLV、MP4、MOV 等。它们每 10 MB 文件大小使用 1 个 API 调用,并且在 5 分钟内每增加一分钟的处理时间使用 1 个 API 调用,总处理时间最多为 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。下面的功能将在保持原始纵横比和编码的同时调整视频大小,这将允许您保留原始视觉并以您选择的风格传达内容。要执行操作,我们需要将导入添加到控制器的顶部并调用函数:

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

现在,如果您更喜欢可自定义的编辑体验,可以使用以下功能调整视频大小而不保留原始纵横比;这将允许您通过稍作改动来适应任何视频格式。但是,在选择这种方法时请记住,它确实会增加图像倾斜的可能性。 

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

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

  • 输入文件(必填):您希望对其执行操作的特定视频文件。
  • API Key(必填):您的个人API密钥;如果您还没有,可以通过在 Cloudmersive 网站上注册一个免费帐户来检索。
  • 文件 URL(可选):您要对其执行操作的文件的 URL;推荐用于超过 2GB 的任何文件。
  • Max Width(可选):输出视频的最大宽度,可达原始视频宽度;默认为原始视频宽度。
  • Max Height(可选):输出视频的最大高度,可达原始视频宽度;默认为原始视频高度。
  • Frame Rate(可选):输出视频的帧率;默认为原始视频帧率。
  • 质量(可选):输出视频的质量,其中 100 是无损的,1 是具有最高压缩率的最低质量;默认值为 50。
  • 扩展名(可选):输入视频的文件扩展名;直接输入文件时建议使用此选项,无需文件名。如果没有可用的文件名且未提供扩展名,则将从文件数据中推断出扩展名,这可能会导致在输出中使用不同的扩展名。

无论您选择保持还是调整纵横比,我们都希望本教程适合您的视频调整大小需求。



0 人点赞