Ffmpeg video cutting error: out of sync audio

audioffmpegvideovideo conversion

I want to be able to upload a video to a server and cut it if I want to.

So, first I encode it:

ffmpeg -i input.mp4 -vcodec h264 -acodec aac -strict -2 -preset slow output.mp4

Then I try to cut it:

ffmpeg -i input.mp4 -ss 00:30:00 -c copy -t 00:30:00 output.mp4

AND the audio and video tracks are desynchronized, audio is about 10 seconds late…

So, is there a way to fix ffmpeg cutting or is there another way to cut video in linux?

UPDATE
here's the ffmpeg output when cutting

ffmpeg version 2.8 Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04)
  configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libdcadec --enable-libfreetype --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvo-aacenc --enable-libvidstab
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/mnt/storage/19b7c850b0c05c6f6fd26a7eda34841dddd55e33.mp4.prot':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.40.101
  Duration: 00:45:24.92, start: 0.042667, bitrate: 492 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 736x414 [SAR 1:1 DAR 16:9], 345 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 139 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
[mp4 @ 0x2aff400] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 @ 0x2aff400] Codec for stream 1 does not use global headers but container format requires global headers
Output #0, mp4, to '/home/videohosting/uploads/output7.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.40.101
    Stream #0:0(eng): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 736x414 [SAR 1:1 DAR 16:9], q=2-31, 345 kb/s, 25 fps, 25 tbr, 12800 tbn, 12800 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 139 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=22873 fps=22858 q=-1.0 Lsize=   53406kB time=00:15:24.87 bitrate= 473.0kbits/s
video:36732kB audio:15765kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.732541%

UPDATE 2

cutting and converting at same time just outputs empty video files

command

ffmpeg -ss 00:05:00 -i input.mp4 -t 00:10:00 -vcodec h264 -acodec aac -strict -2 -preset slow output.mp4

output

ffmpeg version 2.8 Copyright (c) 2000-2015 the FFmpeg developers
built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04)
configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libdcadec --enable-libfreetype --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvo-aacenc --enable-libvidstab
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, asf, from '/mnt/storage/tmp/e7ff19971eab45abf06d034444d94549ef3b5584.mp4':
Metadata:
SfOriginalFPS : 299700
WMFSDKVersion : 11.0.6001.7000
WMFSDKNeeded : 0.0.0.0000
comment : Footage: Small World Productions, Inc; Tourism New Zealand | Producer: Gary F. Spradling | Music: Steve Ball
title : Wildlife in HD
copyright : © 2008 Microsoft Corporation
IsVBR : 0
DeviceConformanceTemplate: AP@L3
Duration: 00:00:30.09, start: 0.000000, bitrate: 6977 kb/s
Stream #0:0(eng): Audio: wmav2 (a[1][0][0] / 0x0161), 44100 Hz, 2 channels, fltp, 192 kb/s
Stream #0:1(eng): Video: vc1 (Advanced) (WVC1 / 0x31435657), yuv420p, 1280x720, 5942 kb/s, 29.97 fps, 29.97 tbr, 1k tbn, 1k tbc
[libx264 @ 0x306fb80] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x306fb80] profile High, level 3.1
[libx264 @ 0x306fb80] 264 - core 142 r2491 24e4fed - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=5 deblock=1:0:0 analyse=0x3:0x113 me=umh subme=8 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=2 b_bias=0 direct=3 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=50 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '/home/videohosting/uploads/output6.mp4':
Metadata:
SfOriginalFPS : 299700
WMFSDKVersion : 11.0.6001.7000
WMFSDKNeeded : 0.0.0.0000
comment : Footage: Small World Productions, Inc; Tourism New Zealand | Producer: Gary F. Spradling | Music: Steve Ball
title : Wildlife in HD
copyright : © 2008 Microsoft Corporation
IsVBR : 0
DeviceConformanceTemplate: AP@L3
encoder : Lavf56.40.101
Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q=-1--1, 29.97 fps, 30k tbn, 29.97 tbc
Metadata:
encoder : Lavc56.60.100 libx264
Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc56.60.100 aac
Stream mapping:
Stream #0:1 -> #0:0 (vc1 (native) -> h264 (libx264))
Stream #0:0 -> #0:1 (wmav2 (native) -> aac (native))
Press [q] to stop, [?] for help
frame= 0 fps=0.0 q=0.0 Lsize= 0kB time=00:00:00.00 bitrate=N/A
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

Best Answer

Because of Keyframes, combining -ss and -t options with -c copy, results in unsynchronizing video and/or imprecise duration.

Better is to have one cycle of decode/encode (although it results some quality loss) to get better timing . Cutting and converting at the same time is a better option and don't have above mentioned cons.

Related Question