OSDN Git Service

fix: cannot preview with QT
[handbrake-jp/handbrake-jp.git] / libhb / decmetadata.c
1 /* decmetadata.c - Extract and decode metadata from the source
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.fr/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include <mp4v2/mp4v2.h>
8
9 #include "common.h"
10
11 static void decmp4metadata( hb_title_t *title )
12 {
13     MP4FileHandle input_file;
14     hb_deep_log( 2, "Got an MP4 input, read the metadata");
15
16     input_file = MP4Read( title->dvd, 0 );
17
18     if( input_file != MP4_INVALID_FILE_HANDLE )
19     { 
20         /*
21          * Store iTunes MetaData
22          */
23         const MP4Tags* tags;
24
25         /* alloc,fetch tags */
26         tags = MP4TagsAlloc();
27         MP4TagsFetch( tags, input_file );
28
29         if( tags->name ) {
30             hb_deep_log( 2, "Metadata Name in input file is '%s'", tags->name );
31             strncpy( title->metadata->name, tags->name, sizeof(title->metadata->name) );
32         }
33
34         if( tags->artist )
35             strncpy( title->metadata->artist, tags->artist, sizeof(title->metadata->artist) );
36
37         if( tags->composer )
38             strncpy( title->metadata->composer, tags->composer, sizeof(title->metadata->composer) );
39
40         if( tags->comments )
41             strncpy( title->metadata->comment, tags->comments, sizeof(title->metadata->comment) );
42
43         if( tags->releaseDate )
44             strncpy( title->metadata->release_date, tags->releaseDate, sizeof(title->metadata->release_date) );
45
46         if( tags->album )
47             strncpy( title->metadata->album, tags->album, sizeof(title->metadata->album) );
48
49         if( tags->genre )
50             strncpy( title->metadata->genre, tags->genre, sizeof(title->metadata->genre) );
51
52         if( tags->artworkCount > 0 ) {
53             const MP4TagArtwork* art = tags->artwork + 0; // first element
54             title->metadata->coverart = (uint8_t*)malloc( art->size );
55             title->metadata->coverart_size = art->size;
56             memcpy( title->metadata->coverart, art->data, art->size );
57             hb_deep_log( 2, "Got some cover art of type %d, size %d", 
58                          art->type,
59                          title->metadata->coverart_size );
60         }
61
62         /* store,free tags */
63         MP4TagsStore( tags, input_file );
64         MP4TagsFree( tags );
65         
66         /*
67          * Handle the chapters. 
68          */
69         MP4Chapter_t *chapter_list = NULL;
70         uint32_t      chapter_count;
71         
72         MP4GetChapters( input_file, &chapter_list, &chapter_count, 
73                         MP4ChapterTypeQt );
74
75         if( chapter_list && ( hb_list_count( title->list_chapter ) == 0 ) ) {
76             uint32_t i = 1;
77             while( i <= chapter_count )
78             {
79                 hb_chapter_t * chapter;
80                 chapter = calloc( sizeof( hb_chapter_t ), 1 );
81                 chapter->index = i;
82                 chapter->duration = chapter_list[i-1].duration * 90;
83                 chapter->hours    = chapter->duration / 90000 / 3600;
84                 chapter->minutes  = ( ( chapter->duration / 90000 ) % 3600 ) / 60;
85                 chapter->seconds  = ( chapter->duration / 90000 ) % 60;
86                 strcpy( chapter->title, chapter_list[i-1].title );
87                 hb_deep_log( 2, "Added chapter %i, name='%s', dur=%"PRId64", (%02i:%02i:%02i)", chapter->index, chapter->title, 
88                        chapter->duration, chapter->hours, 
89                        chapter->minutes, chapter->seconds);
90                 hb_list_add( title->list_chapter, chapter );
91                 i++;
92             }
93         }
94
95         MP4Close( input_file );
96     }
97 }
98
99 /*
100  * decmetadata()
101  *
102  * Look at the title and extract whatever metadata we can from that title.
103  */
104 void decmetadata( hb_title_t *title )
105 {
106     if( !title ) 
107     {
108         return;
109     }
110     
111     if( title->metadata )
112     {
113         free( title->metadata );
114         title->metadata = NULL;
115     }
116
117     title->metadata = calloc( sizeof(hb_metadata_t), 1);
118
119     if( !title->metadata )
120     {
121         return;
122     }
123
124     /*
125      * Hacky way of figuring out if this is an MP4, in which case read the data using libmp4v2
126      */
127     if( title->container_name && strcmp(title->container_name, "mov,mp4,m4a,3gp,3g2,mj2") == 0 ) 
128     {
129         decmp4metadata( title );
130     } else {
131         free( title->metadata );
132         title->metadata = NULL;
133     }
134 }