Extract HEVC bitstream with ffmpeg

The raw bitstream of H.264/H.265 is typically called the Annex B format. You can create those files with the following FFmpeg commands:

H.265 to Annex B

ffmpeg -i in.mkv -c:v copy -bsf hevc_mp4toannexb out.h265

does the trick with ffmpeg and H.265/HEVC.

H.264 to Annex B

For H.264 it's slightly different:

ffmpeg -i in.mkv -c:v copy -bsf h264_mp4toannexb out.h264

I tried the ffmpeg solution from Sebastian, but it didn't work for me. Here is what I did to get just the H.265 bitstream. I did see that some people suggested setting the output file extension to .bin, but I like h264/h265 as it makes it more clear what kind of bitstream the file contains.

H.265 to Annex B

ffmpeg -i test.mkv -c:v copy -bsf hevc_mp4toannexb -f hevc test.h265

H.264 to Annex B

ffmpeg -i test.mkv -c:v copy -bsf h264_mp4toannexb -f h264 test.h264

NOTE: This also works for .mov files and probably for other formats as well.