1 /* GStreamer taglib-based APEv2 muxer
2 * Copyright (C) 2006 Christophe Fergeau <teuf@gnome.org>
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) 2006 Sebastian Dröge <mail@slomosnail.de>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-apev2mux
24 * @see_also: #GstTagSetter
28 * This element adds APEv2 tags to the beginning of a stream using the taglib
32 * Applications can set the tags to write using the #GstTagSetter interface.
33 * Tags sent by upstream elements will be picked up automatically (and merged
34 * according to the merge mode set via the tag setter interface).
37 * Here is a simple pipeline that transcodes a file from Ogg/Vorbis to mp3
38 * format with an APEv2 that contains the same as the the Ogg/Vorbis file:
40 * gst-launch -v filesrc location=foo.ogg ! decodebin ! audioconvert ! lame ! apev2mux ! filesink location=foo.mp3
42 * Make sure the Ogg/Vorbis file actually has comments to preserve.
43 * You can verify the tags were written using:
45 * gst-launch -m filesrc location=foo.mp3 ! apedemux ! fakesink silent=TRUE 2> /dev/null | grep taglist
55 #include "gstapev2mux.h"
60 #include <gst/tag/tag.h>
62 using namespace TagLib;
64 GST_DEBUG_CATEGORY_STATIC (gst_apev2_mux_debug);
65 #define GST_CAT_DEFAULT gst_apev2_mux_debug
67 static const GstElementDetails gst_apev2_mux_details =
68 GST_ELEMENT_DETAILS ("TagLib-based APEv2 Muxer",
70 "Adds an APEv2 header to the beginning of files using taglib",
71 "Sebastian Dröge <mail@slomosnail.de>");
73 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
76 GST_STATIC_CAPS ("application/x-apetag"));
79 GST_BOILERPLATE (GstApev2Mux, gst_apev2_mux, GstTagLibMux,
80 GST_TYPE_TAG_LIB_MUX);
82 static GstBuffer *gst_apev2_mux_render_tag (GstTagLibMux * mux,
83 GstTagList * taglist);
86 gst_apev2_mux_base_init (gpointer g_class)
88 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
90 gst_element_class_add_pad_template (element_class,
91 gst_static_pad_template_get (&src_template));
93 gst_element_class_set_details (element_class, &gst_apev2_mux_details);
95 GST_DEBUG_CATEGORY_INIT (gst_apev2_mux_debug, "apev2mux", 0,
96 "taglib-based APEv2 tag muxer");
100 gst_apev2_mux_class_init (GstApev2MuxClass * klass)
102 GST_TAG_LIB_MUX_CLASS (klass)->render_tag =
103 GST_DEBUG_FUNCPTR (gst_apev2_mux_render_tag);
107 gst_apev2_mux_init (GstApev2Mux * apev2mux, GstApev2MuxClass * apev2mux_class)
113 add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
115 APE::Tag * apev2tag = (APE::Tag *) user_data;
118 /* FIXME: if there are several values set for the same tag, this won't
119 * work, only the first value will be taken into account
121 if (strcmp (tag, GST_TAG_TITLE) == 0) {
124 result = gst_tag_list_get_string_index (list, tag, 0, &title);
125 if (result != FALSE) {
126 GST_DEBUG ("Setting title to %s", title);
127 apev2tag->setTitle (String::String (title, String::UTF8));
130 } else if (strcmp (tag, GST_TAG_ALBUM) == 0) {
133 result = gst_tag_list_get_string_index (list, tag, 0, &album);
134 if (result != FALSE) {
135 GST_DEBUG ("Setting album to %s", album);
136 apev2tag->setAlbum (String::String (album, String::UTF8));
139 } else if (strcmp (tag, GST_TAG_ARTIST) == 0) {
142 result = gst_tag_list_get_string_index (list, tag, 0, &artist);
143 if (result != FALSE) {
144 GST_DEBUG ("Setting artist to %s", artist);
145 apev2tag->setArtist (String::String (artist, String::UTF8));
148 } else if (strcmp (tag, GST_TAG_GENRE) == 0) {
151 result = gst_tag_list_get_string_index (list, tag, 0, &genre);
152 if (result != FALSE) {
153 GST_DEBUG ("Setting genre to %s", genre);
154 apev2tag->setGenre (String::String (genre, String::UTF8));
157 } else if (strcmp (tag, GST_TAG_COMMENT) == 0) {
160 result = gst_tag_list_get_string_index (list, tag, 0, &comment);
161 if (result != FALSE) {
162 GST_DEBUG ("Setting comment to %s", comment);
163 apev2tag->setComment (String::String (comment, String::UTF8));
166 } else if (strcmp (tag, GST_TAG_DATE) == 0) {
169 result = gst_tag_list_get_date_index (list, tag, 0, &date);
170 if (result != FALSE) {
173 year = g_date_get_year (date);
174 GST_DEBUG ("Setting track year to %d", year);
175 apev2tag->setYear (year);
178 } else if (strcmp (tag, GST_TAG_TRACK_NUMBER) == 0) {
181 result = gst_tag_list_get_uint_index (list, tag, 0, &track_number);
182 if (result != FALSE) {
185 result = gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT,
190 tag_str = g_strdup_printf ("%d/%d", track_number, total_tracks);
191 GST_DEBUG ("Setting track number to %s", tag_str);
192 apev2tag->addValue (String::String ("TRACK", String::UTF8),
193 String::String (tag_str, String::UTF8), true);
196 GST_DEBUG ("Setting track number to %d", track_number);
197 apev2tag->setTrack (track_number);
200 } else if (strcmp (tag, GST_TAG_TRACK_COUNT) == 0) {
203 if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_NUMBER, 0, &n)) {
204 GST_DEBUG ("track-count handled with track-number, skipping");
205 } else if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT, 0, &n)) {
208 tag_str = g_strdup_printf ("0/%d", n);
209 GST_DEBUG ("Setting track number to %s", tag_str);
210 apev2tag->addValue (String::String ("TRACK", String::UTF8),
211 String::String (tag_str, String::UTF8), true);
215 } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
218 result = gst_tag_list_get_uint_index (list, tag, 0, &volume_number);
220 if (result != FALSE) {
224 result = gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
228 tag_str = g_strdup_printf ("CD %d/%d", volume_number, volume_count);
230 tag_str = g_strdup_printf ("CD %d", volume_number);
233 GST_DEBUG ("Setting album number to %s", tag_str);
235 apev2tag->addValue (String::String ("MEDIA", String::UTF8),
236 String::String (tag_str, String::UTF8), true);
239 } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_COUNT) == 0) {
242 if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_NUMBER, 0, &n)) {
243 GST_DEBUG ("volume-count handled with volume-number, skipping");
244 } else if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
248 tag_str = g_strdup_printf ("CD 0/%u", n);
249 GST_DEBUG ("Setting album volume number/count to %s", tag_str);
251 apev2tag->addValue (String::String ("MEDIA", String::UTF8),
252 String::String (tag_str, String::UTF8), true);
256 } else if (strcmp (tag, GST_TAG_COPYRIGHT) == 0) {
259 result = gst_tag_list_get_string_index (list, tag, 0, ©right);
261 if (result != FALSE) {
262 GST_DEBUG ("Setting copyright to %s", copyright);
263 apev2tag->addValue (String::String ("COPYRIGHT", String::UTF8),
264 String::String (copyright, String::UTF8), true);
267 } else if (strcmp (tag, GST_TAG_LOCATION) == 0) {
270 result = gst_tag_list_get_string_index (list, tag, 0, &location);
272 if (result != FALSE) {
273 GST_DEBUG ("Setting location to %s", location);
274 apev2tag->addValue (String::String ("FILE", String::UTF8),
275 String::String (location, String::UTF8), true);
278 } else if (strcmp (tag, GST_TAG_ISRC) == 0) {
281 result = gst_tag_list_get_string_index (list, tag, 0, &isrc);
283 if (result != FALSE) {
284 GST_DEBUG ("Setting ISRC to %s", isrc);
285 apev2tag->addValue (String::String ("ISRC", String::UTF8),
286 String::String (isrc, String::UTF8), true);
289 } else if (strcmp (tag, GST_TAG_TRACK_GAIN) == 0) {
292 result = gst_tag_list_get_double_index (list, tag, 0, &value);
294 if (result != FALSE) {
295 gchar *track_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
297 track_gain = g_ascii_dtostr (track_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
298 GST_DEBUG ("Setting track gain to %s", track_gain);
299 apev2tag->addValue (String::String ("REPLAYGAIN_TRACK_GAIN",
300 String::UTF8), String::String (track_gain, String::UTF8), true);
303 } else if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0) {
306 result = gst_tag_list_get_double_index (list, tag, 0, &value);
308 if (result != FALSE) {
309 gchar *track_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
311 track_peak = g_ascii_dtostr (track_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
312 GST_DEBUG ("Setting track peak to %s", track_peak);
313 apev2tag->addValue (String::String ("REPLAYGAIN_TRACK_PEAK",
314 String::UTF8), String::String (track_peak, String::UTF8), true);
317 } else if (strcmp (tag, GST_TAG_ALBUM_GAIN) == 0) {
320 result = gst_tag_list_get_double_index (list, tag, 0, &value);
322 if (result != FALSE) {
323 gchar *album_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
325 album_gain = g_ascii_dtostr (album_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
326 GST_DEBUG ("Setting album gain to %s", album_gain);
327 apev2tag->addValue (String::String ("REPLAYGAIN_ALBUM_GAIN",
328 String::UTF8), String::String (album_gain, String::UTF8), true);
331 } else if (strcmp (tag, GST_TAG_ALBUM_PEAK) == 0) {
334 result = gst_tag_list_get_double_index (list, tag, 0, &value);
336 if (result != FALSE) {
337 gchar *album_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
339 album_peak = g_ascii_dtostr (album_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
340 GST_DEBUG ("Setting album peak to %s", album_peak);
341 apev2tag->addValue (String::String ("REPLAYGAIN_ALBUM_PEAK",
342 String::UTF8), String::String (album_peak, String::UTF8), true);
346 GST_WARNING ("Unsupported tag: %s", tag);
351 gst_apev2_mux_render_tag (GstTagLibMux * mux, GstTagList * taglist)
354 ByteVector rendered_tag;
359 gst_tag_list_foreach (taglist, add_one_tag, &apev2tag);
361 rendered_tag = apev2tag.render ();
362 tag_size = rendered_tag.size ();
364 GST_LOG_OBJECT (mux, "tag size = %d bytes", tag_size);
366 /* Create buffer with tag */
367 buf = gst_buffer_new_and_alloc (tag_size);
368 memcpy (GST_BUFFER_DATA (buf), rendered_tag.data (), tag_size);
369 gst_buffer_set_caps (buf, GST_PAD_CAPS (mux->srcpad));
375 gst_apev2_mux_plugin_init (GstPlugin * plugin)
377 if (!gst_element_register (plugin, "apev2mux", GST_RANK_NONE,