diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 88239c56555345b9443121cdfd8ce7042c2f1e7b..b79bbe83c634160667e2f7d78171d5e619bc1b60 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1217,7 +1217,7 @@ static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
     avio_wb32(pb, 0); /* reserved */
     avio_wb32(pb, 0); /* reserved */
     avio_wb16(pb, 0); /* layer */
-    avio_wb16(pb, st->codec->codec_type); /* alternate group) */
+    avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
     /* Volume, only for audio */
     if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
         avio_wb16(pb, 0x0100);