diff --git a/ffmpeg.c b/ffmpeg.c
index b1052c0..509b945 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -544,6 +544,51 @@ static void update_benchmark(const char *fmt, ...)
     }
 }
 
+#define STAMP(pkt) ( ((pkt)->dts == AV_NOPTS_VALUE) ? (pkt)->pts : (pkt)->dts)
+
+static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost);
+static void queue_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
+{
+    PacketQueue *last = ost->queue, *new;
+    new = av_mallocz(sizeof(PacketQueue));
+    new->s   = s;
+    new->pkt = av_memdup(pkt, sizeof(AVPacket));
+    new->ost = ost;
+
+    if(STAMP(pkt) == AV_NOPTS_VALUE) {
+	write_frame(s, pkt, ost);
+	return;
+    }
+
+    if(!last) {
+	ost->queue = new;
+	return;
+    }
+
+    while(last && last->next && STAMP(last->next->pkt) < STAMP(pkt)) {
+	last = last->next;
+    }
+
+    new->next  = last->next;
+    last->next = new;
+}
+
+static void flush_queue(OutputStream *ost, int64_t ts)
+{
+    PacketQueue *last;
+    
+    if(ts == AV_NOPTS_VALUE)
+	return;
+    
+    while(ost->queue && STAMP(ost->queue->pkt) <= ts) {
+	last = ost->queue;
+	ost->queue = ost->queue->next;
+	write_frame(last->s, last->pkt, last->ost);
+	av_free(last->pkt);
+	av_free(last);
+    }
+}
+
 static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
 {
     AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
@@ -605,6 +650,8 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
         bsfc = bsfc->next;
     }
 
+    flush_queue(ost, STAMP(pkt));
+
     if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
         (avctx->codec_type == AVMEDIA_TYPE_AUDIO || avctx->codec_type == AVMEDIA_TYPE_VIDEO) &&
         pkt->dts != AV_NOPTS_VALUE &&
@@ -652,6 +699,8 @@ static void close_output_stream(OutputStream *ost)
 {
     OutputFile *of = output_files[ost->file_index];
 
+    flush_queue(ost, INT64_MAX);
+
     ost->finished = 1;
     if (of->shortest) {
         int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, AV_TIME_BASE_Q);
@@ -789,7 +838,10 @@ static void do_subtitle_out(AVFormatContext *s,
                 pkt.pts += 90 * sub->end_display_time;
         }
         subtitle_size += pkt.size;
-        write_frame(s, &pkt, ost);
+	if(i == 0)
+	    write_frame(s, &pkt, ost);
+	else
+	    queue_frame(s, &pkt, ost);
     }
 }
 
diff --git a/ffmpeg.h b/ffmpeg.h
index c001d2c..d614c79 100644
--- a/ffmpeg.h
+++ b/ffmpeg.h
@@ -312,6 +312,14 @@ enum forced_keyframes_const {
 
 extern const char *const forced_keyframes_const_names[];
 
+struct OutputStream;
+typedef struct PacketQueue {
+    AVFormatContext *s;
+    AVPacket *pkt;
+    struct OutputStream *ost;
+    struct PacketQueue *next;
+} PacketQueue;
+
 typedef struct OutputStream {
     int file_index;          /* file index */
     int index;               /* stream index in the output file */
@@ -373,6 +381,7 @@ typedef struct OutputStream {
     int keep_pix_fmt;
 
     AVCodecParserContext *parser;
+    PacketQueue *queue;
 } OutputStream;
 
 typedef struct OutputFile {
