Q:

ffmpeg Invalid data found when processing input h264 to h265

I want to convert video files from h264 to h265. 
The command I use worked for many files so far, but now I get an error for some files:
 # ffmpeg -i rst.mkv -vcodec hevc -x265-params crf=28 -sn -acodec copy -map 0  out.mkv
ffmpeg version 2.8.6 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8.5 (Gentoo 4.8.5 p1.3, pie-0.6.2)
  configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --mandir=/usr/share/man --enable-shared --cc=x86_64-pc-linux-gnu-gcc --cxx=x86_64-pc-linux-gnu-g++ --ar=x86_64-pc-linux-gnu-ar --optflags='-O2 -pipe -march=core2' --disable-static --enable-avfilter --enable-avresample --disable-stripping --disable-indev=v4l2 --disable-outdev=v4l2 --disable-indev=alsa --disable-indev=oss --disable-indev=jack --disable-outdev=alsa --disable-outdev=oss --disable-outdev=sdl --enable-bzlib --disable-runtime-cpudetect --disable-debug --disable-doc --disable-gnutls --enable-gpl --enable-hardcoded-tables --enable-iconv --disable-lzma --enable-network --disable-openssl --enable-postproc --disable-libsmbclient --disable-ffplay --disable-sdl --disable-vaapi --disable-vdpau --disable-xlib --disable-libxcb --disable-libxcb-shm --disable-libxcb-xfixes --enable-zlib --disable-libcdio --disable-libiec61883 --disable-libdc1394 --disable-libcaca --disable-openal --disable-opengl --disable-libv4l2 --disable-libpulse --disable-libopencore-amrwb --disable-libopencore-amrnb --disable-libfdk-aac --disable-libopenjpeg --disable-libbluray --disable-libcelt --disable-libgme --disable-libgsm --disable-libmodplug --disable-libopus --disable-libquvi --disable-librtmp --disable-libssh --disable-libschroedinger --disable-libspeex --enable-libvorbis --enable-libvpx --disable-libzvbi --disable-libbs2b --disable-libflite --disable-frei0r --disable-libfribidi --disable-fontconfig --disable-ladspa --disable-libass --disable-libfreetype --disable-libsoxr --enable-pthreads --disable-libvo-aacenc --disable-libvo-amrwbenc --disable-libmp3lame --disable-libaacplus --disable-libfaac --disable-libsnappy --enable-libtheora --disable-libtwolame --disable-libwavpack --disable-libwebp --enable-libx264 --enable-libx265 --disable-libxvid --disable-x11grab --disable-amd3dnow --disable-amd3dnowext --disable-avx --disable-avx2 --disable-fma3 --disable-fma4 --disable-sse3 --disable-ssse3 --disable-sse4 --disable-sse42 --disable-xop --cpu=core2
  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, matroska,webm, from 'rst.mkv':
  Metadata:
    encoder         : libebml v1.0.0 + libmatroska v1.0
  Duration: 00:21:22.28, start: 0.000000, bitrate: 11533 kb/s
    Chapter #0:0: start 0.000000, end 159.784000
    Metadata:
      title           : 00:00:00.000
    Chapter #0:1: start 159.784000, end 642.266000
    Metadata:
      title           : 00:02:39.784
    Chapter #0:2: start 642.266000, end 1225.641000
    Metadata:
      title           : 00:10:42.266
    Chapter #0:3: start 1225.641000, end 1254.878000
    Metadata:
      title           : 00:20:25.641
    Chapter #0:4: start 1254.878000, end 1282.281000
    Metadata:
      title           : 00:20:54.878
    Chapter #0:5: start 1282.364000, end 1282.281000
    Metadata:
      title           : 00:21:22.364
    Stream #0:0(eng): Video: h264 (High), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Stream #0:1(ger): Audio: ac3, 48000 Hz, 5.1(side), fltp, 384 kb/s (default)
    Stream #0:2(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), fltp, 1536 kb/s
File 'tbbt-s07e02.mkv' already exists. Overwrite ? [y/N] y
x265 [info]: HEVC encoder version 1.9
x265 [info]: build info [Linux][GCC 4.8.5][64 bit] 8bit+10bit+12bit
x265 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.1 Cache64
x265 [info]: Main profile, Level-4 (Main tier)
x265 [info]: Thread pool created using 4 threads
x265 [info]: frame threads / pool features       : 2 / wpp(17 rows)
x265 [info]: Coding QT: max CU size, min CU size : 64 / 8
x265 [info]: Residual QT: max TU size, max depth : 32 / 1 inter / 1 intra
x265 [info]: ME / range / subpel / merge         : hex / 57 / 2 / 2
x265 [info]: Keyframe min / max / scenecut       : 23 / 250 / 40
x265 [info]: Lookahead / bframes / badapt        : 20 / 4 / 2
x265 [info]: b-pyramid / weightp / weightb       : 1 / 1 / 0
x265 [info]: References / ref-limit  cu / depth  : 3 / 1 / 1
x265 [info]: AQ: mode / str / qg-size / cu-tree  : 1 / 1.0 / 32 / 1
x265 [info]: Rate Control / qCompress            : CRF-28.0 / 0.60
x265 [info]: tools: rd=3 psy-rd=2.00 signhide tmvp strong-intra-smoothing
x265 [info]: tools: lslices=6 deblock sao
[matroska @ 0x1b09450] Codec for stream 1 does not use global headers but container format requires global headers
[matroska @ 0x1b09450] Codec for stream 2 does not use global headers but container format requires global headers
[matroska @ 0x1b09450] Invalid chapter start (1282364000000) or end (1282281000000).
Output #0, matroska, to 'out.mkv':
  Metadata:
    encoder         : Lavf56.40.101
    Chapter #0:0: start 0.000000, end 159.784000
    Metadata:
      title           : 00:00:00.000
    Chapter #0:1: start 159.784000, end 642.266000
    Metadata:
      title           : 00:02:39.784
    Chapter #0:2: start 642.266000, end 1225.641000
    Metadata:
      title           : 00:10:42.266
    Chapter #0:3: start 1225.641000, end 1254.878000
    Metadata:
      title           : 00:20:25.641
    Chapter #0:4: start 1254.878000, end 1282.281000
    Metadata:
      title           : 00:20:54.878
    Chapter #0:5: start 1282.364000, end 1282.281000
    Metadata:
      title           : 00:21:22.364
    Stream #0:0(eng): Video: hevc (libx265), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 23.98 fps, 1k tbn, 23.98 tbc (default)
    Metadata:
      encoder         : Lavc56.60.100 libx265
    Stream #0:1(ger): Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, 5.1(side), 384 kb/s (default)
    Stream #0:2(eng): Audio: dts ([1] [0][0] / 0x2001), 48000 Hz, 5.1(side), 1536 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> hevc (libx265))
  Stream #0:1 -> #0:1 (copy)
  Stream #0:2 -> #0:2 (copy)
Could not write header for output file #0 (incorrect codec parameters ?): Invalid data found when processing input

encoded 0 frames

The files in question are playable by for example mpv, so they are not corrupt.

A:

I would start with the first error.

[matroska @ 0x1b09450] Codec for stream 1 does not use global headers but container format requires global headers

Try including the following to correc that: "-flags +global_header".
And to convert an h264 to h265 you can use the following command with a standard FFmpeg install (assuming input.mp4 is a valid h264 file). Not sure if there are any unique flags required for an .mkv file but give that a try.
Command I use for .mp4 files:

ffmpeg -i /path/input.mp4 -c:v libx265 -c:a copy -flags +global_header /path/output.mp4

