|
|
@@ -1,10 +1,16 @@
|
|
|
package com.goafanti.admin.controller;
|
|
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.math.BigInteger;
|
|
|
+import java.nio.channels.FileChannel;
|
|
|
import java.security.MessageDigest;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.Base64;
|
|
|
@@ -159,7 +165,41 @@ public class AdminVideoApiController extends CertifyApiController {
|
|
|
|
|
|
// 压缩视频并将视频存储
|
|
|
|
|
|
- videoChange(filename, output, video.getUrl(), videoTemppath + "/convert_after" + filepath );
|
|
|
+ FileChannel fc= null;
|
|
|
+ try {
|
|
|
+ File f= new File(filename);
|
|
|
+ if (f.exists() && f.isFile()){
|
|
|
+ FileInputStream fis= new FileInputStream(f);
|
|
|
+ fc= fis.getChannel();
|
|
|
+ Long fsize = fc.size()/1024/1024;
|
|
|
+ System.out.println(fc.size());
|
|
|
+ if(fsize.longValue() > 50){
|
|
|
+ videoChange(filename, output, video.getUrl(), videoTemppath + "/convert_after" + filepath );
|
|
|
+ }else{
|
|
|
+ copy(filename, output, videoTemppath + "/convert_after" + filepath + "/" );
|
|
|
+ //修改数据库
|
|
|
+ updateSql(video.getUrl(), videoTemppath + "/convert_after" + filepath ,output);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }else{
|
|
|
+ System.out.println("file doesn't exist or is not a file");
|
|
|
+ }
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ System.out.println(e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println(e);
|
|
|
+ } finally {
|
|
|
+ if (null!=fc){
|
|
|
+ try{
|
|
|
+ fc.close();
|
|
|
+ }catch(IOException e){
|
|
|
+ System.out.println(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
@@ -202,24 +242,58 @@ public class AdminVideoApiController extends CertifyApiController {
|
|
|
deleteFtpVideo( videoTemppath + "/convert_after" + oldVideo.getUrl());
|
|
|
}
|
|
|
}
|
|
|
+ Boolean flag = false;
|
|
|
+ String filepath = "";
|
|
|
+ String output = "";
|
|
|
if (StringUtils.isNotBlank(video.getUrl()) && !video.getUrl().equals(oldVideo.getUrl())) { // 如果修改了视频
|
|
|
video.setTranscoding(1);
|
|
|
// 压缩视频并将视频
|
|
|
String filename = video.getUrl();
|
|
|
- String output = System.nanoTime() + ".mp4";
|
|
|
- String filepath = createFileName() ;
|
|
|
+ output = System.nanoTime() + ".mp4";
|
|
|
+ filepath = createFileName() ;
|
|
|
video.setUrl(filepath + "/" + output);
|
|
|
- videoChange(filename, output, video.getUrl(), videoTemppath + "/convert_after" + filepath);
|
|
|
+ FileChannel fc= null;
|
|
|
+ try {
|
|
|
+ File f= new File(filename);
|
|
|
+ if (f.exists() && f.isFile()){
|
|
|
+ FileInputStream fis= new FileInputStream(f);
|
|
|
+ fc= fis.getChannel();
|
|
|
+ Long fsize = fc.size()/1024/1024;
|
|
|
+ System.out.println(fc.size());
|
|
|
+ if(fsize.longValue() > 50){
|
|
|
+ videoChange(filename, output, video.getUrl(), videoTemppath + "/convert_after" + filepath );
|
|
|
+ }else{
|
|
|
+ copy(filename, output, videoTemppath + "/convert_after" + filepath + "/" );
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ System.out.println("file doesn't exist or is not a file");
|
|
|
+ }
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ System.out.println(e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println(e);
|
|
|
+ } finally {
|
|
|
+ if (null!=fc){
|
|
|
+ try{
|
|
|
+ fc.close();
|
|
|
+ }catch(IOException e){
|
|
|
+ System.out.println(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // videoChange(filename, output, video.getUrl(), videoTemppath + "/convert_after" + filepath);
|
|
|
|
|
|
//删除原来远程的视频
|
|
|
deleteFtpVideo(videoTemppath + "/convert_after" + oldVideo.getUrl());
|
|
|
-
|
|
|
-
|
|
|
}else{
|
|
|
video.setUrl(null);
|
|
|
}
|
|
|
|
|
|
result.setData(adminVideoService.updateVideo(video));
|
|
|
+ if(flag){
|
|
|
+ updateSql(video.getUrl(), videoTemppath + "/convert_after" + filepath ,output);
|
|
|
+ }
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@@ -395,5 +469,51 @@ public class AdminVideoApiController extends CertifyApiController {
|
|
|
return state;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static void copy(String file1, String file2, String dir) {
|
|
|
+
|
|
|
+ File file = new File(dir);
|
|
|
+ if(!file.isDirectory()){
|
|
|
+ file.mkdirs();//创建文件夹
|
|
|
+ }
|
|
|
+ System.out.println(file1);
|
|
|
+ System.out.println(file2);
|
|
|
+ File src = new File(file1);
|
|
|
+ InputStream in = null;
|
|
|
+ OutputStream out = null;
|
|
|
+ System.out.println(file1.substring(file1.lastIndexOf("/"),file1.length()));//获取单个文件的源文件的名称
|
|
|
+ try {
|
|
|
+ in = new BufferedInputStream(new FileInputStream(src), 16 * 1024);
|
|
|
+ FileOutputStream f = new FileOutputStream(dir+file2);// 一定要加上文件名称
|
|
|
+ out = new BufferedOutputStream(f, 16 * 1024);
|
|
|
+ byte[] buffer = new byte[16 * 1024];
|
|
|
+ int len = 0;
|
|
|
+ while ((len = in.read(buffer)) > 0) {
|
|
|
+ out.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (null != in) {
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (null != out) {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ copy("D:/Downloads/环保小视频.mp4","haha.mp4","F:/data/local/video/convert_after/2018/11/9/");
|
|
|
+ }
|
|
|
|
|
|
}
|