~jcowgill/ubuntu/trusty/easytag/bug-1295882

« back to all changes in this revision

Viewing changes to src/mp4_tag.c

  • Committer: Bazaar Package Importer
  • Author(s): Maia Kozheva
  • Date: 2011-06-09 01:03:44 UTC
  • Revision ID: james.westby@ubuntu.com-20110609010344-ybldr7th761wyquz
Tags: 2.1.6+git20110423-3ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  + debian/control:
    - Add easytag-aac transitional package.
  + debian/install:
    - Install upstream files to easytag binary package.
  + debian/rules:
    - Do not fail on deleting ChangeLog file if it doesn't exist.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 */
22
22
 
23
23
/* Portions of this code was borrowed from the MPEG4IP tools project */
24
 
#include <config.h> // For definition of ENABLE_MP4
 
24
#include <config.h> // For definition of ENABLE_TAGLIB
25
25
 
26
26
#ifdef ENABLE_MP4
27
27
 
50
50
#undef PACKAGE_STRING
51
51
#undef PACKAGE_TARNAME
52
52
#undef PACKAGE_VERSION
53
 
#include <mp4v2/mp4v2.h>
 
53
#include <tag_c.h>
54
54
 
55
55
 
56
56
/****************
79
79
gboolean Mp4tag_Read_File_Tag (gchar *filename, File_Tag *FileTag)
80
80
{
81
81
    FILE   *file;
82
 
    MP4FileHandle mp4file = NULL;
83
 
    uint16_t track, track_total;
84
 
    uint16_t disk, disktotal;
85
 
    u_int8_t *coverArt;
86
 
    u_int32_t coverSize;
87
 
    Picture *prev_pic = NULL;
88
 
#ifdef NEWMP4
89
 
    gint pic_num;
90
 
#endif
 
82
    TagLib_File *mp4file;
 
83
    TagLib_Tag *tag;
 
84
    unsigned track;
91
85
    
92
86
    if (!filename || !FileTag)
93
87
        return FALSE;
102
96
    fclose(file); // We close it cause mp4 opens/closes file itself
103
97
 
104
98
    /* Get data from tag */
105
 
    mp4file = MP4Read(filename, 0);
106
 
    if (mp4file == MP4_INVALID_FILE_HANDLE)
 
99
    mp4file = taglib_file_new_type(filename, TagLib_File_MP4);
 
100
    if (mp4file == NULL)
107
101
    {
108
102
        gchar *filename_utf8 = filename_to_display(filename);
109
103
        Log_Print(LOG_ERROR,_("ERROR while opening file: '%s' (%s)."),filename_utf8,_("MP4 format invalid"));
112
106
    }
113
107
 
114
108
    /* TODO Add error detection */
 
109
    tag = taglib_file_tag(mp4file);
115
110
 
116
111
    /*********
117
112
     * Title *
118
113
     *********/
119
 
    MP4GetMetadataName(mp4file, &FileTag->title);
 
114
    FileTag->title = g_strdup(taglib_tag_title(tag));
120
115
 
121
116
    /**********
122
117
     * Artist *
123
118
     **********/
124
 
    MP4GetMetadataArtist(mp4file, &FileTag->artist);
 
119
    FileTag->artist = g_strdup(taglib_tag_artist(tag));
125
120
 
126
121
    /*********
127
122
     * Album *
128
123
     *********/
129
 
    MP4GetMetadataAlbum(mp4file, &FileTag->album);
 
124
    FileTag->album = g_strdup(taglib_tag_album(tag));
130
125
 
131
126
    /**********************
132
127
     * Disk / Total Disks *
133
128
     **********************/
 
129
    /*
134
130
    if (MP4GetMetadataDisk(mp4file, &disk, &disktotal))
135
131
    {
136
132
        if (disk != 0 && disktotal != 0)
142
138
        //if (disktotal != 0)
143
139
        //    FileTag->disk_number_total = g_strdup_printf("%d",(gint)disktotal);
144
140
    }
 
141
    */
145
142
 
146
143
    /********
147
144
     * Year *
148
145
     ********/
