0

So, after i convert the mkv file to mp4, the audio plays at the correct time but the video ends early. That means the starting bit of the video has been cut off and so the video starts at a later frame and at the end, the video just stays on the same frame leading the audio/video not in sync problem... How can i solve this? Here's my command:

ffmpeg -i "input.mkv" -codec copy -ss 00:15:20 -t 00:00:17 "output.mp4"

And the output:

ffmpeg version N-88804-g3af2bf0af0 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  0.100 / 56.  0.100
  libavcodec     58.  2.100 / 58.  2.100
  libavformat    58.  2.100 / 58.  2.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  0.101 /  7.  0.101
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Input #0, matroska,webm, from 'input.mkv':
  Metadata:
    CREATION_TIME   : 2018-05-02T15:19:05Z
    ENCODER         : Lavf57.7.2
  Duration: 01:33:57.65, start: 0.000000, bitrate: 13042 kb/s
    Chapter #0:0: start 0.000000, end 58.458000
    Metadata:
      title           : Chapter 1
    Chapter #0:1: start 58.458000, end 245.312000
    Metadata:
      title           : Chapter 2
    Chapter #0:2: start 245.312000, end 527.160000
    Metadata:
      title           : Chapter 3
    Chapter #0:3: start 527.160000, end 819.786000
    Metadata:
      title           : Chapter 4
    Chapter #0:4: start 819.786000, end 1772.704000
    Metadata:
      title           : Chapter 5
    Chapter #0:5: start 1772.704000, end 2106.037000
    Metadata:
      title           : Chapter 6
    Chapter #0:6: start 2106.037000, end 2181.212000
    Metadata:
      title           : Chapter 7
    Chapter #0:7: start 2181.212000, end 2440.939000
    Metadata:
      title           : Chapter 8
    Chapter #0:8: start 2440.939000, end 2725.089000
    Metadata:
      title           : Chapter 9
    Chapter #0:9: start 2725.089000, end 3018.282000
    Metadata:
      title           : Chapter 10
    Chapter #0:10: start 3018.282000, end 3352.182000
    Metadata:
      title           : Chapter 11
    Chapter #0:11: start 3352.182000, end 3964.427000
    Metadata:
      title           : Chapter 12
    Chapter #0:12: start 3964.427000, end 4318.181000
    Metadata:
      title           : Chapter 13
    Chapter #0:13: start 4318.181000, end 4644.140000
    Metadata:
      title           : Chapter 14
    Chapter #0:14: start 4644.140000, end 5062.057000
    Metadata:
      title           : Chapter 15
    Chapter #0:15: start 5062.057000, end 5361.089000
    Metadata:
      title           : Chapter 16
    Chapter #0:16: start 5361.089000, end 5636.631000
    Metadata:
      title           : Chapter 17
    Stream #0:0: Video: h264 (Main), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 1k tbn, 180k tbc (default)
    Stream #0:1(jpn): Audio: aac (LC), 48000 Hz, stereo, fltp (default)
    Metadata:
      title           : Stereo
Output #0, mp4, to 'output.mp4':
  Metadata:
    encoder         : Lavf58.2.100
    Chapter #0:0: start 0.000000, end 17.000000
    Metadata:
      title           : Chapter 5
    Stream #0:0: Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 29.97 fps, 29.97 tbr, 16k tbn, 1k tbc (default)
    Stream #0:1(jpn): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp (default)
    Metadata:
      title           : Stereo
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=  395 fps= 24 q=-1.0 Lsize=   30997kB time=00:00:16.99 bitrate=14938.1kbits/s speed=1.02x
video:30645kB audio:333kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.061779%
2
  • Are audio and video actually out of sync, or do they just start and end at different times? What player are you using to show the video?
    – slhck
    Commented May 6, 2018 at 13:45
  • @slhck now that you mentioned it, i tried playing the video in VLC, the beginning of the video is just a black screen but after that it begins to play with the audio in sync... its like the beginning part is being cut off...
    – user902222
    Commented May 6, 2018 at 13:49

1 Answer 1

3

That's normal if you cut video at random points when copying the video bitstream (i.e. without re-encoding). The cut point may specify a frame that requires previous frames to be available to the decoder, and ffmpeg will include those frames with a negative timestamp, but not all players handle that properly.

If you use a player that does not support these negative timestamps, the video will only start playing from the first keyframe after the specified cut point.

You may add the option -avoid_negative_ts make_zero to the command line, but it may lead to the video showing before the cut point.

Or, leave out the -c copy part and re-encode the video using, e.g. -c:v libx264.

2
  • So that means I can't have a lossless conversion without the black screen between mkv and mp4, right?
    – user902222
    Commented May 6, 2018 at 14:12
  • It depends on the player; some will support it. Or you create the cut at known keyframe locations. (You can use a solution as shown here: superuser.com/questions/885452/…)
    – slhck
    Commented May 6, 2018 at 16:07

Not the answer you're looking for? Browse other questions tagged .