diff -u -p -r1.82 rb-metadata-gst.c
--- metadata/rb-metadata-gst.c	14 Jul 2006 12:27:15 -0000	1.82
+++ metadata/rb-metadata-gst.c	21 Oct 2006 04:48:43 -0000
@@ -154,13 +154,42 @@ rb_add_flac_tagger (RBMetaData *md, GstE
 static void
 id3_pad_added_cb (GstElement *demux, GstPad *pad, GstElement *mux)
 {
+	GstCaps *caps;
 	GstPad *mux_pad;
+	GstStructure *structure;
+	const gchar *mimetype;
 
-	mux_pad = gst_element_get_compatible_pad (mux, pad, NULL);
-	if (gst_pad_link (pad, mux_pad) != GST_PAD_LINK_OK)
-		rb_debug ("unable to link pad from id3demux to id3mux");
-	else
-		rb_debug ("linked pad from id3de to id3mux");
+	caps = gst_pad_get_caps (pad);
+	structure = gst_caps_get_structure (caps, 0);
+	mimetype = gst_structure_get_name (structure);
+
+	if (strcmp (mimetype, "application/x-apetag") == 0) {
+		/* strip out APE tags, to prevent bug #362876*/
+		GstElement *apedemux;
+
+		apedemux = gst_element_factory_make ("apedemux", NULL);
+		if (apedemux == NULL) {
+			rb_debug ("unable to create APE demuxer");
+			return;
+		}
+
+		mux_pad = gst_element_get_compatible_pad (apedemux, pad, NULL);
+
+		if (gst_pad_link (pad, mux_pad) != GST_PAD_LINK_OK) {
+			rb_debug ("unable to link pad from id3demux to apedemux");
+		} else {
+			rb_debug ("pluggin apedemux to strip APE tags");
+			g_signal_connect (apedemux, "pad-added", (GCallback)id3_pad_added_cb, mux);
+		}
+	} else {
+		/* plug id3mux in to re-tag */
+		mux_pad = gst_element_get_compatible_pad (mux, pad, NULL);
+
+		if (gst_pad_link (pad, mux_pad) != GST_PAD_LINK_OK)
+			rb_debug ("unable to link pad from id3demux to id3mux");
+		else
+			rb_debug ("linked pad from id3de to id3mux");
+	}
 }
 
 static gboolean
