瀏覽代碼

修复,调整时间轴时候遇到时间输出字符串,小时,的问题

Signed-off-by: allan716 <[email protected]>
allan716 3 年之前
父節點
當前提交
cd89a00256

+ 10 - 0
.idea/go.imports.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="GoImports">
+    <option name="excludedPackages">
+      <array>
+        <option value="io" />
+      </array>
+    </option>
+  </component>
+</project>

+ 36 - 0
.idea/inspectionProfiles/Project_Default.xml

@@ -0,0 +1,36 @@
+<component name="InspectionProjectProfileManager">
+  <profile version="1.0">
+    <option name="myName" value="Project Default" />
+    <inspection_tool class="HttpUrlsUsage" enabled="true" level="WEAK WARNING" enabled_by_default="true">
+      <option name="ignoredUrls">
+        <list>
+          <option value="http://localhost" />
+          <option value="http://127.0.0.1" />
+          <option value="http://0.0.0.0" />
+          <option value="http://www.w3.org/" />
+          <option value="http://json-schema.org/draft" />
+          <option value="http://java.sun.com/" />
+          <option value="http://xmlns.jcp.org/" />
+          <option value="http://javafx.com/javafx/" />
+          <option value="http://javafx.com/fxml" />
+          <option value="http://maven.apache.org/xsd/" />
+          <option value="http://maven.apache.org/POM/" />
+          <option value="http://www.springframework.org/schema/" />
+          <option value="http://www.springframework.org/tags" />
+          <option value="http://www.springframework.org/security/tags" />
+          <option value="http://www.thymeleaf.org" />
+          <option value="http://www.jboss.org/j2ee/schema/" />
+          <option value="http://www.jboss.com/xml/ns/" />
+          <option value="http://www.ibm.com/webservices/xsd" />
+          <option value="http://activemq.apache.org/schema/" />
+          <option value="http://schema.cloudfoundry.org/spring/" />
+          <option value="http://schemas.xmlsoap.org/" />
+          <option value="http://cxf.apache.org/schemas/" />
+          <option value="http://primefaces.org/ui" />
+          <option value="http://tiles.apache.org/" />
+          <option value="http://google.com" />
+        </list>
+      </option>
+    </inspection_tool>
+  </profile>
+</component>

+ 25 - 0
internal/pkg/my_util/util.go

@@ -423,3 +423,28 @@ func MakeFloor10msMultipleFromTime(input time.Time) time.Time {
 	newTime := time.Time{}.Add(time.Duration(nowTime * math.Pow10(9)))
 	return newTime
 }
+
+// Time2SubTimeString 时间转字幕格式的时间字符串
+func Time2SubTimeString(inTime time.Time, timeFormat string) string {
+	/*
+		这里进行时间转字符串的时候有一点比较特殊
+		正常来说输出的格式是类似 15:04:05.00
+		那么有个问题,字幕的时间格式是 0:00:12.00, 小时,是个数,除非有跨度到 20 小时的视频,不然小时就应该是个数
+		这就需要一个额外的函数去处理这些情况
+	*/
+	outTimeString := inTime.Format(timeFormat)
+	if inTime.Hour() > 9 {
+		// 小时,两位数
+		return outTimeString
+	} else {
+		// 小时,一位数
+		items := strings.SplitN(outTimeString, ":", -1)
+		if len(items) == 3 {
+
+			outTimeString = strings.Replace(outTimeString, items[0], fmt.Sprintf("%d", inTime.Hour()), 1)
+			return outTimeString
+		}
+
+		return outTimeString
+	}
+}

+ 8 - 2
internal/pkg/sub_timeline_fixer/fixer.go

@@ -77,6 +77,12 @@ func (s *SubTimelineFixer) FixSubTimeline(infoSrc *subparser.FileInfo, inOffsetT
 	// 偏移时间
 	offsetTime := time.Duration(inOffsetTime*1000) * time.Millisecond
 	fixContent := infoSrc.Content
+	/*
+		这里进行时间转字符串的时候有一点比较特殊
+		正常来说输出的格式是类似 15:04:05.00
+		那么有个问题,字幕的时间格式是 0:00:12.00, 小时,是个数,除非有跨度到 20 小时的视频,不然小时就应该是个数
+		这就需要一个额外的函数去处理这些情况
+	*/
 	timeFormat := infoSrc.GetTimeFormat()
 	for _, srcOneDialogue := range infoSrc.Dialogues {
 
@@ -92,8 +98,8 @@ func (s *SubTimelineFixer) FixSubTimeline(infoSrc *subparser.FileInfo, inOffsetT
 		fixTimeStart := timeStart.Add(offsetTime)
 		fixTimeEnd := timeEnd.Add(offsetTime)
 
-		fixContent = strings.ReplaceAll(fixContent, srcOneDialogue.StartTime, fixTimeStart.Format(timeFormat))
-		fixContent = strings.ReplaceAll(fixContent, srcOneDialogue.EndTime, fixTimeEnd.Format(timeFormat))
+		fixContent = strings.ReplaceAll(fixContent, srcOneDialogue.StartTime, my_util.Time2SubTimeString(fixTimeStart, timeFormat))
+		fixContent = strings.ReplaceAll(fixContent, srcOneDialogue.EndTime, my_util.Time2SubTimeString(fixTimeEnd, timeFormat))
 	}
 
 	dstFile, err := os.Create(desSaveSubFileFullPath)

+ 14 - 0
internal/pkg/sub_timeline_fixer/fixer_test.go

@@ -743,6 +743,20 @@ func TestGetOffsetTimeV2_BaseAudio(t *testing.T) {
 			if bFind == false {
 				t.Fatal("sub not match")
 			}
+
+			_, err = timelineFixer.FixSubTimeline(infoSrc, 10, "c:\\tmp\\123.ass")
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			bFind, infoSrc, err = subParserHub.DetermineFileTypeFromFile("c:\\tmp\\123.ass")
+			if err != nil {
+				t.Fatal(err)
+			}
+			if bFind == false {
+				t.Fatal("sub not match")
+			}
+
 			/*
 				这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
 				internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现