149
 
    MP4GetMetadataYear(mp4file, &FileTag->year);
 
146
    FileTag->year = g_strdup_printf("%u", taglib_tag_year(tag));
150
147
 
151
148
    /*************************
152
149
     * Track and Total Track *
153
150
     *************************/
154
 
    if (MP4GetMetadataTrack(mp4file, &track, &track_total))
 
151
    track = taglib_tag_track(tag);
 
152
    
155
153
    {
156
154
        if (track != 0)
157
155
            FileTag->track = g_strdup_printf("%.*d",NUMBER_TRACK_FORMATED_SPIN_BUTTON,(gint)track); // Just to have numbers like this : '01', '05', '12', ...
158
 
        if (track_total != 0)
159
 
            FileTag->track_total = g_strdup_printf("%.*d",NUMBER_TRACK_FORMATED_SPIN_BUTTON,(gint)track_total); // Just to have numbers like this : '01', '05', '12', ...
 
156
        //if (track_total != 0)
 
157
        //    FileTag->track_total = g_strdup_printf("%.*d",NUMBER_TRACK_FORMATED_SPIN_BUTTON,(gint)track_total); // Just to have numbers like this : '01', '05', '12', ...
160
158
    }
161
159
 
162
160
    /*********
163
161
     * Genre *
164
162
     *********/
165
 
    MP4GetMetadataGenre(mp4file, &FileTag->genre);
 
163
    FileTag->genre = g_strdup(taglib_tag_genre(tag));
166
164
 
167
165
    /***********
168
166
     * Comment *
169
167
     ***********/
170
 
    MP4GetMetadataComment(mp4file, &FileTag->comment);
 
168
    FileTag->comment = g_strdup(taglib_tag_comment(tag));
171
169
 
172
170
    /**********************
173
171
     * Composer or Writer *
174
172
     **********************/
175
 
    MP4GetMetadataWriter(mp4file, &FileTag->composer);
 
173
    // MP4GetMetadataWriter(mp4file, &FileTag->composer);
176
174
 
177
175
    /*****************
178
176
     * Encoding Tool *
179
177
     *****************/
180
 
    MP4GetMetadataTool(mp4file, &FileTag->encoded_by);
 
178
    // MP4GetMetadataTool(mp4file, &FileTag->encoded_by);
181
179
 
182
180
    /* Unimplemented
183
181
    Tempo / BPM
187
185
    /***********
188
186
     * Picture *
189
187
     ***********/
 
188
     /*
190
189
#ifdef NEWMP4
191
190
    // There version can handle multiple pictures!
192
191
    // Version 1.6 of libmp4v2 introduces an index argument for MP4GetMetadataCoverart
210
209
        pic->type = PICTURE_TYPE_FRONT_COVER;
211
210
        pic->description = NULL;
212
211
    }
213
 
 
 
212
    */
214
213
 
215
214
    /* Free allocated data */
216
 
    MP4Close(mp4file);
 
215
    taglib_tag_free_strings();
 
216
    taglib_file_free(mp4file);
217
217
 
218
218
    return TRUE;
219
219
}
233
233
    gchar    *filename;
234
234
    gchar    *filename_utf8;
235
235
    FILE     *file;
236
 
    MP4FileHandle mp4file = NULL;
 
236
    TagLib_File *mp4file = NULL;
 
237
    TagLib_Tag *tag;
237
238
    gint error = 0;
238
239
 
239
240
    if (!ETFile || !ETFile->FileTag)
252
253
    fclose(file);
253
254
 
254
255
    /* Open file for writing */
255
 
    mp4file = MP4Modify(filename,0,0);
256
 
    if (mp4file == MP4_INVALID_FILE_HANDLE)
 
256
    mp4file = taglib_file_new_type(filename, TagLib_File_MP4);
 
257
    if (mp4file == NULL)
257
258
    {
258
259
        Log_Print(LOG_ERROR,_("ERROR while opening file: '%s' (%s)."),filename_utf8,_("MP4 format invalid"));
259
260
        return FALSE;
260
261
    }
 
