学成在线-第14天-讲义-媒资管理
1 视频处理 1.1需求分析
原始视频通常需要经过编码处理,生成m3u8和ts文件方可基于HLS协议播放视频。通常用户上传原始视频,系统 自动处理成标准格式,系统对用户上传的视频自动编码、转换,最终生成m3u8文件和ts文件,处理流程如下:
1、用户上传视频成功
2、系统对上传成功的视频自动开始编码处理
3、用户查看视频处理结果,没有处理成功的视频用户可在管理界面再次触发处理 4、视频处理完成将视频地址及处理结果保存到数据库
视频处理流程如下:
现在开始复制代码
使用Java调用第三方得命令
@Test
public void testProcessBuilder() throws IOException {
//创建processBuilder对象
ProcessBuilder processBuilder = new ProcessBuilder();
//设置第三方应用程序的命令
// processBuilder.command("ping","127.0.0.1");
processBuilder.command("ipconfig");
//将标准输入流和错误流合并
processBuilder.redirectErrorStream(true);
//启动一个进程
Process process = processBuilder.start();
//通过标准输入流来拿到正常和错误的信息
InputStream inputStream = process.getInputStream();
//转成字符流
InputStreamReader reader = new InputStreamReader(inputStream,"gbk");
//缓冲
char[] chars = new char[1024];
int len = -1;
while ((len = reader.read(chars))!=-1){
String string = new String(chars,0,len);
System.out.println(string);
}
inputStream.close();
reader.close();
}
Java程序调用第三方命令
public void testFFmpeg() throws IOException {
//创建processBuilder对象
ProcessBuilder processBuilder = new ProcessBuilder();
//设置第三方应用程序的命令
List<String> command = new ArrayList<>();
command.add("D:\\video\\ffmepg\\bin\\ffmpeg.exe");
command.add("-i");
command.add("D:\\video\\lucene.avi");
command.add("-y");//覆盖输出文件
command.add("-c:v");
command.add("libx264");
command.add("-s");
command.add("1280x720");
command.add("-pix_fmt");
command.add("yuv420p");
command.add("-b:a");
command.add("63k");
command.add("-b:v");
command.add("753k");
command.add("-r");
command.add("18");
command.add("D:\\video\\1.mp4");
processBuilder.command(command);
//将标准输入流和错误流合并
processBuilder.redirectErrorStream(true);
//启动一个进程
Process process = processBuilder.start();
//通过标准输入流来拿到正常和错误的信息
InputStream inputStream = process.getInputStream();
//转成字符流
InputStreamReader reader = new InputStreamReader(inputStream,"gbk");
//缓冲
char[] chars = new char[1024];
int len = -1;
while ((len = reader.read(chars))!=-1){
String string = new String(chars,0,len);
System.out.println(string);
}
inputStream.close();
reader.close();
}
开始转换
转换成功
工具类转换为MP4
@Test
public void testMp4VideoUtil(){
//String ffmpeg_path, String video_path, String mp4_name, String mp4folder_path
String ffmpeg_path = "D:\\video\\ffmepg\\bin\\ffmpeg.exe";
String video_path = "D:\\video\\1.avi";
String mp4_name = "11.mp4";
String mp4folder_path = "D:\\video\\";
Mp4VideoUtil mp4VideoUtil = new Mp4VideoUtil(ffmpeg_path,video_path,mp4_name,mp4folder_path);
//生成mp4
String result = mp4VideoUtil.generateMp4();
System.out.println(result);
}
消息队列得监听类
处理我得媒资管理系统
Controller
@Override
@GetMapping("/list/{page}/{size}")
public QueryResponseResult<MediaFile> findList(int page, int size, QueryMediaFileRequest queryMediaFileRequest) {
return mediaFileService.findList(page,size,queryMediaFileRequest);
}
Service
//查询我的媒资列表
public QueryResponseResult<MediaFile> findList(int page, int size, QueryMediaFileRequest queryMediaFileRequest) {
if(queryMediaFileRequest == null){
queryMediaFileRequest = new QueryMediaFileRequest();
}
//条件值对象
MediaFile mediaFile = new MediaFile();
if(StringUtils.isNotEmpty(queryMediaFileRequest.getTag())){
mediaFile.setTag(queryMediaFileRequest.getTag());
}
if(StringUtils.isNotEmpty(queryMediaFileRequest.getFileOriginalName())){
mediaFile.setFileOriginalName(queryMediaFileRequest.getFileOriginalName());
}
if(StringUtils.isNotEmpty(queryMediaFileRequest.getProcessStatus())){
mediaFile.setProcessStatus(queryMediaFileRequest.getProcessStatus());
}
//条件匹配器
ExampleMatcher exampleMatcher = ExampleMatcher.matching()
.withMatcher("tag",ExampleMatcher.GenericPropertyMatchers.contains())
.withMatcher("fileOriginalName",ExampleMatcher.GenericPropertyMatchers.contains());
// .withMatcher("processStatus",ExampleMatcher.GenericPropertyMatchers.exact());//如果不设置匹配器默认精确匹配
//定义example条件对象
Example<MediaFile> example = Example.of(mediaFile,exampleMatcher);
//分页查询对象
if(page<=0){
page = 1;
}
page = page-1;
if(size<=0){
size = 10;
}
Pageable pageable = new PageRequest(page,size);
//分页查询
Page<MediaFile> all = mediaFileRepository.findAll(example, pageable);
//总记录数
long total = all.getTotalElements();
//数据列表
List<MediaFile> content = all.getContent();
//返回的数据集
QueryResult<MediaFile> queryResult = new QueryResult<>();
queryResult.setList(content);
queryResult.setTotal(total);
//返回结果
QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);
return queryResponseResult;
}
数据保存成功