Mar*_*k L 345 mp4 ffmpeg h.264
我正在尝试使用ffmpeg连接两个mp4文件.我需要这是一个自动过程因此我选择ffmpeg.我正在将这两个文件转换为.ts文件,然后将它们连接起来,然后尝试对该连接的.ts文件进行编码.文件是h264和aac编码,我希望保持质量相同或尽可能接近原始.
ffmpeg -i part1.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy part1.ts
ffmpeg -i part2.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy part2.ts
cat part1.ts part2.ts > parts.ts
ffmpeg -y -i parts.ts -acodec copy -ar 44100 -ab 96k -coder ac -vbsf h264_mp4toannexb parts.mp4
Run Code Online (Sandbox Code Playgroud)
不幸的是我在编码过程中收到来自ffmpeg的以下错误消息:
[h264 @ 0x1012600]sps_id out of range
[h264 @ 0x1012600]non-existing SPS 0 referenced in buffering period
[h264 @ 0x1012600]sps_id out of range
[h264 @ 0x1012600]non-existing SPS 0 referenced in buffering period
[NULL @ 0x101d600]error, non monotone timestamps 13779431 >= 13779431kbits/s
av_interleaved_write_frame(): Error while opening file
Run Code Online (Sandbox Code Playgroud)
这种情况发生在编码的一半左右,这让我觉得你不能将两个.ts文件连在一起并让它工作.
rog*_*ack 641
FFmpeg有三种串联方法.
ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \
-filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" \
-map "[v]" -map "[a]" output.mkv
Run Code Online (Sandbox Code Playgroud)
请注意,此方法执行重新编码.
$ cat mylist.txt
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
$ ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
Run Code Online (Sandbox Code Playgroud)
对于Windows:
(echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt
ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4
Run Code Online (Sandbox Code Playgroud)
ffmpeg -i "concat:input1|input2" -codec copy output.mkv
Run Code Online (Sandbox Code Playgroud)
由于这些格式的性质以及通过此方法执行的简单连接,此方法不适用于许多格式,包括MP4.
concat过滤器:如果您的输入没有相同的参数(宽度,高度等),或者不是相同的格式/编解码器,或者您想要执行任何过滤,请使用此选项.(您可以仅重新编码不匹配的输入,以便它们共享相同的编解码器和其他参数,然后使用concat解复用器来避免重新编码其他输入).
concat demuxer:当您想要避免重新编码时使用,并且您的格式不支持文件级别连接(一般用户使用的大多数文件不支持文件级别连接).
concat协议:与支持文件级联接的格式(MPEG-1,MPEG-2 PS,DV)一起使用.不要与MP4一起使用.
如果有疑问,请尝试使用concat demuxer.
Ed9*_*999 58
对于MP4文件
对于.MP4文件(这是我从DailyMotion.com获得:50分钟的电视节目,可下载的仅在三个部分,三个.mp4格式的视频文件)以下是为有效的解决方案的Windows 7,和不NOT涉及重新编码文件.
我重命名了文件(如file1.mp4,file2.mp4,file3.mp4),以便部件按照正确的顺序查看完整的电视剧集.
然后我创建了一个简单的批处理文件(concat.bat),其中包含以下内容:
:: Create File List
echo file file1.mp4 > mylist.txt
echo file file2.mp4 >> mylist.txt
echo file file3.mp4 >> mylist.txt
:: Concatenate Files
ffmpeg -f concat -i mylist.txt -c copy output.mp4
Run Code Online (Sandbox Code Playgroud)
该批处理文件,并ffmpeg.exe,都必须放在同一文件夹中的.mp4文件被加入.然后运行批处理文件.运行通常不到十秒钟.
.
增编(2018/10/21) -
如果您正在寻找的是一种方法来指定当前文件夹中的所有mp4文件而无需重新输入,请在Windows 批处理文件中尝试此操作(必须包含选项-safe 0):
:: Create File List
for %%i in (*.mp4) do echo file '%%i'>> mylist.txt
:: Concatenate Files
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
Run Code Online (Sandbox Code Playgroud)
这适用于Windows 7中的批处理文件.不要尝试在命令行中使用它,因为它只能在批处理文件中使用!
cnd*_*cnd 54
这是一个快速(不到1分钟)和无损的方式来做到这一点,而不需要中间文件:
ls Movie_Part_1.mp4 Movie_Part_2.mp4 | perl -ne 'print "file $_"' | ffmpeg -f concat -i - -c copy Movie_Joined.mp4
Run Code Online (Sandbox Code Playgroud)
"ls"包含要加入的文件"perl"将动态连接文件创建到管道中"-i - "部分告诉ffmpeg从管道读取
(注意 - 我的文件中没有空格或奇怪的东西 - 如果你想用"硬"文件做这个想法,你需要适当的shell转义).
T.T*_*dua 37
如果它们不完全相同(100%相同的编解码器,相同的分辨率,相同的类型)MP4文件,那么您必须首先将它们转码为中间流:
ffmpeg -i myfile1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp1.ts
ffmpeg -i myfile2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp2.ts
// now join
ffmpeg -i "concat:temp1.ts|temp2.ts" -c copy -bsf:a aac_adtstoasc output.mp4
Run Code Online (Sandbox Code Playgroud)
注意!:输出将像第一个文件(而不是第二个文件)
小智 34
我试图将三个.mp3
音频文件连接成一个.m4a
文件,这个ffmpeg命令工作.
输入命令:
ffmpeg -i input1.mp3 -i input2.mp3 -i input3.mp3 -filter_complex concat=n=3:v=0:a=1 -f MOV -vn -y input.m4a
Run Code Online (Sandbox Code Playgroud)
含义:
"-filter_complex concat = n = 3:v = 0:a = 1":
concat表示使用媒体连接(连接)功能.
n表示确认输入文件的总数.
v意味着有视频吗?使用0 =没有视频,1 =包含视频.
一个手段有声音?使用0 =无音频,1 =包含音频.
-f表示强制设置文件格式(查看所有支持的格式,使用ffmpeg -formats
)
-vn表示禁用视频(-an
如果不需要也会禁用音频)
-y表示覆盖输出文件(如果输出文件已存在).
有关更多信息:使用ffmpeg -h full
print all选项(包括所有格式和编解码器特定选项,非常长)
Mar*_*k L 29
我最终使用mpg作为中间格式并且它有效(注意这是一个危险的例子,-qscale 0将重新编码视频......)
ffmpeg -i 1.mp4 -qscale 0 1.mpg
ffmpeg -i 2.mp4 -qscale 0 2.mpg
cat 1.mpg 2.mpg | ffmpeg -f mpeg -i - -qscale 0 -vcodec mpeg4 output.mp4
Run Code Online (Sandbox Code Playgroud)
Gab*_*les 21
主要答案对我不起作用,因为它给了我下面“故障排除”部分中描述的错误,所以这是我自己的答案。
如果您正在寻找如何将多个文件合并.mov
为.MOV
一个文件,请参阅我的其他答案:超级用户:合并 MOV 视频文件。我还有一个find
命令可以将所有感兴趣的文件快速收集到files.txt
或中inputs.txt
,而不必手动执行。
ffmpeg
创建一个inputs.txt
包含要组合的所有文件路径列表的文件(请参见下面的示例)。然后,将以上所有视频合并为一个,如下所示:
# Option 1 (preferred): into a single mp4 video with AAC audio encoding
# and original video encoding
time ffmpeg -f concat -safe 0 -i inputs.txt -c:v copy -c:a aac output.mp4
# Option 2: into a single .mkv video with original audio and video encoding
time ffmpeg -f concat -safe 0 -i inputs.txt -c copy output.mkv
Run Code Online (Sandbox Code Playgroud)
在 Ubuntu 20.04 上进行了测试,将我的一堆 Wyze 安全摄像头视频合并为一个。(我将相机的 micro SD 卡直接插入电脑)。
创建一个名为 的文件inputs.txt
,其中包含您要连接的所有文件的列表。前任:
file 'path/to/file1.mp4'
file 'path/to/file2.mp4'
file 'path/to/file3.mp4'
Run Code Online (Sandbox Code Playgroud)
就我而言,我合并了从 2022 年 11 月 13 日 18:31(下午 6:31)到 2022 年 11 月 13 日 19:39(下午 7:39)的 69 分钟视频。在 Wyze 安全摄像头路径中,这意味着从record/20221113/18/31.mp4'
到record/20221113/19/39.mp4
,包含在内。所以,这是我的完整inputs.txt
文件。请注意,您可以使用 Sublime Text 或 MS VSCode 的多光标编辑模式一次编辑多行和位置,快速轻松地编辑或创建此类文件。在这种情况下我使用了 Sublime Text。
file '/media/gabriel/3339-3730/record/20221113/18/31.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/32.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/33.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/34.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/35.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/36.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/37.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/38.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/39.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/40.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/41.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/42.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/43.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/44.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/45.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/46.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/47.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/48.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/49.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/50.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/51.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/52.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/53.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/54.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/55.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/56.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/57.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/58.mp4'
file '/media/gabriel/3339-3730/record/20221113/18/59.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/00.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/01.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/02.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/03.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/04.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/05.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/06.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/07.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/08.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/09.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/10.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/11.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/12.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/13.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/14.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/15.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/16.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/17.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/18.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/19.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/20.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/21.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/22.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/23.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/24.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/25.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/26.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/27.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/28.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/29.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/30.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/31.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/32.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/33.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/34.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/35.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/36.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/37.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/38.mp4'
file '/media/gabriel/3339-3730/record/20221113/19/39.mp4'
Run Code Online (Sandbox Code Playgroud)
将以上所有视频合并为一个:
# Option 1 (preferred): into a single mp4 video with AAC audio encoding
# and original video encoding
time ffmpeg -f concat -safe 0 -i inputs.txt -c:v copy -c:a aac output.mp4
# Option 2: into a single .mkv video with original audio and video encoding
time ffmpeg -f concat -safe 0 -i inputs.txt -c copy output.mkv
Run Code Online (Sandbox Code Playgroud)
就我而言,原始 69 个文件占用了411.1 MB。下面是上面两个命令的结果:
output.mp4
生成时间为11 秒,大小为380.3 MB 。AAC 编码的音频显然要小一些。output.mkv
生成时间为 8 秒,大小为410.7MB 。如果您运行ffmpeg -f concat -i inputs.txt -c copy output.mp4
并收到此错误:
[concat @ 0x563ca0173700] Unsafe file name '/media/gabriel/3339-3730/record/20221113/18/31.mp4'
inputs.txt: Operation not permitted
Run Code Online (Sandbox Code Playgroud)
...这是因为你忘记使用-safe 0
.
看:
如果您运行ffmpeg -f concat -safe 0 -i inputs.txt -c copy output.mp4
并收到此错误:
[mp4 @ 0x55ced96f2100] Could not find tag for codec pcm_alaw in stream #1, codec not currently supported in container
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Run Code Online (Sandbox Code Playgroud)
...这是因为“ ffmpeg
MP4 容器中不支持 PCM(pcm_alaw、pcm_s16le 等)”。请参阅此处:容器中当前不支持编解码器和此处。因此,请运行ffmpeg -f concat -safe 0 -i inputs.txt -c:v copy -c:a aac output.mp4
,将音频重新编码为 AAC 格式。或者,运行ffmpeg -f concat -safe 0 -i inputs.txt -c copy output.mkv
以写入 .mkv 容器而不是写入 .mp4 容器。
Flo*_*ues 18
ffmpeg
和不使用单线解决方案使用 ls
ls video1.mp4 video2.mp4 | while read line; do echo file \'$line\'; done | ffmpeg -protocol_whitelist file,pipe -f concat -i - -c copy output.mp4
Run Code Online (Sandbox Code Playgroud)
接受 0 或 2+ 个参数的函数
#######################################
# Merge mp4 files into one output mp4 file
# usage:
# mergemp4 #merges all mp4 in current directory
# mergemp4 video1.mp4 video2.mp4
# mergemp4 video1.mp4 video2.mp4 [ video3.mp4 ...] output.mp4
#######################################
function mergemp4() {
if [ $# = 1 ]; then return; fi
outputfile="output.mp4"
#if no arguments we take all mp4 in current directory as array
if [ $# = 0 ]; then inputfiles=($(ls -1v *.mp4)); fi
if [ $# = 2 ]; then inputfiles=($1 $2); fi
if [ $# -ge 3 ]; then
outputfile=${@: -1} # Get the last argument
inputfiles=(${@:1:$# - 1}) # Get all arguments besides last one as array
fi
# -y: automatically overwrite output file if exists
# -loglevel quiet: disable ffmpeg logs
ffmpeg -y \
-loglevel quiet \
-f concat \
-safe 0 \
-i <(for f in $inputfiles; do echo "file '$PWD/$f'"; done) \
-c copy $outputfile
if test -f "$outputfile"; then echo "$outputfile created"; fi
}
Run Code Online (Sandbox Code Playgroud)
注意:在这个线程中尝试了一些解决方案,但没有一个让我满意
SJ0*_*J00 11
可以在此处找到有关 ffmpeg中各种串联方式的详细文档.
您可以使用"Concat过滤器"进行快速连接.
它执行重新编码.当输入具有不同的视频/音频格式时,此选项最佳.
对于连接2个文件:
ffmpeg -i input1.mp4 -i input2.webm \
-filter_complex "[0:v:0] [0:a:0] [1:v:0] [1:a:0] concat=n=2:v=1:a=1 [v] [a]" \
-map "[v]" -map "[a]" output.mp4
Run Code Online (Sandbox Code Playgroud)
对于连接3个文件:
ffmpeg -i input1.mp4 -i input2.webm -i input3.mp4 \
-filter_complex "[0:v:0] [0:a:0] [1:v:0] [1:a:0] [2:v:0] [2:a:0] concat=n=3:v=1:a=1 [v] [a]" \
-map "[v]" -map "[a]" output.mp4
Run Code Online (Sandbox Code Playgroud)
这适用于同一个以及多个输入文件类型.
这对我有用(在Windows上)
ffmpeg -i "concat:input1|input2" -codec copy output
Run Code Online (Sandbox Code Playgroud)
一个例子...
ffmpeg -i "concat:01.mp4|02.mp4" -codec copy output.mp4
Run Code Online (Sandbox Code Playgroud)
使用一些python代码在一个文件夹中处理尽可能多的mp4(从python.org安装python,将其复制并粘贴并保存到名为mp4.py的文件中,然后使用python从文件夹中打开的cmd中运行mp4.py和文件夹中的所有mp4都将串联在一起)
ffmpeg -i "concat:input1|input2" -codec copy output
Run Code Online (Sandbox Code Playgroud)
从我博客上的帖子中摘录,这就是我在python中做的事情:
ffmpeg -i "concat:01.mp4|02.mp4" -codec copy output.mp4
Run Code Online (Sandbox Code Playgroud)
根据rogerdpack和Ed999的回复,我创建了.sh版本
#!/bin/bash
[ -e list.txt ] && rm list.txt
for f in *.mp4
do
echo "file $f" >> list.txt
done
ffmpeg -f concat -i list.txt -c copy joined-out.mp4 && rm list.txt
Run Code Online (Sandbox Code Playgroud)
它将*.mp4
当前文件夹中的所有文件加入joined-out.mp4
在Mac上测试。
生成的文件大小是我的60个测试文件的精确总和。不应有任何损失。我所需要的
对于.mp4
文件,我发现使用开源命令行工具:mp4box
. 然后你可以这样使用它:
mp4box.exe -add video1.mp4 -cat video2.mp4 destvideo.mp4
Run Code Online (Sandbox Code Playgroud)
对于大多数平台,请在此处下载:https : //gpac.wp.imt.fr/mp4box/
我发现使用选项3在公认的答案中在Mac上连接多个MP4时,管道运算符对我不起作用。
以下单线可在Mac(High Sierra)上连接mp4,而无需创建中间文件。
ffmpeg -f concat -safe 0 -i <(for f in ./*.mp4; do echo "file '$PWD/$f'"; done) -c copy output.mp4
迟到的答案,但这是唯一对我有用的选项:
(echo file '1.mp4' & echo file '2.mp4' & echo file '3.mp4' & echo file '4.mp4') | ffmpeg -protocol_whitelist file,pipe -f concat -safe 0 -i pipe: -vcodec copy -acodec copy "1234.mp4"
从这里的文档:https : //trac.ffmpeg.org/wiki/Concatenate
如果您有 MP4 文件,可以通过首先将它们转码为 MPEG-2 传输流来无损地连接这些文件。对于 H.264 视频和 AAC 音频,可以使用以下内容:
ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4
Run Code Online (Sandbox Code Playgroud)
这种方法适用于所有平台。
我需要能够将其封装在跨平台脚本中,因此我使用fluent-ffmpeg
并提出了以下解决方案:
ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4
Run Code Online (Sandbox Code Playgroud)
对于 Windows Powershell
创建文件列表
PS > ls file* | % { $n = $_.name; "file '\$n'" } | out-file mylist.txt
Run Code Online (Sandbox Code Playgroud)
检查创建的文件
PS > cat mylist.txt
file '/path/to/file1.mkv'
file '/path/to/file2.mkv'
file '/path/to/file3.mkv'
Run Code Online (Sandbox Code Playgroud)
运行 ffmpeg (不要忘记附加./
列表文件)
PS > ffmpeg -safe 0 -f concat -i ./mylist.txt -c copy file.mkv
ffmpeg version git-2020-05-15-b18fd2b Copyright (c) 2000-2020 the FFmpeg developers
...
Run Code Online (Sandbox Code Playgroud)
这是我用来将几个 GoPro mp4 连接成 720p mp4 的脚本。希望它有帮助。
#!/bin/sh
cmd="( "
for i; do
cmd="${cmd}ffmpeg -i $i -ab 256000 -vb 10000000 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 100 -f mpeg -; "
done
cmd="${cmd} ) | ffmpeg -i - -vb 10000000 -ab 256000 -s 1280x720 -y out-`date +%F-%H%M.%S`.mp4"
echo "${cmd}"
eval ${cmd}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
335134 次 |
最近记录: |