剪切/拼接视频文件是一种常见需求。在线视频网站现在往往将一个视频文件分割成 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 的格式可以写成两种格式:

  1. 以秒为单位计数: 80
  2. 时:分:秒: 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

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

方便起见,我写了一个脚本来简化操作。放在 github 上,请自取:
https://gist.github.com/imcaspar/8771268

#!/bin/bash
#cut/join videos using ffmpeg without quality lossif [ "$(uname)" == "Darwin" ]; thenif ! [ -x "$(command -v brew)" ]; thenecho 'homebrew is not installed.'/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"fiif ! [ -x "$(command -v ffmpeg)" ]; thenecho 'ffmpeg is not installed.'brew install ffmpegfielif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; thenif ![ -x "$(command -v ffmpeg)" ]; thenecho 'ffmpeg is not installed.'fi
fiif [ -z $1 ] || [ -z $2 ]; thenecho "Usage:$0 c[ut] seconds <File>"echo "   eg. $0 c 10 80 example.mp4"echo "   eg. $0 c 00:00:10.100 00:01:20.200 example.mp4"echo "Usage:$0 j[oin] <FileType>"echo "   eg. $0 j avi"exit
ficase "$1" inc)echo "cuttig video..."echo $4fileName=$(echo $4 | rev | cut -f 2- -d '.' | rev)fileType=$(echo $4 | rev | cut -f 1 -d '.' | rev)echo $fileNameecho $fileTypestartTime=$( echo "$2" | tr ':' '_')endTime=$( echo "$3" | tr ':' '_' )echo $escapedFileNameffmpeg -i "$4" -ss $2 -to $3 -acodec copy -vcodec copy "$fileName-$startTime-$endTime.mp4";;j)echo "joinning videos..."rm temp_list.txt      for f in `ls *.$2 | sort -k 1n -t '.'`; do echo "file '$f'" >> temp_list.txt; done#printf "file '%s'\n" ./*.$2 > temp_list.txtffmpeg -f concat -i temp_list.txt -c copy output.$2rm temp_list.txt;;*)echo "wrong arguments";;
esac
exit

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

