Browse Source

调整 ffmpeg 输出 HLS 的细节

Signed-off-by: allan716 <[email protected]>
allan716 3 years ago
parent
commit
eccbdb69ee
2 changed files with 16 additions and 21 deletions
  1. 15 20
      pkg/ffmpeg_helper/ffmpeg_helper.go
  2. 1 1
      pkg/ffmpeg_helper/ffmpeg_helper_test.go

+ 15 - 20
pkg/ffmpeg_helper/ffmpeg_helper.go

@@ -603,14 +603,8 @@ func (f *FFMPEGHelper) getVideoExportArgsByTimeRange(videoFullPath string, start
 	videoArgs = append(videoArgs, startTimeString)
 	videoArgs = append(videoArgs, "-t")
 	videoArgs = append(videoArgs, timeLeng)
-	//// 字符串转 int
-	//nowTimeLenStr := timeLeng
-	//timeLenInt, err := strconv.Atoi(timeLeng)
-	//if err == nil {
-	//	nowTimeLenStr = fmt.Sprintf("%d", timeLenInt+2)
-	//}
-	//videoArgs = append(videoArgs, nowTimeLenStr) // 多加 2 s
 
+	// 解决开头黑屏问题
 	videoArgs = append(videoArgs, "-accurate_seek")
 
 	videoArgs = append(videoArgs, "-i")
@@ -620,8 +614,7 @@ func (f *FFMPEGHelper) getVideoExportArgsByTimeRange(videoFullPath string, start
 	videoArgs = append(videoArgs, "copy")
 	videoArgs = append(videoArgs, "-c:a")
 	videoArgs = append(videoArgs, "copy")
-	//videoArgs = append(videoArgs, "-avoid_negative_ts")
-	//videoArgs = append(videoArgs, "1")
+
 	videoArgs = append(videoArgs, outVideiFullPath)
 
 	return videoArgs
@@ -640,18 +633,15 @@ func (f *FFMPEGHelper) getVideoHLSExportArgsByTimeRange(videoFullPath string, st
 	videoArgs = append(videoArgs, startTimeString)
 	videoArgs = append(videoArgs, "-t")
 	videoArgs = append(videoArgs, timeLeng)
-	//// 字符串转 int
-	//nowTimeLenStr := timeLeng
-	//timeLenInt, err := strconv.Atoi(timeLeng)
-	//if err == nil {
-	//	nowTimeLenStr = fmt.Sprintf("%d", timeLenInt+2)
-	//}
-	//videoArgs = append(videoArgs, nowTimeLenStr) // 多加 2 s
-
+	// 解决开头黑屏问题
 	videoArgs = append(videoArgs, "-accurate_seek")
+
 	videoArgs = append(videoArgs, "-i")
 	videoArgs = append(videoArgs, videoFullPath)
 
+	videoArgs = append(videoArgs, "-force_key_frames")
+	videoArgs = append(videoArgs, "\"expr:gte(t,n_forced*"+sgmentTime+")\"")
+
 	videoArgs = append(videoArgs, "-c:v")
 	videoArgs = append(videoArgs, "copy")
 	videoArgs = append(videoArgs, "-c:a")
@@ -678,10 +668,15 @@ func (f *FFMPEGHelper) getVideo2HLSArgs(videoFullPath, segmentTime, outVideoDirP
 	videoArgs := make([]string, 0)
 	videoArgs = append(videoArgs, "-i")
 	videoArgs = append(videoArgs, videoFullPath)
-	videoArgs = append(videoArgs, "-c")
+
+	videoArgs = append(videoArgs, "-force_key_frames")
+	videoArgs = append(videoArgs, "\"expr:gte(t,n_forced*"+segmentTime+")\"")
+
+	videoArgs = append(videoArgs, "-c:v")
 	videoArgs = append(videoArgs, "copy")
-	videoArgs = append(videoArgs, "-map")
-	videoArgs = append(videoArgs, "0")
+	videoArgs = append(videoArgs, "-c:a")
+	videoArgs = append(videoArgs, "copy")
+
 	videoArgs = append(videoArgs, "-f")
 	videoArgs = append(videoArgs, "segment")
 	videoArgs = append(videoArgs, "-segment_list")

+ 1 - 1
pkg/ffmpeg_helper/ffmpeg_helper_test.go

@@ -134,7 +134,7 @@ func TestExportVideoHLSAndSubByTimeRange(t *testing.T) {
 	videoFPath := "C:\\Tmp\\media\\test\\Chainsaw Man - S01E02 - ARRIVAL IN TOKYO HDTV-1080p.mp4"
 	subFPath := "C:\\Tmp\\media\\test\\Chainsaw Man - S01E02 - ARRIVAL IN TOKYO HDTV-1080p.chinese(简,csf).default.srt"
 	f := NewFFMPEGHelper(log_helper.GetLogger4Tester())
-	err := f.ExportVideoHLSAndSubByTimeRange(videoFPath, subFPath, "0:0:0", "300", "10", outDirPath)
+	err := f.ExportVideoHLSAndSubByTimeRange(videoFPath, subFPath, "0:0:0", "300", "5.000", outDirPath)
 	if err != nil {
 		t.Fatal(err)
 	}