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 关注
- 142 浏览
添加回答
举报