83 lines
3.1 KiB
C#
83 lines
3.1 KiB
C#
|
|
using System.Diagnostics;
|
|
using System.Text.Json.Serialization;
|
|
using Tesses.WebServer;
|
|
using YoutubeExplode.Videos;
|
|
|
|
namespace Tesses.YouTubeDownloader2025Companion;
|
|
|
|
public class MediaEntry {
|
|
public string AlbumArtist {get;set;}="";
|
|
|
|
public string AlbumName {get;set;}="";
|
|
|
|
|
|
public string SongName {get;set;} = "";
|
|
|
|
public string AlbumId {get;set;}="";
|
|
|
|
public List<string> ContributingArtists {get;set;}=new List<string>();
|
|
|
|
public string ContributingArtistsString => string.Join("; ", ContributingArtists.Select(e=>e.Replace(";","")));
|
|
|
|
public int TrackIndex {get;set;}
|
|
|
|
public override string ToString()
|
|
{
|
|
var trackIndex = TrackIndex.ToString("D2");
|
|
return Path.Combine("data","Music",ServerContext.FixFileName(AlbumArtist),ServerContext.FixFileName(AlbumName),$"{trackIndex} {ServerContext.FixFileName(SongName)}.mp3");
|
|
}
|
|
|
|
public async Task<bool> ImportAsync(string srcPath)
|
|
{
|
|
string dest = ToString();
|
|
if(File.Exists(dest)) return true;
|
|
using Process p = new Process();
|
|
p.StartInfo.FileName = "ffmpeg";
|
|
p.StartInfo.UseShellExecute = false;
|
|
p.StartInfo.CreateNoWindow=true;
|
|
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
p.StartInfo.ArgumentList.Add("-y");
|
|
p.StartInfo.ArgumentList.Add("-i");
|
|
p.StartInfo.ArgumentList.Add(srcPath);
|
|
p.StartInfo.ArgumentList.Add("-i");
|
|
p.StartInfo.ArgumentList.Add($"data/Thumbnails/Albums/{AlbumId}.jpg");
|
|
p.StartInfo.ArgumentList.Add("-metadata");
|
|
p.StartInfo.ArgumentList.Add($"artist={ContributingArtistsString}");
|
|
p.StartInfo.ArgumentList.Add("-metadata");
|
|
p.StartInfo.ArgumentList.Add($"album={AlbumName}");
|
|
p.StartInfo.ArgumentList.Add("-metadata");
|
|
p.StartInfo.ArgumentList.Add($"album_artist={AlbumArtist}");
|
|
p.StartInfo.ArgumentList.Add("-metadata");
|
|
p.StartInfo.ArgumentList.Add($"title={SongName}");
|
|
p.StartInfo.ArgumentList.Add("-metadata");
|
|
p.StartInfo.ArgumentList.Add($"track={TrackIndex}");
|
|
p.StartInfo.ArgumentList.Add("-c:v");
|
|
p.StartInfo.ArgumentList.Add("copy");
|
|
p.StartInfo.ArgumentList.Add("-map");
|
|
p.StartInfo.ArgumentList.Add("0:a");
|
|
p.StartInfo.ArgumentList.Add("-map");
|
|
p.StartInfo.ArgumentList.Add("1:v");
|
|
p.StartInfo.ArgumentList.Add("-id3v2_version");
|
|
p.StartInfo.ArgumentList.Add("3");
|
|
p.StartInfo.ArgumentList.Add("-metadata:s:v");
|
|
p.StartInfo.ArgumentList.Add("title=Album cover");
|
|
p.StartInfo.ArgumentList.Add("-metadata:s:v");
|
|
p.StartInfo.ArgumentList.Add("comment=Cover (Front)");
|
|
|
|
p.StartInfo.ArgumentList.Add("data/Temp/conv.mp3");
|
|
if(p.Start())
|
|
{
|
|
await p.WaitForExitAsync();
|
|
if(p.ExitCode == 0)
|
|
{
|
|
string? dir=Path.GetDirectoryName(dest);
|
|
if(!string.IsNullOrWhiteSpace(dir))
|
|
Directory.CreateDirectory(dir);
|
|
File.Move("data/Temp/conv.mp3",dest);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
} |