retag.c 2.87 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* 
 * This example shows how to use interfaces and the tag subsystem.
 * It takes an mp3 file as input, and makes an ogg file out of it. While doing
 * this, it parses the filename and sets artist and title in the ogg file.
 * It assumes the filename to be "<artist> - <title>.mp3"
 * 
 * Run the program as "retag <mp3 file>"
 *
 * To run this program, you need to have the gst-plugins package (specifically
 * the vorbis and mad plugins) installed.
 */

/* main header */
#include <gst/gst.h>
/* and a header we need for the string manipulation */
#include <string.h>

int
main (int argc, char *argv[]) 
{
  GstElement *bin, *filesrc, *tag_changer, *filesink;
  gchar *artist, *title, *ext, *filename;
  
24
25
26
  /* initialize GStreamer */
  gst_init (&argc, &argv);

Benjamin Otte's avatar
Benjamin Otte committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  /* check that the argument is there */
  if (argc != 2) {
    g_print ("usage: %s <mp3 file>\n", argv[0]);
    return 1;
  }

  /* parse the mp3 name */
  artist = strrchr (argv[1], '/');
  if (artist == NULL)
    artist = argv[1];
  artist = g_strdup (artist);
  ext = strrchr (artist, '.');
  if (ext) *ext = '\0';
  title = strstr (artist, " - ");
  if (title == NULL) {
    g_print ("The format of the mp3 file is invalid.\n");
    return 1;
  }
  *title = '\0';
  title += 3;
  
  
  /* create a new bin to hold the elements */
  bin = gst_pipeline_new ("pipeline");
  g_assert (bin);

  /* create a file reader */
  filesrc = gst_element_factory_make ("filesrc", "disk_source");
  g_assert (filesrc);

  /* now it's time to get the tag_changer */
  tag_changer = gst_element_factory_make ("id3tag", "tag_changer");
  if (!tag_changer) {
    g_print ("could not find plugin \"mad\"");
    return 1;
  }

  /* and a file writer */
  filesink = gst_element_factory_make ("filesink", "filesink");
  g_assert (filesink);

  /* set the filenames */
  filename = g_strdup_printf ("%s.temp", argv[1]); /* easy solution */
  g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
  g_object_set (G_OBJECT (filesink), "location", filename, NULL);

  /* make sure the tag setter uses our stuff 
     (though that should already be default) */
  gst_tag_setter_set_merge_mode (GST_TAG_SETTER (tag_changer), GST_TAG_MERGE_KEEP);
  /* set the tagging information */
  gst_tag_setter_add (GST_TAG_SETTER (tag_changer), GST_TAG_MERGE_REPLACE,
		      GST_TAG_ARTIST, artist, 
		      GST_TAG_TITLE, title, 
		      NULL);

  /* add objects to the main pipeline */
  gst_bin_add_many (GST_BIN (bin), filesrc, tag_changer, filesink, NULL);

  /* link the elements */
86
  g_assert (gst_element_link_many (filesrc, tag_changer, filesink));
Benjamin Otte's avatar
Benjamin Otte committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  
  /* start playing */
  gst_element_set_state (bin, GST_STATE_PLAYING);

  while (gst_bin_iterate (GST_BIN (bin)));

  /* stop the bin */
  gst_element_set_state (bin, GST_STATE_NULL);

  /* rename the file to the correct name and remove the old one */
  remove (argv[1]);
  rename (filename, argv[1]);
  g_free (filename);

  return 0;
}