diff -ur dvbcut/src/mpgfile.cpp dvbcut-wgmin/src/mpgfile.cpp
--- dvbcut/src/mpgfile.cpp	Sun May 27 17:24:25 2007
+++ dvbcut-wgmin/src/mpgfile.cpp	Mon May 28 12:47:08 2007
@@ -565,8 +565,12 @@
       {
         bool stopped=false;
         sd->audio_addpts();
-        streamdata::itemlisttype::const_iterator nx,it=sd->itemlist().begin();
+        streamdata::itemlisttype::const_iterator nx,it;
 
+	while ((it = sd->itemlist().begin())!=sd->itemlist().end() &&
+	       !it->is_frame())
+	  sd->pop();
+	if (it!=sd->itemlist().end())
         while(!stopped)
         {
           audiopts[a]=it->headerpts(audiopts[a]);
@@ -578,7 +582,7 @@
           }
           nx=it;
           ++nx;
-          while (nx!=sd->itemlist().end() && !nx->headerhaspts())
+          while (nx!=sd->itemlist().end() && !nx->is_frame())
             ++nx;
           if (nx==sd->itemlist().end())
             break;
@@ -619,6 +623,7 @@
           if (bytes>0)
           {
             pts_t pts=audiopts[a]-audiooffset[a];
+	    //fprintf(stderr, "mux.put audio %d %lld\n", bytes, pts);
             mux.putpacket(audiostream(a),sd->getdata(),bytes,pts,pts,MUXER_FLAG_KEY);
 
             sd->discard(bytes);
diff -ur dvbcut/src/streamdata.cpp dvbcut-wgmin/src/streamdata.cpp
--- dvbcut/src/streamdata.cpp	Sat May 12 22:24:20 2007
+++ dvbcut-wgmin/src/streamdata.cpp	Mon May 28 12:48:40 2007
@@ -37,28 +37,28 @@
       && (header[2] & 0x0c) != 0x0c;	/* sampling rate code */
 }
 
-static int mpaframe(const void *data, int &pos, int len)
+// return frame size in bytes, or 0 for no valid frame
+static int mpaframe(const void *data, int &pos, int len, int &duration)
   {
   const unsigned char *d=(const unsigned char *)data;
-  while ((pos+2)<len && !is_mpa(&d[pos]))
-    ++pos;
-  if (pos+2>=len)
-    return 0;
-
-  int layer=(d[pos+1]>>1)&0x03;
-  int samples=(layer==4-1)?384:1152;
-  int samplingrate=mpegaudio_rates[(d[pos+2]>>2)&0x03];
-  int bitratecode=(d[pos+2]>>4)&0x0f;
-
-  int skipbytes=(mpegaudio_bitrate[layer][bitratecode]*125)*samples/samplingrate;
-  if (skipbytes)
-    pos+=skipbytes;
-  else
-    pos+=3;
-
-  while ((pos+2)<len && !is_mpa(&d[pos]))
-    ++pos;
-  return samples*90000/samplingrate; // Duration of MPEG audio frame in 90000Hz units
+
+  for (; pos+2<len; ++pos) {
+    if (!is_mpa(&d[pos]))
+      continue;
+
+    int layer=(d[pos+1]>>1)&0x03;
+    int samples=(layer==4-1)?384:1152;
+    int samplingrate=mpegaudio_rates[(d[pos+2]>>2)&0x03];
+    int bitratecode=(d[pos+2]>>4)&0x0f;
+
+    int skipbytes=(mpegaudio_bitrate[layer][bitratecode]*125)*samples/samplingrate;
+    if (skipbytes>0 && pos+skipbytes+2<len && is_mpa(&d[pos+skipbytes])) {
+      //fprintf(stderr, "mpaframe: samples=%d pos=%d len=%d\n", samples, pos,len);
+      duration = samples*90000/samplingrate; // Duration of MPEG audio frame in 90000Hz units
+      return skipbytes;
+    }
+  }
+  return 0;
   }
 
 //
@@ -101,19 +101,23 @@
 
 static int ac3_frameduration[4]={270000*1536/480,270000*1536/441,270000*1536/320,270000*1536/480};
 
-static int ac3frame(const void *data, int &pos, int len)
+// return frame size in bytes, or 0 for no valid frame
+static int ac3frame(const void *data, int &pos, int len, int &duration)
   {
   const unsigned char *d=(const unsigned char *)data;
-  while ((pos+2)<len && (*(uint16_t*)(d+pos)!=AC3_SYNCWORD))
-    ++pos;
-  if (pos+2>=len)
-    return 0;
-
-  pos+=ac3_framelength[d[pos+4]];
-
-  while ((pos+2)<len && (*(uint16_t*)(d+pos)!=AC3_SYNCWORD))
-    ++pos;
-  return ac3_frameduration[(d[pos+4]>>6)&3]/300; // Duration of AC3 audio frame in 90000Hz units
+
+  for(; pos+4<len; ++pos) {
+  if ((*(uint16_t*)(d+pos)!=AC3_SYNCWORD))
+    continue;
+
+  int framelength = ac3_framelength[d[pos+4]];
+
+  if ((pos+framelength+1)<len && (*(uint16_t*)(d+pos+framelength)==AC3_SYNCWORD)) {
+    duration = ac3_frameduration[(d[pos+4]>>6)&3]/300; // Duration of AC3 audio frame in 90000Hz units
+    return framelength;
+  }
+  }
+  return 0;
   }
 
 void streamdata::audio_addpts(uint32_t startbufferpos, bool onepacket)
@@ -131,7 +135,6 @@
   while (it!=items.end() && it->bufferposition<startbufferpos)
     ++it;
   itemlisttype::iterator nx;
-  uint32_t bufferposition=it->bufferposition;
   uint32_t stopbufferpos=0;
   pts_t pts=0;
 
@@ -162,40 +165,42 @@
           }
         }
 
