最近做了个影视网站,其中需要进行上传视频文件,在本地运行还好,上传大点的视频文件以下就可以,也不会产生什么错误,可是部署到服务器上,如果服务器带宽较小那么就会出现请求头过大,报413,此时我们解决办法就是增大服务器带宽,或者是进行切片(本人是使用这种),上传后后端将分片进行组合,生成整合文件,此时我又遇到mp4大文件在客户端访问缓慢的问题,我就想在将合成的mp4文件进行转码切片,所以使用到了ffpemg,直接上代码:
//视频切片,若是有多人同时上传的情况,还需要在item添加文件唯一标识比如使用md5,这里我就不多赘述
sliceUp(file){
let size=file.size //获取文件大小
let count=Math.ceil(size/this.slice) //每份切片大小为1024b /1kb 1024*1024*1024 /1mb /100mb
for (let i = 0; i < count; i++) {
let item={
count:count,
current:i,
file:file.slice(i*this.slice,(i+1)*this.slice)
}
this.size=count
this.list.push(item)
}
}
//这是上传文件
async select(e){
this.sliceUp(e.target.files[0]) 上传之前先将文件进行切分
let i=0
let flag=true
while(flag){
let fromDate=new FormData()
fromDate.append("movie",this.list[i].file)
fromDate.append("count",this.list[i].count)
fromDate.append("current",this.list[i].current)
fromDate.append("username",this.username)
fromDate.append("filename",this.filename)
fromDate.append("moviename",this.moviename)
i++
if (i>=this.list.length) {
flag=false
}
await this.$axios({
headers:{
"Content-type":"multipart/form-data"
},
method:"post",
url:"/movie/uploadPath",
data:fromDate,
onUploadProgress:(progressEvent)=>{
basicpercent=progressEvent.loaded / progressEvent.total*100//实时获取上传进度
if (basicpercent==100) {
this.percent++
var temp=this.percent/this.size*100
this.allpercent=Math.floor(temp)
}
}
}).then((res)=>{
if (res.data==1) {
Message.success({
message:"上传成功!",
type:"success"
})
}
})
}
},
上传结果是这样的:
后端代码:
public class SpliceUpload {
/**接收上传的分片然后通过
* RandomAccessFile进行读取写入到服务其中
* filepath:切片存储位置
*/
public static Boolean Collector(MultipartFile file, int count, int current,String filepath,String Outpath) throws IOException {
InputStream inputStream = file.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte[] bytes = new byte[(int) file.getSize()];
bufferedInputStream.read(bytes);
bufferedInputStream.close();
//该类的详情各位自己去查找jdk文档
//该类的实例支持读取和写入随机访问文件。 随机访问文件的行为类似于存储在文件系统中的大量字节。
RandomAccessFile rw = new RandomAccessFile(filepath+current+".mp4", "rw");
rw.write(bytes);
rw.close();
if (count==current+1){
int merge = merge(count, filepath, Outpath);
if (merge==1){
return true;
}
}
return false;
}
/**
*该方法用于合并分片
* path:切片存储位置,outpath是合成后文件位置
*/
private static int merge(int coust,String path,String outpath) throws IOException {
ArrayList<FileInputStream> fileInputStreams = new ArrayList<>();
for (int i = 0; i < coust; i++) {
fileInputStreams.add(new FileInputStream(path+i+".mp4"));
}
Iterator<FileInputStream> iterator = fileInputStreams.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
public boolean hasMoreElements()
{
return iterator.hasNext();
}
public FileInputStream nextElement()
{
return iterator.next();
}
};
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream(outpath);
byte[] buf = new byte[1024];
int len = 0;
while((len = sis.read(buf)) != -1)
{
fos.write(buf, 0, len);
}
fos.close();
sis.close();
return 1;
}
}
合并后使用ffmpeg进行切片生成m3u8格式文件,其实javaweb是集成了ffmpeg的,但是使用java编程需要自己写出多线程,所以我就直接使用命令行来实现了
ffmpeg需要自己去官网上进行下载:梯子给你们 https://ffmpeg.org/download.html
下载完成后找到ffmpeg的bin目录将其目录添加到环境变量中的path中
public class FFmpegProcessor{
/**
*srouce:源文件地址
* m3u8Url: 切片后的地址
* */
public static void convertMediaToM3u8ByHttp(String srouce, String m3u8Url) throws IOException {
Runtime runtime = Runtime.getRuntime();
String commint="ffmpeg -i "+srouce+" -threads 2000 -codec: copy -start_number 0 -hls_time 10 -hls_list_size 0 -f hls "+m3u8Url;
runtime.exec(commint).isAlive();
}
}
参考的博客:
https://blog.csdn.net/Sunshine_Moon/article/details/102524173
如有错误欢迎指出!