I have two .MTS
files taken in sequence and I want to trim them to the first five seconds. This command works for the first file:
ffmpeg -ss 0 -to 5 -i 00117.MTS -vcodec copy -acodec copy start_00117.MTS
The result is 5 seconds and 17 MB, as opposed to the 12 minutes and 2 GB from the original.
The same command on the second file fails:
ffmpeg -ss 0 -to 5 -i 00118.MTS -vcodec copy -acodec copy start_00118.MTS
The result is 12 minutes and 2 GB, like its original file.
What is the problem?
Full log :
$ rm start_00117.MTS; ffmpeg -ss 0 -to 5 -i 00117.MTS -vcodec copy -acodec copy start_00117.MTS
ffmpeg version 4.1.4 Copyright (c) 2000-2019 the FFmpeg developers
built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.4_1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
Input #0, mpegts, from '00117.MTS':
Duration: 00:12:36.02, start: 1.040000, bitrate: 22490 kb/s
Program 1
Stream #0:0[0x1011]: Video: h264 (High) (HDMV / 0x564D4448), yuv420p(top first), 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 50 tbr, 90k tbn, 50 tbc
Stream #0:1[0x1100]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, stereo, fltp, 256 kb/s
Stream #0:2[0x1200]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090), 1920x1080
Output #0, mpegts, to 'start_00117.MTS':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Video: h264 (High) (HDMV / 0x564D4448), yuv420p(top first), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 25 fps, 50 tbr, 90k tbn, 90k tbc
Stream #0:1: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, stereo, fltp, 256 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 304 fps=0.0 q=-1.0 Lsize= 16601kB time=00:00:06.02 bitrate=22590.4kbits/s speed=25.8x
video:15162kB audio:189kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 8.143849%
$ rm start_00118.MTS; ffmpeg -ss 0 -to 5 -i 00118.MTS -vcodec copy -acodec copy start_00118.MTS
rm: start_00118.MTS: No such file or directory
ffmpeg version 4.1.4 Copyright (c) 2000-2019 the FFmpeg developers
built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.4_1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
Input #0, mpegts, from '00118.MTS':
Duration: 00:12:36.59, start: 756.592000, bitrate: 22471 kb/s
Program 1
Stream #0:0[0x1011]: Video: h264 (High) (HDMV / 0x564D4448), yuv420p(top first), 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 50 tbr, 90k tbn, 50 tbc
Stream #0:1[0x1100]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, stereo, fltp, 256 kb/s
Stream #0:2[0x1200]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090), 1920x1080
Output #0, mpegts, to 'start_00118.MTS':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Video: h264 (High) (HDMV / 0x564D4448), yuv420p(top first), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 25 fps, 50 tbr, 90k tbn, 90k tbc
Stream #0:1: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, stereo, fltp, 256 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=37804 fps=923 q=-1.0 Lsize= 2130467kB time=00:12:36.54 bitrate=23069.0kbits/s speed=18.5x
video:1946718kB audio:23627kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 8.126621%
This looks to be a bug with input -to
. For now, use -ss
and -to
as output options.
ffmpeg -i 00118.MTS -ss 0 -to 5 -c copy start_00118.MTS
Using -c copy
may fail in several situations and this is one of them. From the documentation :
Stream copy is a mode selected by supplying the
copy
parameter to the-codec
option. It makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only demuxing and muxing. It is useful for changing the container format or modifying container-level metadata. The diagram above will, in this case, simplify to this:_______ ______________ ________ | | | | | | | input | demuxer | encoded data | muxer | output | | file | ---------> | packets | -------> | file | |_______| |______________| |________|
Since there is no decoding or encoding, it is very fast and there is no quality loss. However, it might not work in some cases because of many factors.
This is one such factor. See the closed bug report for a longer explanation and the coincidence of why the command works with the first section of a long record and not the second section.
Removing -c copy
(or -vcodec copy -acodec copy
) fixes the corner case, ie:
ffmpeg -ss 0 -to 5 -i 00122.MTS start_00122.MTS
See also Gyan's solution of using -ss
and -to
as output options, which also fixes the corner case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.