00001
00002
00003 #include "metaioavfcomment.h"
00004 #include "metadata.h"
00005
00006
00007 #include <mythcontext.h>
00008
00009 extern "C" {
00010 #include <libavformat/avformat.h>
00011 #include <libavcodec/avcodec.h>
00012 }
00013
00014 MetaIOAVFComment::MetaIOAVFComment(void)
00015 : MetaIO()
00016 {
00017 QMutexLocker locker(avcodeclock);
00018 av_register_all();
00019 }
00020
00021 MetaIOAVFComment::~MetaIOAVFComment(void)
00022 {
00023 }
00024
00028 bool MetaIOAVFComment::write(const Metadata* mdata)
00029 {
00030
00031 (void)mdata;
00032 return false;
00033 }
00034
00038 Metadata* MetaIOAVFComment::read(const QString &filename)
00039 {
00040 QString artist, compilation_artist, album, title, genre;
00041 int year = 0, tracknum = 0, length = 0;
00042
00043 AVFormatContext* p_context = NULL;
00044 AVInputFormat* p_inputformat = NULL;
00045
00046 QByteArray local8bit = filename.toLocal8Bit();
00047 if ((avformat_open_input(&p_context, local8bit.constData(),
00048 p_inputformat, NULL) < 0))
00049 {
00050 return NULL;
00051 }
00052
00053 if (avformat_find_stream_info(p_context, NULL) < 0)
00054 return NULL;
00055
00056 AVDictionaryEntry *tag = av_dict_get(p_context->metadata, "title", NULL, 0);
00057 if (!tag)
00058 {
00059 readFromFilename(filename, artist, album, title, genre, tracknum);
00060 }
00061 else
00062 {
00063 title = (char *)tag->value;
00064
00065 tag = av_dict_get(p_context->metadata, "author", NULL, 0);
00066 if (tag)
00067 artist += (char *)tag->value;
00068
00069
00070 tag = av_dict_get(p_context->metadata, "album", NULL, 0);
00071 if (tag)
00072 album += (char *)tag->value;
00073
00074 tag = av_dict_get(p_context->metadata, "genre", NULL, 0);
00075 if (tag)
00076 genre += (char *)tag->value;
00077
00078 tag = av_dict_get(p_context->metadata, "year", NULL, 0);
00079 if (tag)
00080 year = atoi(tag->value);
00081
00082 tag = av_dict_get(p_context->metadata, "tracknum", NULL, 0);
00083 if (tag)
00084 tracknum = atoi(tag->value);
00085 }
00086
00087 length = getTrackLength(p_context);
00088
00089 Metadata *retdata = new Metadata(filename, artist, compilation_artist, album,
00090 title, genre, year, tracknum, length);
00091
00092 retdata->determineIfCompilation();
00093
00094 avformat_close_input(&p_context);
00095
00096 return retdata;
00097 }
00098
00105 int MetaIOAVFComment::getTrackLength(const QString &filename)
00106 {
00107 AVFormatContext* p_context = NULL;
00108 AVInputFormat* p_inputformat = NULL;
00109
00110
00111 QByteArray local8bit = filename.toLocal8Bit();
00112 if ((avformat_open_input(&p_context, local8bit.constData(),
00113 p_inputformat, NULL) < 0))
00114 {
00115 return 0;
00116 }
00117
00118 if (avformat_find_stream_info(p_context, NULL) < 0)
00119 return 0;
00120
00121 int rv = getTrackLength(p_context);
00122
00123 avformat_close_input(&p_context);
00124
00125 return rv;
00126 }
00127
00134 int MetaIOAVFComment::getTrackLength(AVFormatContext* pContext)
00135 {
00136 if (!pContext)
00137 return 0;
00138
00139 av_estimate_timings(pContext, 0);
00140
00141 return (pContext->duration / AV_TIME_BASE) * 1000;
00142 }