diff -ruN xvidcap-1.1.3.orig/src/xtoffmpeg.c xvidcap-1.1.3/src/xtoffmpeg.c
--- xvidcap-1.1.3.orig/src/xtoffmpeg.c	2004-02-15 22:00:54 +0300
+++ xvidcap-1.1.3/src/xtoffmpeg.c	2005-04-14 13:56:57 +0400
@@ -186,24 +186,33 @@
         
         frame_bytes = enc->frame_size * 2 * enc->channels;
         
         while (fifo_read (&ost->fifo, audio_buf, frame_bytes,
         &ost->fifo.rptr) == 0) {
-            ret = avcodec_encode_audio (enc, audio_out, audio_out_size,
-            (short *)audio_buf);
 
-//            audio_pts = (double)au_out_st->st->pts.val * output_file->pts_num / output_file->pts_den;
-//            video_pts = (double)out_st->pts.val * output_file->pts_num / output_file->pts_den;
+	    AVPacket pkt;
+    
+            // initialize audio output packet
+            av_init_packet(&pkt);
+            
+            pkt.size = avcodec_encode_audio (enc, audio_out, audio_out_size, (short *)audio_buf);
+            pkt.stream_index= ost->st->index;
+            if (enc->coded_frame) { 
+		    pkt.pts = enc->coded_frame->pts;
+	    }
+            pkt.flags |= PKT_FLAG_KEY;
+            pkt.data = audio_out;
 
-//            if ( audio_pts < video_pts && pthread_mutex_trylock (&mp) == 0 ) {
             if (  pthread_mutex_trylock (&mp) == 0 ) {
                 /* write the compressed frame in the media file */
-                if (av_write_frame (s, ost->st->index, audio_out, ret) != 0) {
+                if (av_write_frame (s, &pkt) != 0) {
                     fprintf (stderr, "Error while writing audio frame\n");
                     exit (1);
                 }
+                
                 if ( pthread_mutex_unlock (&mp) > 0 ) {
                     fprintf (stderr, "Couldn't unlock mutex lock for writing audio frame\n");
                 }
+
             } /* else {
              fprintf (stderr, "Can't lock mutex for writing audio frame\n");
              } */
@@ -239,6 +247,7 @@
 }
 
 
+
 void cleanup_thread_when_stopped () {
     int retval = 0;
     
@@ -371,6 +380,27 @@
 #endif // HAVE_FFMPEG_AUDIO
 
 
+static void do_video_out (AVFormatContext *s, AVStream *ost, unsigned char *buf, int size) {
+    AVCodecContext *enc; 
+    AVPacket pkt;
+    
+    enc = &(ost->codec);
+
+    // initialize video output packet
+    av_init_packet(&pkt);
+    pkt.size = size;
+    if (enc->coded_frame) pkt.pts = enc->coded_frame->pts;
+    if (enc->coded_frame && enc->coded_frame->key_frame) pkt.flags |= PKT_FLAG_KEY;
+    pkt.stream_index= ost->index;
+    pkt.data= buf;
+
+    if (av_write_frame (s, &pkt) != 0) {
+        fprintf (stderr, "Error while writing audio frame\n");
+        exit (1);
+    }
+} 
+
+
 #ifdef DEBUG
 /*
  * dump info about XImage - for debugging purposes
@@ -1117,7 +1147,7 @@
     }
 #endif // HAVE_FFMPEG_AUDIO
     /* write frame to file */
-    av_write_frame (output_file, out_st->index, outbuf, out_size);
+    do_video_out (output_file, out_st, outbuf, out_size);
 #ifdef HAVE_FFMPEG_AUDIO
     /* release the mutex */
     if (job->flags & FLG_AUDIO_WANTED) {
