package com.video.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.video.cofig.AuthService;
import com.video.enums.VideoStatusEnum;
import com.video.pojo.Bgm;
import com.video.pojo.Comments;
import com.video.pojo.Videos;
import com.video.service.BgmService;
import com.video.service.VideoService;
import com.video.utils.*;
import io.swagger.annotations.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;
@RestController
@Api(value = "视频相关业务的接口", tags = {"视频相关业务的controller"})
@RequestMapping("/video")
public class VideoController extends BasicController {
@Autowired
private RestTemplate restTemplate;
@Resource
private BgmService bgmService;
@Resource
private VideoService videoService;
@ApiOperation(value = "上传视频", notes = "上传视频的接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户id", required = true,
dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "bgmId", value = "背景音乐id", required = false,
dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "videoSeconds", value = "背景音乐播放长度", required = true,
dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "videoWidth", value = "视频宽度", required = true,
dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "videoHeight", value = "视频高度", required = true,
dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "desc", value = "视频描述", required = false,
dataType = "String", paramType = "form")
})
@PostMapping(value = "/upload", headers = "content-type=multipart/form-data")
public KJSONResult upload(String userId,
String bgmId, double videoSeconds,
int videoWidth, int videoHeight,
String desc,
@ApiParam(value = "短视频", required = true)
MultipartFile file) throws Exception {
if (StringUtils.isBlank(userId)) {
return KJSONResult.errorMsg("用户id不能为空...");
}
// 文件保存的命名空间
// 保存到数据库中的相对路径
String uploadPathDB = "/" + userId + "/video";
String coverPathDB = "/" + userId + "/video";
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
// 文件上传的最终保存路径
String finalVideoPath = "";
try {
if (file != null) {
String fileName = file.getOriginalFilename();
// abc.mp4
String arrayFilenameItem[] = fileName.split("\\.");
String fileNamePrefix = "";
for (int i = 0; i < arrayFilenameItem.length - 1; i++) {
fileNamePrefix += arrayFilenameItem[i];
}
// fix bug: 解决小程序端OK,PC端不OK的bug,原因:PC端和小程序端对临时视频的命名不同
// String fileNamePrefix = fileName.split("\\.")[0];
if (StringUtils.isNotBlank(fileName)) {
finalVideoPath = FILE_SPACE + uploadPathDB + "/" + fileName;
// 设置数据库保存的路径
uploadPathDB += ("/" + fileName);
coverPathDB = coverPathDB + "/" + fileNamePrefix + ".jpg";
File outFile = new File(finalVideoPath);
if (outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
// 创建父文件夹
outFile.getParentFile().mkdirs();
}
fileOutputStream = new FileOutputStream(outFile);
inputStream = file.getInputStream();
IOUtils.copy(inputStream, fileOutputStream);
}
} else {
return KJSONResult.errorMsg("上传出错...");
}
} catch (Exception e) {
e.printStackTrace();
return KJSONResult.errorMsg("上传出错...");
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
// 判断bgmId是否为空,如果不为空,
// 那就查询bgm的信息,并且合并视频,生产新的视频
if (StringUtils.isNotBlank(bgmId)) {
Bgm bgm = bgmService.queryBgmById(bgmId);
String mp3InputPath = FILE_SPACE + bgm.getPath();
MergeVideoMp3 tool = new MergeVideoMp3(FFMPEG_EXE);
outMusic om = new outMusic(FFMPEG_EXE);
String videoInputPath = finalVideoPath;
String videoOutputName = UUID.randomUUID().toString() + ".mp4";
String videoMusicOutName = UUID.randomUUID().toString() + ".mp4";
uploadPathDB = "/" + userId + "/video" + "/" + videoOutputName;
String outMusicName = "/" + userId + "/video" + "/" + videoMusicOutName;
finalVideoPath = FILE_SPACE + uploadPathDB;
String outMusic1 = FILE_SPACE + outMusicName;
om.convertor(videoInputPath,outMusic1);
tool.convertor(outMusic1, mp3InputPath, videoSeconds, finalVideoPath);
}
System.out.println("uploadPathDB=" + uploadPathDB);
System.out.println("finalVideoPath=" + finalVideoPath);
// 对视频进行截图
FetchVideoCover videoInfo = new FetchVideoCover(FFMPEG_EXE);
videoInfo.getCover(finalVideoPath, FILE_SPACE + coverPathDB);
// 保存视频信息到数据库
Videos video = new Videos();
video.setAudioId(bgmId);
video.setUserId(userId);
video.setVideoSeconds((float) videoSeconds);
video.setVideoHeight(videoHeight);
video.setVideoWidth(videoWidth);
video.setVideoDesc(desc);
video.setVideoPath(uploadPathDB);
video.setCoverPath(coverPathDB);
video.setStatus(VideoStatusEnum.SUCCESS.value);
video.setCreateTime(new Date());
String videoId = videoService.saveVideo(video);
return KJSONResult.ok(videoId);
}
@ApiOperation(value = "上传封面", notes = "上传封面的接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户id", required = true,
dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "videoId", value = "视频主键id", required = true,
dataType = "String", paramType = "form")
})
@PostMapping(value = "/uploadCover", headers = "content-type=multipart/form-data")
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于微信小程序开发的web系统,适合做毕业设计,课程设计作业,java,springboot等技术,源码完整,配置后可直接运行。 基于微信小程序开发的web系统,适合做毕业设计,课程设计作业,java,springboot等技术,源码完整,配置后可直接运行。 基于微信小程序开发的web系统,适合做毕业设计,课程设计作业,java,springboot等技术,源码完整,配置后可直接运行。 基于微信小程序开发的web系统,适合做毕业设计,课程设计作业,java,springboot等技术,源码完整,配置后可直接运行。
资源推荐
资源详情
资源评论
收起资源包目录
微信小程序短视频社交系统后端.zip (407个子文件)
VideoController.class 13KB
VideoServiceImpl.class 7KB
UserCF.class 7KB
DefaultWorkerIdStrategy.class 7KB
UserController.class 6KB
DefaultRandomCodeStrategy.class 6KB
UserServiceImpl.class 5KB
HttpReq.class 5KB
Utils.class 5KB
RegistLoginController.class 5KB
RedisOperator.class 4KB
ZKCuratorClient$1.class 4KB
MiniInterceptor.class 4KB
FileLock.class 4KB
Serializes.class 4KB
Swagger2.class 4KB
MasterDataSourceConfiguration.class 3KB
Props.class 3KB
MergeVideoMp3.class 3KB
ZKCuratorClient.class 3KB
SpringRestTemplateController.class 3KB
AuthService.class 3KB
IdWorker.class 3KB
KJSONResult.class 3KB
WebMvcConfig.class 3KB
outMusic.class 3KB
FetchVideoCover.class 3KB
TimeAgoUtils.class 2KB
JsonUtils.class 2KB
FFMpegTest.class 2KB
UserCRUDServiceImpl.class 2KB
Sid.class 2KB
UserCRUDController.class 2KB
Ip.class 2KB
BGMOperatorTypeEnum.class 2KB
DayPrefixRandomCodeStrategy.class 2KB
IPv4Utils.class 2KB
ResourceConfig.class 1KB
PagedResult.class 1KB
RestTemplateConfig.class 1KB
BgmServiceImpl.class 1KB
Code.class 1KB
BgmController.class 1KB
DayCode.class 1KB
VideoStatusEnum.class 1KB
Props$Silent.class 1KB
Id.class 1004B
VideosCustomMapper.class 934B
Application.class 929B
BasicController.class 914B
MD5Utils.class 861B
VideoService.class 830B
DefaultWorkerIdStrategy$1.class 793B
DefaultRandomCodeStrategy$1.class 718B
DefaultWorkerIdStrategy$2.class 697B
UserService.class 684B
VideosApiApplicationTests.class 647B
HelloWorldController.class 636B
VideosServiceApplicationTests.class 635B
Sid$1.class 541B
MyMapper.class 518B
UsersMapper.class 515B
CommentsCustomMapper.class 487B
SearchRecordsMapper.class 440B
InvalidSystemClock.class 371B
UsersLikeVideosMapper.class 354B
UsersReportMapper.class 342B
UsersFansMapper.class 336B
CommentsMapper.class 333B
VideosMapper.class 327B
BgmMapper.class 318B
BgmService.class 292B
Test.class 255B
UserCRUDService.class 245B
RandomCodeStrategy.class 203B
WorkerIdStrategy.class 201B
mvnw.cmd 6KB
mvnw.cmd 6KB
.DS_Store 10KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.gitignore 303B
.gitignore 269B
videos-api.iml 15KB
videos-service.iml 13KB
videos-common.iml 13KB
WchatVideo-dev.iml 1KB
maven-wrapper.jar 47KB
maven-wrapper.jar 47KB
maven-wrapper.jar 47KB
maven-wrapper.jar 47KB
VideoController.java 14KB
UserCF.java 10KB
VideoServiceImpl.java 6KB
DefaultWorkerIdStrategy.java 6KB
Videos.java 6KB
UserController.java 5KB
VideosVO.java 5KB
DefaultRandomCodeStrategy.java 5KB
共 407 条
- 1
- 2
- 3
- 4
- 5
资源评论
白话机器学习
- 粉丝: 9109
- 资源: 7681
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功