有一段5,6秒的视频需要保存到oss服务器上,应该怎么做?

2019-01-02 20:22发布

直接用oss的文件流好像有点慢,大概要10多秒的时间,想要一种更快的方法
目前的想法是吧文件先压缩,再上传
尝试文件压缩,只能将9M的视频,压缩到8M
/**
* 压缩一个文件夹

@throws IOException
*/
public void zipDirectory(String path) throws IOException {
File file = new File(path);
String parent = file.getParent();
File zipFile = new File(parent, file.getName() + ".zip");
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
zip(zos, file, file.getName());
zos.flush();
zos.close();
}
尝试视频压缩,用都没有用起来,jar包工具我都下了,但然并卵
import java.io.File;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.VideoAttributes;
import it.sauronsoftware.jave.VideoSize;

public class FfmepgUtil {
public static void ffmepg(String sourceAddr, String targetAddr,
String codec, int bitRate, String format) {

File source = new File(sourceAddr);
File target = new File(targetAddr);
try {

AudioAttributes audio = new AudioAttributes();// 音频属性
audio.setCodec("libmp3lame");// libfaac PGM编码
// audio.setBitRate(new Integer(128000));// 音频比特率
// audio.setChannels(new Integer(2));// 声道
// audio.setSamplingRate(new Integer(44100));// 采样率
VideoAttributes video = new VideoAttributes();// 视频属性
video.setCodec(codec);// 视频编码
video.setBitRate(new Integer(bitRate));// 视频比特率
// video.setFrameRate(new Integer(30));// 帧率
// video.setSize(new VideoSize(1920,1080));// 视频宽高
EncodingAttributes attrs = new EncodingAttributes();// 转码属性
attrs.setFormat(format);// 视频格式
attrs.setAudioAttributes(audio);// 音频属性
attrs.setVideoAttributes(video);// 视频属性
Encoder encoder = new Encoder();// 创建解码器
encoder.encode(source, target, attrs);
System.out.println("上传成功!!!");
} catch (EncoderException e) {
e.printStackTrace();
System.out.println("文件格式不正确!");
}
}
}

1条回答
残风、尘缘若梦
2楼-- · 2019-01-02 20:44

你这压缩没有任何意义啊,并且我记得这个方法还不支持h264编码,压缩速度也很一般。
推荐一种方法:
通过OSS直传,你后台回调阿里上传结果,获得上传地址

查看更多
登录 后发表回答