Implemented #56
This commit is contained in:
@@ -54,59 +54,18 @@ namespace Icarus.Controllers.Managers
|
|||||||
|
|
||||||
public CoverArt SaveCoverArt(Song song)
|
public CoverArt SaveCoverArt(Song song)
|
||||||
{
|
{
|
||||||
try
|
var sng = SongManager.ConvertSongToSng(song);
|
||||||
{
|
|
||||||
// TODO: Change logic so it attempts to create the directory
|
|
||||||
// after it has been determined that the song does not have
|
|
||||||
// a cover art image
|
|
||||||
|
|
||||||
var strCount = _rootCoverArtPath.Length + song.Artist.Length +
|
var cov = ConvertCoverArtToCovArt(new CoverArt
|
||||||
song.AlbumTitle.Length + 2;
|
|
||||||
var imgPath = new StringBuilder(strCount);
|
|
||||||
|
|
||||||
DirectoryManager.create_directory(SongManager.ConvertSongToSng(song),
|
|
||||||
_rootCoverArtPath, imgPath);
|
|
||||||
|
|
||||||
var imagePath = imgPath.ToString().Substring(0, strCount) +
|
|
||||||
song.Title + ".png";
|
|
||||||
var coverArt = new CoverArt
|
|
||||||
{
|
{
|
||||||
SongTitle = song.Title,
|
SongTitle = song.Title,
|
||||||
ImagePath = imagePath
|
ImagePath = _rootCoverArtPath
|
||||||
};
|
});
|
||||||
|
|
||||||
var cov = ConvertCoverArtToCovArt(coverArt);
|
MetadataRetriever.update_cover_art(ref cov,
|
||||||
var stock_path = _rootCoverArtPath + "CoverArt.png";
|
ref sng, _rootCoverArtPath);
|
||||||
|
|
||||||
//var metaData = new MetadataRetriever();
|
return ConvertCovArtToCoverArt(cov);
|
||||||
// TODO: Work on the function on the c++ side
|
|
||||||
// I left some TODO's
|
|
||||||
MetadataRetriever.update_cover_art(ref cov, stock_path, song.SongPath);
|
|
||||||
/**
|
|
||||||
var imgBytes = metaData.RetrieveCoverArtBytes(song);
|
|
||||||
|
|
||||||
if (imgBytes != null)
|
|
||||||
{
|
|
||||||
_logger.Info("Saving cover art to the filesystem");
|
|
||||||
File.WriteAllBytes(coverArt.ImagePath, imgBytes);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.Info("Song has no cover art, applying stock cover art");
|
|
||||||
coverArt.ImagePath = _rootCoverArtPath + "CoverArt.png";
|
|
||||||
metaData.UpdateCoverArt(song, coverArt);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
return coverArt;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CovArt ConvertCoverArtToCovArt(CoverArt cover)
|
public static CovArt ConvertCoverArtToCovArt(CoverArt cover)
|
||||||
|
|||||||
@@ -58,54 +58,17 @@ namespace Icarus.Controllers.Utilities
|
|||||||
|
|
||||||
#region C++ Libs
|
#region C++ Libs
|
||||||
[DllImport("libicarus.so")]
|
[DllImport("libicarus.so")]
|
||||||
public static extern void retrieve_metadata(ref Sng sng, string file_path);
|
public static extern void retrieve_metadata(ref Sng sng,
|
||||||
|
string file_path);
|
||||||
|
|
||||||
[DllImport("libicarus.so")]
|
[DllImport("libicarus.so")]
|
||||||
public static extern void update_metadata(ref Sng sng_updated, ref Sng sng_old);
|
public static extern void update_metadata(ref Sng sng_updated,
|
||||||
|
ref Sng sng_old);
|
||||||
|
|
||||||
[DllImport("libicarus.so")]
|
[DllImport("libicarus.so")]
|
||||||
public static extern void update_cover_art(ref CovArt cov, string stock_cover_path, string song_path);
|
public static extern void update_cover_art(ref CovArt cov,
|
||||||
|
ref Sng sng, string root_cover_path);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
// TODO: Remove this when the update_cover_art(..) has been implemented
|
|
||||||
/**
|
|
||||||
public byte[] RetrieveCoverArtBytes(Song song)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Console.WriteLine("Fetching image");
|
|
||||||
var tag = TagLib.File.Create(song.SongPath);
|
|
||||||
byte[] imgBytes = tag.Tag.Pictures[0].Data.Data;
|
|
||||||
|
|
||||||
return imgBytes;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var msg = ex.Message;
|
|
||||||
_logger.Error(msg, "An error occurred in MetadataRetriever");
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// TODO: Remove this when the update_cover_art(..) has been implemented
|
|
||||||
public void UpdateCoverArt(Song song, CoverArt coverArt)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Updating song's cover art");
|
|
||||||
|
|
||||||
var tag = TagLib.File.Create(song.SongPath);
|
|
||||||
var pics = tag.Tag.Pictures;
|
|
||||||
Array.Resize(ref pics, 1);
|
|
||||||
|
|
||||||
pics[0] = new Picture(coverArt.ImagePath)
|
|
||||||
{
|
|
||||||
Description = "Cover Art"
|
|
||||||
};
|
|
||||||
|
|
||||||
tag.Tag.Pictures = pics;
|
|
||||||
tag.Save();
|
|
||||||
}
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#ifndef MODELS_H_
|
||||||
|
#define MODELS_H_
|
||||||
|
|
||||||
struct Song
|
struct Song
|
||||||
{
|
{
|
||||||
@@ -17,3 +19,5 @@ struct Cover
|
|||||||
char SongTitle[1024];
|
char SongTitle[1024];
|
||||||
char ImagePath[1024];
|
char ImagePath[1024];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <taglib/tag.h>
|
#include <taglib/attachedpictureframe.h>
|
||||||
#include <taglib/fileref.h>
|
#include <taglib/fileref.h>
|
||||||
|
#include <taglib/mpegfile.h>
|
||||||
|
#include <taglib/tag.h>
|
||||||
|
|
||||||
|
#include "directory_manager.h"
|
||||||
#include "metadata_retriever.h"
|
#include "metadata_retriever.h"
|
||||||
#include "imageFile.h"
|
#include "imageFile.h"
|
||||||
|
|
||||||
@@ -50,19 +56,42 @@ void update_metadata(Song *sng_updated, Song *sng_old)
|
|||||||
|
|
||||||
file.save();
|
file.save();
|
||||||
}
|
}
|
||||||
void update_cover_art(Cover *cov, const char *stock_path, const char *song_path)
|
void update_cover_art(Cover *cov, Song *song, const char *root_path)
|
||||||
{
|
{
|
||||||
const std::string img_frame = "APIC";
|
TagLib::MPEG::File sngF(song->SongPath);
|
||||||
TagLib::MPEG::File sngF(song_path);
|
|
||||||
TagLib::ID3v2::Tag *tag = sngF.ID3v2Tag();
|
TagLib::ID3v2::Tag *tag = sngF.ID3v2Tag();
|
||||||
auto frameList = tag->frameListMap()[img_frame.c_str()];
|
auto frameList = tag->frameListMap()["APIC"];
|
||||||
|
|
||||||
if (frameList.isEmpty()) {
|
if (frameList.isEmpty()) {
|
||||||
// TODO: Apply stock cover art
|
std::string stock_path{root_path};
|
||||||
imageFile stock_img(stock_path);
|
stock_path.append("CoverArt.png");
|
||||||
|
strcpy(cov->ImagePath, stock_path.c_str());
|
||||||
|
|
||||||
|
imageFile stock_img(stock_path.c_str());
|
||||||
|
|
||||||
|
TagLib::ID3v2::AttachedPictureFrame *pic =
|
||||||
|
new TagLib::ID3v2::AttachedPictureFrame;
|
||||||
|
pic->setPicture(stock_img.data());
|
||||||
|
pic->setType(TagLib::ID3v2::AttachedPictureFrame::FrontCover);
|
||||||
|
|
||||||
|
tag->addFrame(pic);
|
||||||
|
|
||||||
|
sngF.save();
|
||||||
|
std::cout<<"applied stock cover art"<<std::endl;
|
||||||
} else {
|
} else {
|
||||||
// TODO: Extract cover art from song and store it in
|
auto frame = dynamic_cast<TagLib::ID3v2::AttachedPictureFrame*>(
|
||||||
// cov->ImagePath
|
frameList.front());
|
||||||
|
auto img_path = create_directory_process(*song, root_path);
|
||||||
|
img_path.append(song->Title);
|
||||||
|
img_path.append(".png");
|
||||||
|
strcpy(cov->ImagePath, img_path.c_str());
|
||||||
|
std::cout<<cov->ImagePath<<std::endl;
|
||||||
|
|
||||||
|
std::fstream img_save(cov->ImagePath, std::ios::out |
|
||||||
|
std::ios::binary);
|
||||||
|
img_save.write(frame->picture().data(), frame->picture().size());
|
||||||
|
img_save.close();
|
||||||
|
std::cout<<"saved to "<<cov->ImagePath<<std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user