-      pts=it->headerpts();
-
       nx=it;
       ++nx;
       }
 
-    int pos=0;
-    int len=inbytes()-(bufferposition-getoffset());
-    int ptsplus;
-    if (type==streamtype::ac3audio)
-      ptsplus=ac3frame(getdata(bufferposition),pos,len);
-    else
-      ptsplus=mpaframe(getdata(bufferposition),pos,len);
-    if (pos+2>=len)
-      break;
-    if (ptsplus==0) {
-      needsync=true;
-      continue;
-      }
-
-    bufferposition+=pos;
-    pts+=ptsplus;
+    pts=it->headerpts();
+    uint32_t bufferposition=it->bufferposition;
 
-    if (nx==items.end() || bufferposition<nx->bufferposition) {
-      it=items.insert(nx,item(it->fileposition+(bufferposition-it->bufferposition),
-                              STREAM_ITEM_FLAG_HAS_PTS|STREAM_ITEM_FLAG_DATA_ALIGNMENT_INDICATOR,
-                              pts,bufferposition));
+    // parse frames in this item
+    int pos=0;
+    int ptsplus=0,bytes;
+    while ((bytes = (type==streamtype::ac3audio) ?
+	    ac3frame(getdata(bufferposition),pos,inbytes()-(bufferposition-getoffset()),ptsplus) :
+	    mpaframe(getdata(bufferposition),pos,inbytes()-(bufferposition-getoffset()),ptsplus))) {
+      if (pos == 0) {
+	//fprintf(stderr, "setting frame at item\n");
+	it->flags |= STREAM_ITEM_FLAG_FRAME;
       } else {
-      it=nx;
-      ++nx;
-      pts=it->headerpts();
+	bufferposition+=pos;
+	if (nx != items.end() && bufferposition >= nx->bufferposition) {
+	  //fprintf(stderr, "hit next %ld>=%ld pts=%lld interp=%lld\n",
+	  //  bufferposition, nx->bufferposition,nx->headerpts(),
+	  //  pts - (bufferposition - nx->bufferposition)*ptsplus/bytes);
+	  break;
+	}
+	it=items.insert(nx,item(it->fileposition+(bufferposition-it->bufferposition),
+				STREAM_ITEM_FLAG_HAS_PTS|STREAM_ITEM_FLAG_FRAME,
+				pts,bufferposition));
+	//fprintf(stderr, "sd: insert %p (pos %ld, nxflags %d) pts=%lld\n",
+	//	&(*it), bufferposition, nx->flags, pts);
       }
-
+      pos = bytes;
+      pts+=ptsplus;
     }
+    it=nx;
+    ++nx;
+  }
   }
 
 /// Find the packet with the highest PTS less or equal than the given pts and returns its buffer position.
diff -ur dvbcut/src/streamdata.h dvbcut-wgmin/src/streamdata.h
--- dvbcut/src/streamdata.h	Sat May 12 22:24:20 2007
+++ dvbcut-wgmin/src/streamdata.h	Mon May 28 12:47:08 2007
@@ -21,7 +21,9 @@
 
 #include <string>
 #include <list>
+extern "C" {
 #include <ffmpeg/avformat.h>
+}
 
 #include "port.h"
 #include "tsfile.h"
@@ -32,6 +34,7 @@
 
 #define STREAM_ITEM_FLAG_DATA_ALIGNMENT_INDICATOR (1<<0)
 #define STREAM_ITEM_FLAG_HAS_PTS                  (1<<1)
+#define STREAM_ITEM_FLAG_FRAME                    (1<<2)
 
 /**
 @author Sven Over
@@ -94,6 +97,10 @@
         flags |= STREAM_ITEM_FLAG_DATA_ALIGNMENT_INDICATOR;
       else
         flags &=~ STREAM_ITEM_FLAG_DATA_ALIGNMENT_INDICATOR;
+      }
+    bool is_frame() const
+      {
+      return flags & STREAM_ITEM_FLAG_FRAME;
       }
     }
   ;