262
    
 
263
    tag = taglib_file_tag(mp4file);
261
264
 
262
265
    /*********
263
266
     * Title *
264
267
     *********/
265
268
    if (FileTag->title && g_utf8_strlen(FileTag->title, -1) > 0)
266
269
    {
267
 
        MP4SetMetadataName(mp4file, FileTag->title);
 
270
        taglib_tag_set_title(tag, FileTag->title);
268
271
    }else
269
272
    {
270
273
        //MP4DeleteMetadataName(mp4file); // Not available on mpeg4ip-1.2 (only in 1.3)
271
 
        MP4SetMetadataName(mp4file, "");
 
274
        taglib_tag_set_title(tag, "");
272
275
    }
273
276
 
274
277
    /**********
276
279
     **********/
277
280
    if (FileTag->artist && g_utf8_strlen(FileTag->artist, -1) > 0)
278
281
    {
279
 
        MP4SetMetadataArtist(mp4file, FileTag->artist);
 
282
        taglib_tag_set_artist(tag, FileTag->artist);
280
283
    }else
281
284
    {
282
285
        //MP4DeleteMetadataArtist(mp4file);
283
 
        MP4SetMetadataArtist(mp4file, "");
 
286
        taglib_tag_set_artist(tag, "");
284
287
    }
285
288
 
286
289
    /*********
288
291
     *********/
289
292
    if (FileTag->album && g_utf8_strlen(FileTag->album, -1) > 0)
290
293
    {
291
 
        MP4SetMetadataAlbum(mp4file, FileTag->album);
 
294
        taglib_tag_set_album(tag, FileTag->album);
292
295
    }else
293
296
    {
294
297
        //MP4DeleteMetadataAlbum(mp4file);
295
 
        MP4SetMetadataAlbum(mp4file, "");
 
298
        taglib_tag_set_album(tag, "");
296
299
    }
297
300
 
298
301
    /**********************
301
304
    if (FileTag->disc_number && g_utf8_strlen(FileTag->disc_number, -1) > 0)
302
305
    //|| FileTag->disc_number_total && g_utf8_strlen(FileTag->disc_number_total, -1) > 0)
303
306
    {
304
 
        uint16_t disk      = 0;
305
 
        uint16_t disktotal = 0;
306
 
 
307
307
        /* At the present time, we manage only disk number like '1' or '1/2', we
308
308
         * don't use disk number total... so here we try to decompose */
309
 
        if (FileTag->disc_number)
 
309
        /*if (FileTag->disc_number)
310
310
        {
311
311
            gchar *dn_tmp = g_strdup(FileTag->disc_number);
312
312
            gchar *tmp    = strchr(dn_tmp,'/');
325
325
            }
326
326
            g_free(dn_tmp);
327
327
        }
328
 
        /*if (FileTag->disc_number)
 
328
        if (FileTag->disc_number)
329
329
            disk = atoi(FileTag->disc_number);
330
330
        if (FileTag->disc_number_total)
331
331
            disktotal = atoi(FileTag->disc_number_total);
332
332
        */
333
 
        MP4SetMetadataDisk(mp4file, disk, disktotal);
 
333
        // MP4SetMetadataDisk(mp4file, disk, disktotal);
334
334
    }else
335
335
    {
336
336
        //MP4DeleteMetadataDisk(mp4file);
337
 
        MP4SetMetadataDisk(mp4file, 0, 0);
 
337
        //MP4SetMetadataDisk(mp4file, 0, 0);
338
338
    }
339
339
 
340
340
    /********
342
342
     ********/
343
343
    if (FileTag->year && g_utf8_strlen(FileTag->year, -1) > 0)
344
344
    {
345
 
        MP4SetMetadataYear(mp4file, FileTag->year);
 
345
        taglib_tag_set_year(tag, atoi(FileTag->year));
346
346
    }else
347
347
    {
348
348
        //MP4DeleteMetadataYear(mp4file);
349
 
        MP4SetMetadataYear(mp4file, "");
 
349
        taglib_tag_set_year(tag, 0);
350
350
    }
