2016-11-06 43 views
1

我使用YouTube API 3上傳視頻,然後通過請求根據自動字幕的字幕文件我用非連續計時以下文件YouTube自動生成的字幕文件有非連續時間


00:00:00,000 - > 00:00:06629

美好的週末呃怎麼是我的週末,我們

00:00:05549 - > 00:00:14,960

不這樣做,我們

00:00:06629 - > 00:00:14,960

沒錯這就是好耶羅馬很好,我得


樣品視頻:https://youtu.be/F2TVsMD_bDQ

那麼爲什麼每個字幕插槽的末尾不是下一個的第一個?

+0

您對此有什麼要求?您是否嘗試過在YouTube文檔中嘗試使用它? – KENdi

+0

@KENdi我只是按照文檔https://developers.google.com/youtube/v3/docs/videos/insert 要將視頻上傳到YouTube,然後使用以下網址請求標題:https://developers.google.com/youtube/v3/docs/captions/download – YouYou

+0

YouTube儀表板當我嘗試下載與下載文件相同的字幕問題時 – YouYou

回答

1

經過幾天的搜索和挖掘YouTube文檔後,我發現沒有什麼可以解決這個問題,所以我自己解決了這種情況我創建了使用正則表達式來修復字幕的代碼我已經測試了5個視頻和它的時間順序完美工作:

/** 
* 
* @author youans 
*/ 
public class SubtitleCorrector { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     try { 
      String fileContent = null; 
      File inFile = new File("/IN_DIRECTORY/Test Video Bad Format.srt"); 
      BufferedReader br = new BufferedReader(new FileReader(inFile)); 
      try { 
       StringBuilder sb = new StringBuilder(); 
       String line = br.readLine(); 

       while (line != null) { 
        sb.append(line); 
        sb.append("\n"); 
        line = br.readLine(); 
       } 
       fileContent = sb.toString(); 
      } finally { 
       br.close(); 
      } 
      String ragex = "\\d{2}:\\d{2}:\\d{2},\\d{3}"; 
      List<String> slotsTiming = new ArrayList(new TreeSet(getAllMatches(fileContent, ragex))); 

      System.out.println(slotsTiming.size()); 

      String timingRagex = "(((^1\n)|(\\n\\d+\n))(\\d{2}:\\d{2}:\\d{2},\\d{3}.*\\d{2}:\\d{2}:\\d{2},\\d{3}))"; 
      ragex = timingRagex + "[A-Za-z-,;'\"\\s]+"; 

      List<String> subtitleSlots = getAllMatches(fileContent, ragex); 
      List<String> textOnlySlots = new ArrayList(); 

      for (String subtitleSlot : subtitleSlots) { 
       textOnlySlots.add(subtitleSlot.replaceAll(timingRagex + "|\n", "")); 
      } 
      StringBuilder sb = new StringBuilder(""); 

      for (int i = 0; i < textOnlySlots.size(); i++) { 
       sb.append((i + 1)).append("\n").append(slotsTiming.get(i)).append(" --> ").append(slotsTiming.get(i + 1)).append("\n").append(textOnlySlots.get(i)).append("\n\n"); 
      } 

      File outFile = new File("/OUT_DIRECTOR/" + inFile.getName().replaceFirst("[.][^.]+$|bad format", "") + "_edited.SRT"); 
      PrintWriter pw = new PrintWriter(outFile); 

      pw.write(sb.toString()); 
      pw.flush(); 
      pw.close(); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 

    public static List<String> getAllMatches(String text, String regex) { 
     List matches = new ArrayList<>(); 
     Matcher m = Pattern.compile("(?=(" + regex + "))").matcher(text); 
     while (m.find()) { 
      matches.add(m.group(1)); 
     } 
     return matches; 
    } 

}