有的视频上传后只有声音 没有视频 如下图
endPoint 等信息从阿里云控制台获取然后保存到application.yml中
2.导入依赖 <dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>3.5.0version>
dependency>
3. 先上传一个视频
/**
* 视频转码 只能对已经上传到阿里云oss中的视频进行转码处理 故不考虑将视频保存到相对路径临时文件中
*/
@Test
void testTranFromH264Vide(){
//上传视频
File file = new File(videoUrl);
System.out.println(file.getName());
String uploadUrl = objectName + file.getName();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, uploadUrl, new ByteArrayInputStream(FileUtils.file2Byte(file)));
// 关闭OSSClient。
ossClient.shutdown();
//返回文件的路径
System.out.println("https://"+bucketName+"."+endPoint+"/"+uploadUrl);
}
上传成功
进入媒体管理控制台 (如果未开通服务需开通)
<dependency>
<groupId>com.aliyungroupId>
<artifactId>aliyun-java-sdk-mtsartifactId>
<version>2.5.2version>
dependency>
7. 上传视频及转码实现
先删除刚刚上传的视频 然后执行下面的代码
/**
* 视频转码 只能对已经上传到阿里云oss中的视频进行转码处理 故不考虑将视频保存到相对路径临时文件中
*/
@Test
void testTranFromH264Vide() {
//上传视频
File file = new File(videoUrl);
System.out.println(file.getName());
String uploadUrl = objectName + file.getName();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, uploadUrl, new ByteArrayInputStream(FileUtils.file2Byte(file)));
// 关闭OSSClient。
ossClient.shutdown();
//返回文件的路径
String saveOssUrl = "https://" + bucketName + "." + endPoint + "/" + uploadUrl;
System.out.println(saveOssUrl);
// ------------------------------------------------------------------------------------------------------------
//转码后的文件储存路径
String tranUploadVideoUrl = objectName+"(转码)"+file.getName();
// 创建DefaultAcsClient实例并初始化
DefaultProfile profile = DefaultProfile.getProfile(
endPoint.substring(4, endPoint.lastIndexOf(".aliyuncs")), //
accessKeyId, //
accessKeySecret); //
String ossLocation = endPoint.substring(0, endPoint.indexOf(".aliyuncs"));
IAcsClient client = new DefaultAcsClient(profile);
// 创建API请求并设置参数
SubmitJobsRequest request = new SubmitJobsRequest();
// Input
JSONObject input = new JSONObject();
input.put("Location", ossLocation);
input.put("Bucket", bucketName);
try {
input.put("Object", URLEncoder.encode(uploadUrl, "utf-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
request.setInput(input.toJSONString());
// Output
String outputOSSObject;
try {
outputOSSObject = URLEncoder.encode(tranUploadVideoUrl, "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("output URL encode failed");
}
JSONObject output = new JSONObject();
output.put("OutputObject", outputOSSObject);
// Ouput->Container
JSONObject container = new JSONObject();
container.put("Format", "mp4");
output.put("Container", container.toJSONString());
// Ouput->Video
JSONObject video = new JSONObject();
video.put("Codec", "H.264");
video.put("Bitrate", "1500");
video.put("Width", "1280");
video.put("Fps", "25");
output.put("Video", video.toJSONString());
// Ouput->Audio
JSONObject audio = new JSONObject();
audio.put("Codec", "AAC");
audio.put("Bitrate", "128");
audio.put("Channels", "2");
audio.put("Samplerate", "44100");
output.put("Audio", audio.toJSONString());
// Ouput->TemplateId
output.put("TemplateId", templateId);
JSONArray outputs = new JSONArray();
outputs.add(output);
request.setOutputs(outputs.toJSONString());
request.setOutputBucket(bucketName);
request.setOutputLocation(ossLocation);
// PipelineId
request.setPipelineId(pipelineId);
// 发起请求并处理应答或异常
SubmitJobsResponse response;
try {
response = client.getAcsResponse(request);
if (response.getJobResultList().get(0).getSuccess()) {
log.info("transform to H264 success");
} else {
log.error("transform to H264 fail");
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
//todo 删除原来的未转码文件 或手动删除
//返回转码成功后的url
System.out.println("转码后的url:"+"https://" + bucketName + "." + endPoint + "/"+tranUploadVideoUrl);
}
得到的返回的图片的url
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)