ffmpeg 拼接mp4_通过 ffmpeg 无损剪切/拼接视频

剪切/拼接视频文件是一种常见需求。在线视频网站现在往往将一个视频文件分割成 n 段,以减少流量消耗。使用 DownloadHelper/DownThemAll 这类工具下载下来的往往就是分割后的文件。能实现剪切/拼接视频文件的工具多种多样,但往往都需要进行视频重编码(transcoding),这就不可避免的带来了视频质量上的损耗,更不用提那长的令人发指的转换时间了…

其实借助 ffmpeg 我们就可以在不进行视频重编码的情况下完成此类任务:

剪切:

ffmpeg -i input.mp4 -ss **START_TIME** -t **STOP_TIME** -acodec copy -vcodec copy output.mp4

其中 START_TIME/STOP_TIME 的格式可以写成两种格式:

以秒为单位计数: 80

时:分:秒: 00:01:20

拼接 :

拼接的情况稍微复杂些,我们需要将需要拼接的视频文件按以下格式保存在一个列表 list.txt 中:

file '/path/to/file1'

file '/path/to/file2'

file '/path/to/file3'

相应的命令为:

ffmpeg -f concat -i **list.txt** -c copy output.mp4

由于不需要重编码,这两条命令几乎是即时完成的。

#!/bin/bash

#cut/join videos using ffmpeg without quality loss

if [ -z $1 ] || [ -z $2 ]; then

echo "Usage:$0 c[ut] seconds "

echo " eg. $0 c 10 80 example.mp4"

echo " eg. $0 c 00:00:10 00:01:20 example.mp4"

echo "Usage:$0 j[oin] "

echo " eg. $0 j avi"

exit

fi

case "$1" in

c)

echo "cuttig video..."

fileName=$(echo $4 | cut -f 1 -d '.')

fileType=$(echo $4 | cut -f 2 -d '.')

ffmpeg -i $4 -ss $2 -t $3 -acodec copy -vcodec copy $fileName-$2-$3.$fileType

;;

j)

echo "joinning videos..."

rm temp_list.txt

for f in ./*.$2; do echo "file '$f'" >> temp_list.txt; done

printf "file '%s'\n" ./*.$2 > temp_list.txt

ffmpeg -f concat -i temp_list.txt -c copy output.$2

rm temp_list.txt

;;

*)

echo "wrong arguments"

;;

esac

exit

以上拼接操作生效的前提是,所有视频文件的格式编码相同,如果需要拼接不同格式的视频文件可以借助以下脚本:

https://gist.github.com/imcaspar/8778002

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值