Explorar o código

视频存放位置更改

limin %!s(int64=7) %!d(string=hai) anos
pai
achega
8f7755e5d0

+ 31 - 42
src/main/java/com/goafanti/admin/controller/AdminVideoApiController.java

@@ -44,7 +44,6 @@ import com.goafanti.common.controller.CertifyApiController;
 import com.goafanti.common.enums.AttachmentType;
 import com.goafanti.common.enums.VideoFields;
 import com.goafanti.common.model.JtVideo;
-import com.goafanti.common.utils.FtpUtils;
 import com.goafanti.common.utils.StringUtils;
 import com.goafanti.core.shiro.token.TokenManager;
 
@@ -59,8 +58,6 @@ public class AdminVideoApiController extends CertifyApiController {
 	private String ffmpegPath = null;
 	@Value(value = "${ffprobe.path}")
 	private String ffprobePath = null;
-	@Value(value = "${video.path}")
-	private String videoPath = null;
 	@Value(value = "${video.temppath}")
 	private String videoTemppath = null;
 	
@@ -93,13 +90,13 @@ public class AdminVideoApiController extends CertifyApiController {
 		return res;
 	}
 
-	private String createFileName(String local) {
+	private String createFileName() {
 		// 年月日
 		Calendar now = Calendar.getInstance();
 		int year = now.get(Calendar.YEAR);
 		int month = now.get(Calendar.MONTH) + 1;
 		int day = now.get(Calendar.DAY_OF_MONTH);
-		String dir = "/" + local + "/" + year + "/" + month + "/" + day;
+		String dir = "/" + year + "/" + month + "/" + day;
 		return dir;
 	}
 
@@ -149,16 +146,18 @@ public class AdminVideoApiController extends CertifyApiController {
 		
 		// 生成保存到数据库的url
 		String output = System.nanoTime() + ".mp4";
+		
+		String filepath = createFileName();
 
-		video.setUrl(createFileName(videoPath) + "/" + output);
+		video.setUrl(filepath + "/" + output);
 		
 		video.setTranscoding(1);
 		// 保存到数据库
 		result.setData(adminVideoService.insertVideo(video));
 
-		// 压缩视频并将视频
-		//output = videoTemppath + "/convert_after";
-		videoChange(filename, output, video.getUrl(), videoTemppath + "/convert_after");
+		// 压缩视频并将视频存储
+		
+		videoChange(filename, output, video.getUrl(), videoTemppath + "/convert_after" + filepath );
 		
 		return result;
 	}
@@ -196,7 +195,7 @@ public class AdminVideoApiController extends CertifyApiController {
 						buildError( "视频已经发布不能删除", "视频已经发布不能删除"));
 				return result;
 			}else{
-				deleteFtpVideo(oldVideo.getUrl());
+				deleteFtpVideo( videoTemppath + "/convert_after" + oldVideo.getUrl());
 			}
 		}
 		if (StringUtils.isNotBlank(video.getUrl()) && !video.getUrl().equals(oldVideo.getUrl())) { // 如果修改了视频
@@ -204,11 +203,12 @@ public class AdminVideoApiController extends CertifyApiController {
 			// 压缩视频并将视频
 			String filename = video.getUrl();
 			String output = System.nanoTime() + ".mp4";
-			video.setUrl(createFileName(videoPath) + "/" + output);
-			videoChange(filename, output, video.getUrl(), videoTemppath + "/convert_after");
+			String filepath = createFileName() ;
+			video.setUrl(filepath + "/" + output);
+			videoChange(filename, output, video.getUrl(), videoTemppath + "/convert_after" + filepath);
 			
 			//删除原来远程的视频
-			deleteFtpVideo(oldVideo.getUrl());
+			deleteFtpVideo(videoTemppath + "/convert_after" + oldVideo.getUrl());
 			
 			
 		}else{
@@ -220,14 +220,23 @@ public class AdminVideoApiController extends CertifyApiController {
 	}
 	
 	private Boolean deleteFtpVideo(String url){
-		FtpUtils ftp = new FtpUtils();
-		return ftp.deleteFile("",url);
+		boolean b = false;
+		//判断文件是否存在
+		File f= new File(url);
+		if(f.exists()){
+			System.out.println("+++++++++++++++++++++++++++++文件存在,删除视频"+url);
+			b = f.delete();
+		}
+		return b;
 	}
 
 	// 将文件转码
 	private String videoChange(String input, String output, String url,String outputdir) throws IOException,
 			InterruptedException, ExecutionException {
-
+		File file = new File(outputdir);
+		if(!file.isDirectory()){
+			file.mkdirs();//创建文件夹
+		}
 		FFmpeg ffmpeg = new FFmpeg(ffmpegPath);
 		FFprobe ffprobe = new FFprobe(ffprobePath);
 
@@ -236,7 +245,7 @@ public class AdminVideoApiController extends CertifyApiController {
 		FFmpegBuilder builder = new FFmpegBuilder();
 		builder.setInput(in) // 输入文件
 				.overrideOutputFiles(true) // 覆盖重复文件
-				.addOutput(outputdir+"/"+output) // 输出文件
+				.addOutput(outputdir + "/" + output) // 输出文件
 				.setFormat("mp4") // 设置格式
 				.setTargetSize(250_000) // 目标大小
 				.disableSubtitle() // 没有子标题
@@ -252,7 +261,7 @@ public class AdminVideoApiController extends CertifyApiController {
 		FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
 		ExecutorService service = Executors.newCachedThreadPool();
 		// 3.直接new一个FutureTask
-		SubTask subTask = new SubTask(executor, builder, in, url, input, output, outputdir+"/"+output);
+		SubTask subTask = new SubTask(executor, builder, in, url, input, output, outputdir);
 		FutureTask<Boolean> result = new FutureTask<Boolean>(subTask);
 		// 4.提交任务
 		service.submit(result);
@@ -266,36 +275,16 @@ public class AdminVideoApiController extends CertifyApiController {
 
 	private void updateSql(String url, String tempdir, String filename) {
 		// 修改数据
-		File file = new File(tempdir);
+		File file = new File(tempdir+"/"+filename);
 		JtVideo video = new JtVideo();
 		video.setMd5(FileMD5(file));
 		video.setTranscoding(2);
 		video.setUrl(url);
 		url = url.substring(0,url.lastIndexOf("/"));
-		if(videoChange(url, tempdir, filename)){
-			System.out.println("修改数据库000000000000000000000");
-			adminVideoService.updateByUrl(video);//修改状态
-		}// 上传到远程
+		System.out.println("修改数据库000000000000000000000"+url);
+		adminVideoService.updateByUrl(video);//修改状态
 	}
-
-	private boolean videoChange(String url, String tempdir, String filename) {
-		System.out.println("文件路径url:============"+url);
-		System.out.println("上传到ftpfilename:=============="+filename);
-		System.out.println("本地tempdir:=============="+tempdir);
-		boolean t =false;
-		File f2 = new File(tempdir);// 临时压缩后的文件
-		if (f2.exists()) {
-			// 传到远程服务器
-			FtpUtils ftp = new FtpUtils();
-			System.out.println("上传到远程服务器............................");
-			t = ftp.uploadFile(url, filename, tempdir);
-			System.out.println("上传远程服务器完成/////////////////////////");
-			// 删除视频
-			f2.delete();
-		}
-		return t;
-	}
-
+	
 	// 获得文件md5
 	private String FileMD5(File file) {
 		try {

+ 1 - 1
src/main/java/com/goafanti/common/controller/BaseApiController.java

@@ -264,7 +264,7 @@ public class BaseApiController extends BaseController {
 		if(!fd.exists()){
 			fd.mkdirs();
 		}
-		String fileName = local + "/" + mf.getName() + System.nanoTime() + ".mp4" ;
+		String fileName = local + "/" + System.nanoTime() + ".mp4" ;
 		if (!files.isEmpty()) {
 			try {
 				File f = new File(fileName);

+ 4 - 0
src/main/java/com/goafanti/common/controller/BaseController.java

@@ -53,6 +53,9 @@ public class BaseController {
 
 	@Value(value = "${avatar.upload.host}")
 	private String					avatarUploadHost	= null;
+	
+	@Value(value = "${video.server}")
+	private String					videoServer	= null;
 
 	protected Error buildError(String key) {
 		return buildError(key, null);
@@ -118,6 +121,7 @@ public class BaseController {
 		model.addAttribute("shiro", SecurityUtils.getSubject());
 		model.addAttribute("isLogin", TokenManager.isLogin());
 		model.addAttribute("csrf", TokenManager.getCSRF());
+		model.addAttribute("videoServer", videoServer);
 		model.addAttribute("isAdmin", false);
 		if (TokenManager.isLogin() && TokenManager.getToken() instanceof User) {
 			model.addAttribute("userData", TokenManager.getUserToken());

+ 1 - 1
src/main/resources/props/config_local.properties

@@ -85,7 +85,7 @@ jiguang.pushUrl=https://api.jpush.cn/v3/push
 ffmpeg.path=E\:\\mytools\\ffmpeg-20181028-bdfd2e3-win64-static\\bin\\ffmpeg.exe
 ffprobe.path=E\:\\mytools\\ffmpeg-20181028-bdfd2e3-win64-static\\bin\\ffprobe.exe
 
-video.path=local
+video.server=http://192.168.0.20
 video.temppath=F:/data/local/video
 
 collect_flag=true

+ 2 - 2
src/main/resources/props/config_test.properties

@@ -84,7 +84,7 @@ jiguang.pushUrl=https://api.jpush.cn/v3/push
 ffmpeg.path=/usr/bin/ffmpeg
 ffprobe.path=/usr/bin/ffprobe
 
-video.path=test
-video.temppath=/data/statics/video
+video.temppath=/data/video
+video.server=http://101.37.32.31
 
 collect_flag=true

+ 1 - 0
src/main/webapp/WEB-INF/views/common.html

@@ -565,6 +565,7 @@
 			globalConfig.avatarHost = /*[[${avatarHost}]]*/'';
 			globalConfig.avatarUploadHost = /*[[${avatarUploadHost}]]*/'';
 			globalConfig.csrfToken = /*[[${csrf}]]*/'';
+			globalConfig.videoServer = /*[[${videoServer}]]*/'';
 			globalConfig.kfqq = '2035994611';
 			window.userData = {};
 			window.adminData = {};

+ 1 - 1
src/main/webapp/WEB-INF/views/portal/index.html

@@ -147,7 +147,7 @@
 				<div class="video">
 					<video id="video1" controls="true" controlslist="nodownload">
 <!-- 						<source src="http://118.190.205.7:8000/Pear-Demo-Yosemite_National_Park.mp4 "  type="video/mp4"> -->
-						<source th:src="${vurl == null? 'http://118.190.205.7:8000/Pear-Demo-Yosemite_National_Park.mp4': 'http://118.190.205.7:8000'+vurl}"  type="video/mp4">
+						<source th:src="${vurl == null? 'http://118.190.205.7:8000/Pear-Demo-Yosemite_National_Park.mp4': videoServer+vurl}"  type="video/mp4">
 
 						您的浏览器不支持 HTML5 video 标签。
 					</video>

+ 3 - 3
src/main/webapp/WEB-INF/views/portal/video/videoDetail.html

@@ -27,7 +27,7 @@
 			<div class="videoCenter">
 			    <div class="videoPlay">
 					<video id="videoDe" controls="true" controlslist="nodownload">
-						<source th:src="${'http://118.190.205.7:8000' + videoPlay.url}"   type="video/mp4"/> 
+						<source th:src="${videoServer + videoPlay.url}"   type="video/mp4"/> 
 					</video>
 					<div class="share">
 						<div>
@@ -51,7 +51,7 @@
 									<h4 th:text="${videoPlay.name}">申请军工三证72问</h4>
 									<!-- 2排字 -->
 									<p th:text="${#strings.abbreviate(videoPlay.summary,14)}">注意此处限制2排字注意此处限制2排字注意此处限制2排字注意此处限制2排字注意此处限制2排字</p>
-									<input type="hidden" th:value="${'http://118.190.205.7:8000' + videoPlay.url}"/>
+									<input type="hidden" th:value="${videoServer + videoPlay.url}"/>
 								</div>
 							</a>
 						</li>
@@ -62,7 +62,7 @@
 								<h4 th:text="${list.name}">申请军工三证72问</h4>
 								<!-- 2排字 -->
 								<p th:text="${#strings.abbreviate(list.summary,18)}">注意此处限制2排字注意此处限制2排字注意此处限制2排字注意此处限制2排字注意此处限制2排字</p>
-								<input type="hidden" th:value="${'http://118.190.205.7:8000' + list.url}"/>
+								<input type="hidden" th:value="${videoServer + list.url}"/>
 							</div>
 							</a>
 						</li>