merge in tagging
[platform/upstream/gstreamer.git] / examples / retag / transcode.c
1 /* 
2  * This example shows how to use interfaces and the tag subsystem.
3  * It takes an mp3 file as input, and makes an ogg file out of it. While doing
4  * this, it parses the filename and sets artist and title in the ogg file.
5  * It assumes the filename to be "<artist> - <title>.mp3"
6  * 
7  * Run the program as "retag <mp3 file>"
8  *
9  * To run this program, you need to have the gst-plugins package (specifically
10  * the vorbis and mad plugins) installed.
11  */
12
13 /* main header */
14 #include <gst/gst.h>
15 /* and a header we need for the string manipulation */
16 #include <string.h>
17
18 int
19 main (int argc, char *argv[]) 
20 {
21   GstElement *bin, *filesrc, *decoder, *encoder, *filesink;
22   gchar *artist, *title, *ext, *filename;
23   
24   /* initialize GStreamer */
25   gst_init (&argc, &argv);
26
27   /* check that the argument is there */
28   if (argc != 2) {
29     g_print ("usage: %s <mp3 file>\n", argv[0]);
30     return 1;
31   }
32
33   /* parse the mp3 name */
34   artist = strrchr (argv[1], '/');
35   if (artist == NULL)
36     artist = argv[1];
37   artist = g_strdup (artist);
38   ext = strrchr (artist, '.');
39   if (ext) *ext = '\0';
40   title = strstr (artist, " - ");
41   if (title == NULL) {
42     g_print ("The format of the mp3 file is invalid.\n");
43     return 1;
44   }
45   *title = '\0';
46   title += 3;
47   
48   
49   /* create a new bin to hold the elements */
50   bin = gst_pipeline_new ("pipeline");
51   g_assert (bin);
52
53   /* create a file reader */
54   filesrc = gst_element_factory_make ("filesrc", "disk_source");
55   g_assert (filesrc);
56
57   /* now it's time to get the decoder */
58   decoder = gst_element_factory_make ("mad", "decode");
59   if (!decoder) {
60     g_print ("could not find plugin \"mad\"");
61     return 1;
62   }
63
64   /* create the encoder */
65   encoder = gst_element_factory_make ("vorbisenc", "encoder");
66   if (!encoder) {
67     g_print ("cound not find plugin \"vorbisenc\"");
68     return 1;
69   }
70   
71   /* and a file writer */
72   filesink = gst_element_factory_make ("filesink", "filesink");
73   g_assert (filesink);
74
75   /* set the filenames */
76   filename = g_strdup_printf ("%s.ogg", argv[1]); /* easy solution */
77   g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
78   g_object_set (G_OBJECT (filesink), "location", filename, NULL);
79   g_free (filename);
80
81   /* make sure the tag setter uses our stuff 
82      (though that should already be default) */
83   gst_tag_setter_set_merge_mode (GST_TAG_SETTER (encoder), GST_TAG_MERGE_KEEP);
84   /* set the tagging information */
85   gst_tag_setter_add (GST_TAG_SETTER (encoder), GST_TAG_MERGE_REPLACE,
86                       GST_TAG_ARTIST, artist, 
87                       GST_TAG_TITLE, title, 
88                       NULL);
89
90   /* add objects to the main pipeline */
91   gst_bin_add_many (GST_BIN (bin), filesrc, decoder, encoder, filesink, NULL);
92
93   /* link the elements */
94   gst_element_link_many (filesrc, decoder, encoder, filesink, NULL);
95   
96   /* start playing */
97   gst_element_set_state (bin, GST_STATE_PLAYING);
98
99   while (gst_bin_iterate (GST_BIN (bin)));
100
101   /* stop the bin */
102   gst_element_set_state (bin, GST_STATE_NULL);
103
104   return 0;
105 }
106