3 回答

TA貢獻1831條經驗 獲得超4個贊
講優雅有些自以為是,但可以提出KISS原則。順便說一句,您可以對切片使用更簡單的方法,不需要您猜測輸出數組的大小:
func inject(haystack, pile []string, at int) []string {
? ? result := haystack[:at]
? ? result = append(result, pile...)
? ? result = append(result, haystack[at:]...)
? ? return result
}
并且,按如下方式重寫您的代碼:
ffmpegArguments := []string{
? ? "-y",
? ? "-i", "invideo",
? ? "-c:v", "copy",
? ? "-strict", "experimental",
? ? "outvideo",
}
outputArguments := inject(ffmpegArguments, []string{"-i", "inaudio", "-c:a", "aac"}, 3)
fmt.Printf("%#v\n", ffmpegArguments)
fmt.Printf("%#v\n", outputArguments)

TA貢獻1802條經驗 獲得超4個贊
由于您要附加到輸出,我建議這樣做(簡單且一致)并設置最大容量:
out := make([]string, 0, 12)
out = append(out, in[0:3]...)
out = append(out, []string{"-i", "inaudio", "-c:a", "aac"}...)
out = append(out, in[3:8]...)
看:
package main
import (
"fmt"
)
func main() {
in := []string{
"-y",
"-i", "invideo",
// ffmpegAudioArguments...,
"-c:v", "copy",
"-strict", "experimental",
"outvideo",
}
out := make([]string, 0, 12)
out = append(out, in[0:3]...)
out = append(out, []string{"-i", "inaudio", "-c:a", "aac"}...)
out = append(out, in[3:8]...)
fmt.Println(in)
fmt.Println(out)
}
結果:
[-y -i invideo -c:v copy -strict experimental outvideo]
[-y -i invideo -i inaudio -c:a aac -c:v copy -strict experimental outvideo]

TA貢獻1876條經驗 獲得超6個贊
看起來第一個賦值指向 haystack 數組,隨后的步驟修改了切片haystack:
// arrayInject is a helper function written by Alirus on StackOverflow in my
// inquiry to find a way to inject one array into another _elegantly_:
func arrayInject(haystack, pile []string, at int) (result []string) {
? ? result = make([]string, len(haystack[:at]))
? ? copy(result, haystack[:at])
? ? result = append(result, pile...)
? ? result = append(result, haystack[at:]...)
? ? return result
}
- 3 回答
- 0 關注
- 186 瀏覽
添加回答
舉報