简体   繁体   English

FFmpeg如何拼接视频和output相同的文件前缀

[英]How does FFmpeg concat videos and output same file prefix

I have a lot of videos cut into three parts, and I want to concat them.我有很多视频分为三个部分,我想将它们连接起来。 I now have the following bash to output videos in the folder to a file named mergedVideo.mp4.我现在将文件夹中的以下 bash 到 output 视频添加到名为 mergeVideo.mp4 的文件中。

for f in *.mp4 ; do echo file \'$f\' >> fileList.txt;done
ffmpeg -f concat -safe 0 -i fileList.txt -c copy mergedVideo.mp4

All my input files are like Something part1.mp4 , Something part2.mp4 , Something part3.mp4 What I want is to output a file named Something.mp4我所有的输入文件都像Something part1.mp4Something part2.mp4Something part3.mp4我想要的是output一个名为Something.mp4的文件

It is possible?有可能的? How can I modify my bash to achieve that~?我怎样才能修改我的 bash 来实现呢~?

Would you please try the following:请您尝试以下方法:

#!/bin/bash

pat="^([^[:space:]]+)[[:space:]]*part[[:digit:]]+\.mp4$"
for f in *.mp4; do
    echo file \'$f\' >> fileList.txt
    [[ $f =~ $pat ]] && mergedvideo="${BASH_REMATCH[1]}.mp4"
done
ffmpeg -f concat -safe 0 -i fileList.txt -c copy "$mergedvideo"

Explanation of the regex $pat :正则表达式$pat的解释:

  • ^([^[:space:]]+) matches a non-space substring from the beginning of the string $f and is assigned to the shell variable BASH_REMATCH[1] . ^([^[:space:]]+)匹配字符串$f开头的非空格 substring 并分配给 shell 变量BASH_REMATCH[1] As for the provided example filenames, BASH_REMATCH[1] will be assigned to Something .至于提供的示例文件名, BASH_REMATCH[1]将分配给Something
  • [[:space:]]* matches a zero or more whitespace(s). [[:space:]]*匹配零个或多个空格。
  • part[[:digit:]]+ matches a string "part" followed by digit(s). part[[:digit:]]+匹配字符串“part”后跟数字。
  • \.mp4 matches the suffix. \.mp4匹配后缀。

[Alternative] [选择]

If you are familiar with sed as well, the following may be more readable and maintainable:如果您也熟悉sed ,则以下内容可能更具可读性和可维护性:

mergedvideo=$(sed -E 's/[[:space:]]*part[[:digit:]]+//' <<< "$f")

I extend tshiono 's answer to be able to manipulate all the files in the same folders (just like Marinos An mentioned, the idea is to merge files with the same prefix)我扩展了 tshiono的答案,以便能够操作同一文件夹中的所有文件(就像提到的Marinos An一样,想法是合并具有相同前缀的文件)

Cases like a folder with the following files:案例如包含以下文件的文件夹:

  • Monday Blue part1.mp4星期一蓝色 part1.mp4
  • Monday Blue part2.mp4星期一蓝色 part2.mp4
  • Tuesday.mp4星期二.mp4
  • Wednesday part1.mp4星期三 part1.mp4
  • Wednesday part2.mp4星期三 part2.mp4
  • Wednesday part3.mp4星期三 part3.mp4

Expected Output:预期 Output:

  • Monday Blue.mp4蓝色星期一.mp4
  • Tuesday.mp4星期二.mp4
  • Wednesday.mp4星期三.mp4
#!/bin/bash

pat="^(.+)[[:space:]]+part[[:digit:]]+\.mp4$"
for f in *.mp4; do
    if [[ $f =~ $pat ]]; then
        echo file \'$f\' >> "${BASH_REMATCH[1]}.txt"
    fi
done

for f in *.txt; do
    ffmpeg -f concat -safe 0 -i "${f%%.*}.txt" -c copy "${f%%.*}.mp4"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM