Browse Source

增加获取视频时长

songshuai 4 years ago
parent
commit
98fb535c19
1 changed files with 23 additions and 14 deletions
  1. 23 14
      src/main/java/cn/efunbox/audio/utils/VideoUtil.java

+ 23 - 14
src/main/java/cn/efunbox/audio/utils/VideoUtil.java

@@ -2,14 +2,13 @@ package cn.efunbox.audio.utils;
 
 import it.sauronsoftware.jave.MultimediaInfo;
 import it.sauronsoftware.jave.Encoder;
+import org.apache.poi.hssf.usermodel.HSSFRichTextString;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.util.Objects;
 
 public class VideoUtil {
@@ -19,14 +18,14 @@ public class VideoUtil {
         try {
             File tempFile = null;
             InputStream inputStream = new FileInputStream(file);
-            Workbook wb = getWorkbook(inputStream,"百度教材接口数据0229.xlsx");
+            Workbook wb = getWorkbook(inputStream,"百度教材接口数据0229.xls");
             //解析
             Sheet sheet = wb.getSheetAt(0);
             int rows = sheet.getPhysicalNumberOfRows();
-            for (int i = 1; i < rows; i++) {
+            for (int i = 22; i < rows; i++) {
                 Row row = sheet.getRow(i);
-                if (!Objects.isNull(row)) {
-                    String title = getCell(row.getCell(8));
+                if (row != null) {
+                    String title = getCell(row.getCell(7));
                     String playUrl = getCell(row.getCell(11));
                     String trackLength = "";
                     tempFile = getFileByUrl(playUrl);
@@ -55,7 +54,9 @@ public class VideoUtil {
                         trackLength += s;
                     }
                     System.out.println(title+"================"+trackLength);
-                    row.getCell(12).setCellValue(trackLength);
+                    writeResultToFile(title.substring(title.length()-2)+"--------"+trackLength);
+                    //row.createCell((short)13).setCellValue(new HSSFRichTextString(trackLength));
+                    tempFile.delete();
                 }
 
             }
@@ -85,7 +86,7 @@ public class VideoUtil {
     }
 
     private static String getCell(Cell cell) {
-        if (Objects.nonNull(cell)) {
+        if (cell != null) {
             CellType cellType = cell.getCellTypeEnum();
             if (cellType == CellType.STRING) {//字符串类型
                 //System.out.println(cell.getStringCellValue());
@@ -108,12 +109,20 @@ public class VideoUtil {
      */
     public static File getFileByUrl(String url) throws IOException {
         File tmpFile = File.createTempFile("temp", ".tmp");//创建临时文件
-        Image2Binary.toBDFile(url, tmpFile.getCanonicalPath());
+        cn.efunbox.audio.utils.Image2Binary.toBDFile(url, tmpFile.getCanonicalPath());
         return tmpFile;
     }
 
+    private static void writeResultToFile(String content) throws Exception{
+        OutputStream os = new FileOutputStream("output.txt",true);
+        PrintWriter pw=new PrintWriter(os);
+        pw.println(content);//每输入一个数据,自动换行,便于我们每一行每一行地进行读取
+        pw.close();
+        os.close();
+
+    }
+
     public static void main(String[] args) {
-        File file = new File("C:\\Users\\Administrator\\Desktop\\百度教材接口数据0229.xlsx");
+        File file = new File("C:\\Users\\SongShuai\\Desktop\\百度教材接口数据0229.xls");
         videoTimeLength(file);
-    }
-}
+    }