#!/bin/bash################################################################################
#
# Script name: MultiMedia Concat Script (mmcat)
# Author: burek (burek021@gmail.com)
# License: GNU/GPL, see http://www.gnu.org/copyleft/gpl.html
# Date: 2012-07-14
#
# This script concatenates (joins, merges) several audio/video inputs into one
# final output (just like as if all the inputs were played in a playlist, one
# after another).
#
# All input files must have at least one audio and at least one video stream.
# If not, you can easily add audio silence, using FFmpeg. Just search the
# internet for "ffmpeg add silence".
#
# The script makes use of FFmpeg tool (www.ffmpeg.org) and is free for use under
# the GPL license. The inspiration for this script came from this FAQ item:
# http://ffmpeg.org/faq.html#How-can-I-join-video-files_003f
#
# If you find any bugs, please send me an e-mail so I can fix it.
#
################################################################################
#
# General syntax: mmcat <input1> <input2> <input3> ... <output>
#
# For example: mmcat file1.flv file2.flv output.flv
# would create "output.flv" out of "file1.flv" and "file2.flv".
#
################################################################################# change this to what you need !!!
EXTRA_OPTIONS='-vcodec libx264 -crf 23 -preset medium -acodec aac -strict experimental -ac 2 -ar 44100 -ab 128k'################################################################################
#
# NO NEED TO TOUCH ANYTHING AFTER THIS LINE!
#
################################################################################# the version of the script
VERSION=1.3# location of temp folder
TMP=/tmp################################################################################echo "MultiMedia Concat Script v$VERSION (mmcat) - A script to concatenate multiple multimedia files."
echo "Based on FFmpeg - www.ffmpeg.org"
echo "Don't forget to edit this script and change EXTRA_OPTIONS"
echo ""################################################################################
# syntax check (has to have at least 3 params: infile1, infile2, outfile
################################################################################
if [ -z $3 ]; thenecho "Syntax: $0 <input1> <input2> <input3> ... <output>"exit 1
fi################################################################################
# get all the command line parameters, except for the last one, which is output
################################################################################
# $first  - first parameter
# $last   - last parameter (output file)
# $inputs - all the inputs, except the first input, because 1st input is
#           handled separately
################################################################################
first=${@:1:1}
last=${@:$#:1}
len=$(($#-2))
inputs=${@:2:$len}# remove all previous tmp fifos (if exist)
rm -f $TMP/mcs_*################################################################################
# decode first input differently, because the video header does not have to be
# kept for each video input, only the header from the first video is needed
################################################################################
mkfifo $TMP/mcs_a1 $TMP/mcs_v1ffmpeg -y -i $first -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a1 2>/dev/null </dev/null &
ffmpeg -y -i $first -an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_v1 2>/dev/null </dev/null &# if you need to log the output of decoding processes (usually not necessary)
# then replace the "2>/dev/null" in 2 lines above with your log file names, like this:
#ffmpeg -y -i $first -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a1 2>$TMP/log.a.1 </dev/null &
#ffmpeg -y -i $first -an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_v1 2>$TMP/log.v.1 </dev/null &################################################################################
# decode all the other inputs, remove first line of video (header) with tail
# $all_a and $all_v are lists of all a/v fifos, to be used by "cat" later on
################################################################################
all_a=$TMP/mcs_a1
all_v=$TMP/mcs_v1
i=2
for f in $inputs
domkfifo $TMP/mcs_a$i $TMP/mcs_v$iffmpeg -y -i $f -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a$i 2>/dev/null </dev/null &{ ffmpeg -y -i $f -an -f yuv4mpegpipe -vcodec rawvideo - 2>/dev/null </dev/null | tail -n +2 > $TMP/mcs_v$i ; } &# if you need to log the output of decoding processes (usually not necessary)# then replace the "2>/dev/null" in 2 lines above with your log file names, like this:#ffmpeg -y -i $f -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a$i 2>$TMP/log.a.$i </dev/null &#{ ffmpeg -y -i $f -an -f yuv4mpegpipe -vcodec rawvideo - 2>$TMP/log.v.$i </dev/null | tail -n +2 > $TMP/mcs_v$i ; } &all_a="$all_a $TMP/mcs_a$i"all_v="$all_v $TMP/mcs_v$i"let i++
done################################################################################
# concatenate all raw audio/video inputs into one audio/video
################################################################################
mkfifo $TMP/mcs_a_all
mkfifo $TMP/mcs_v_all
cat $all_a > $TMP/mcs_a_all &
cat $all_v > $TMP/mcs_v_all &################################################################################
# finally, encode the raw concatenated audio/video into something useful
################################################################################
ffmpeg -f u16le -acodec pcm_s16le -ac 2 -ar 44100 -i $TMP/mcs_a_all \-f yuv4mpegpipe -vcodec rawvideo -i $TMP/mcs_v_all \$EXTRA_OPTIONS \$last################################################################################
# remove all fifos
################################################################################
rm -f $TMP/mcs_*

通过 ffmpeg 无损剪切/拼接视频相关推荐

  1. linux视频拼接工具,linux中使用ffmpeg 无损剪切/拼接视频程序

    剪切/拼接视频文件是一种常见需求.在线视频网站现在往往将一个视频文件分割成 n 段,以减少流量消耗.使用 DownloadHelper/DownThemAll 这类工具下载下来的往往就是分割后的文件. ...

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

    剪切/拼接视频文件是一种常见需求.在线视频网站现在往往将一个视频文件分割成 n 段,以减少流量消耗.使用 DownloadHelper/DownThemAll 这类工具下载下来的往往就是分割后的文件. ...

  3. ffmpeg 无损 剪切 分割 视频

    ffmpeg -ss START -t DURATION -i INPUT -vcodec copy -acodec copy OUTPUT 例如: ffmpeg -ss 0:0:21 -t 0:0: ...

  4. 通过 ffmpeg 无损剪切与拼接视频方法

    比如说,你想把视频的从1:30秒开始,30秒的视频裁剪出来,保存成一个视频.这是这个文章要讨论的问题. 一 裁剪视频 ffmpeg提供简单的命令参数: ffmpeg -ss START -t DURA ...

  5. ffmpeg如何批量拼接视频(简单便捷)

    1. 把所有视频放在同一路径下,并建立一个videos_to_join.txt文件 2. 写入需要拼接的视频们 3. cmd窗口输入以下指令 说明: -codec copy 使用原视频们的编解码器 f ...

  6. Android实现视频剪切、视频拼接以及音视频合并

    因公司项目有需求,要实现视频剪切,视频拼接以及音视频合并的功能,自己通过在网上查找大量的资料终于把功能实现了,把实现的公共类提取出来,以便以后复习巩固. 使用map4parser作为视频处理包,and ...

  7. ffmpeg拼接视频

    目录 1.两段视频先后播放拼接 2.两段视频同时播放,左右或上下同时显示 1.两段视频先后播放拼接 我没有直接安装ffmpeg,因为好多软件里都已经自带了 想拼接视频的话,找两个视频文件,随便放在一个 ...

  8. ffmpeg 拼接视频

    ffmpeg拼接视频 ffmpeg -s 1920x960 -pix_fmt yuv420p -r 30 -i BQTerrace_1920x960_30.yuv -t 00:00:10 -s 192 ...

  9. ffmpeg 常用命令:视频拼接、裁剪、转图片

    一.视频拼接 1.准备需要进行拼接视频的filelist.txt文件,filelist.txt内容格式如下所示,排列顺序为拼接后的视频先后顺序. file 'video_01.avi' file 'v ...

最新文章

  1. 预、自训练之争:谷歌说预训练虽火,但在标注数据上自训练更有效
  2. 怎么用python运行代码_python怎么运行代码程序
  3. 将批注用于类型化 DataSet (摘自MSDN)
  4. SQL发HTML页脚怎么写,SQL Server中发送HTML格式邮件的方法
  5. 标题与文字的组合[摘]
  6. vsftp匿名访问目录_VSFTP本地用户目录跟匿名用户目录肿么修改?
  7. ubuntu kvm 部署安装   快照
  8. 计算机显卡是指什么时候,电脑哪个是显卡
  9. Linux系统:centos7下搭建Nginx和FastDFS文件管理中间件
  10. Matlab--m代码转C与C++代码)1(简单示例涉及到函数调用)
  11. World Wind Java开发之一(转)
  12. 蓝牙hci主要作用是什么_我的“我”是您的“您”:为什么为HCI精调Deixis很困难
  13. 【Unity】关于ScreenCapture.CaptureScreenshot截屏的尝试
  14. 实现计算机系统的资源共享,实现多操作系统计算机的资源共享.pdf
  15. C/C++ abs 函数 - C语言零基础入门教程
  16. 真正免费的国外PHP建站空间
  17. java随机答题器_Advanced Random Auto Clicker免费版下载-多合一随机自动答题器 v4.21 免费版 - 安下载...
  18. 人脸识别与Disentangled Representation
  19. 计算机word平均分怎么算,word怎么计算一列平均分
  20. OSSH免费版华为Portal

热门文章

  1. CAD建筑软件教程之倒斜角
  2. 反清华博士王垠(未来人类科学家在中国的毁灭)
  3. APS自动排产在五金行业的应用
  4. Windows 10怎么彻底关闭消息通知?
  5. c语言 swtich写简单菜单输出各种图形
  6. 压力变送器你知道多少?
  7. Google抓取网址软404,在测试实际版本的过程中,系统检测到该网址存在索引编制问题
  8. SpringSecurity自定义投票器
  9. 正厚知识 | 还记得时代的眼泪熊猫烧香吗?
  10. 电信增值彩信平台软件模块清单(sp专用)