351
351
 
352
352
    /*************************
353
353
     * Track and Total Track *
354
354
     *************************/
355
355
    if ( (FileTag->track       && g_utf8_strlen(FileTag->track, -1) > 0)
356
 
    ||   (FileTag->track_total && g_utf8_strlen(FileTag->track_total, -1) > 0) )
 
356
    /*||   (FileTag->track_total && g_utf8_strlen(FileTag->track_total, -1) > 0) */)
357
357
    {
358
 
        uint16_t track       = 0;
359
 
        uint16_t track_total = 0;
 
358
        unsigned track       = 0;
360
359
        if (FileTag->track)
361
360
            track = atoi(FileTag->track);
362
 
        if (FileTag->track_total)
363
 
            track_total = atoi(FileTag->track_total);
364
 
        MP4SetMetadataTrack(mp4file, track, track_total);
 
361
        //if (FileTag->track_total)
 
362
        //    track_total = atoi(FileTag->track_total);
 
363
        taglib_tag_set_track(tag, track);
365
364
    }else
366
365
    {
367
366
        //MP4DeleteMetadataTrack(mp4file);
368
 
        MP4SetMetadataTrack(mp4file, 0, 0);
 
367
        taglib_tag_set_track(tag, 0);
369
368
    }
370
369
 
371
370
    /*********
373
372
     *********/
374
373
    if (FileTag->genre && g_utf8_strlen(FileTag->genre, -1) > 0 )
375
374
    {
376
 
        MP4SetMetadataGenre(mp4file, FileTag->genre);
 
375
        taglib_tag_set_genre(tag, FileTag->genre);
377
376
    }else
378
377
    {
379
378
        //MP4DeleteMetadataGenre(mp4file);
380
 
        MP4SetMetadataGenre(mp4file, "");
 
379
        taglib_tag_set_genre(tag, "");
381
380
    }
382
381
 
383
382
    /***********
385
384
     ***********/
386
385
    if (FileTag->comment && g_utf8_strlen(FileTag->comment, -1) > 0)
387
386
    {
388
 
        MP4SetMetadataComment(mp4file, FileTag->comment);
 
387
        taglib_tag_set_comment(tag, FileTag->comment);
389
388
    }else
390
389
    {
391
390
        //MP4DeleteMetadataComment(mp4file);
392
 
        MP4SetMetadataComment(mp4file, "");
 
391
        taglib_tag_set_comment(tag, "");
393
392
    }
394
393
 
395
394
    /**********************
396
395
     * Composer or Writer *
397
396
     **********************/
 
397
    /*
398
398
    if (FileTag->composer && g_utf8_strlen(FileTag->composer, -1) > 0)
399
399
    {
400
400
        MP4SetMetadataWriter(mp4file, FileTag->composer);
403
403
        //MP4DeleteMetadataWriter(mp4file);
404
404
        MP4SetMetadataWriter(mp4file, "");
405
405
    }
 
406
    */
406
407
 
407
408
    /*****************
408
409
     * Encoding Tool *
409
410
     *****************/
 
411
    /*
410
412
    if (FileTag->encoded_by && g_utf8_strlen(FileTag->encoded_by, -1) > 0)
411
413
    {
412
414
        MP4SetMetadataTool(mp4file, FileTag->encoded_by);
415
417
        //MP4DeleteMetadataTool(mp4file);
416
418
        MP4SetMetadataTool(mp4file, "");
417
419
    }
 
420
    */
418
421
 
419
422
    /***********
420
423
     * Picture *
421
424
     ***********/
 
425
    /*
422
426
    {
423
427
        // Can handle only one picture...
424
428
        Picture *pic;
433
437
            }
434
438
        }
435
439
    }
436
 
 
437
 
 
438
 
    MP4Close(mp4file);
 
440
    */
 
441
 
 
442
    taglib_file_save(mp4file);
 
443
    taglib_file_free(mp4file);
439
444
 
440
445
    if (error) return FALSE;
441
446
    else       return TRUE;