Skip to content

Commit

Permalink
use animated webp instead of gif for motion covers
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mk committed Nov 10, 2024
1 parent c844fc6 commit 998bf57
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
22 changes: 13 additions & 9 deletions src/FMBot.AppleMusic/AppleMusicVideoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ public async Task<string> GetVideoUrlFromM3U8(string m3u8Url)
line.Trim().StartsWith("https://") && line.EndsWith(".m3u8") && line.Contains("486x486"));
}


public static async Task<Stream> ConvertM3U8ToGifAsync(string m3u8Url)
public static async Task<Stream> ConvertM3U8ToWebPAsync(string m3u8Url)
{
var gifStream = new MemoryStream();
var webpStream = new MemoryStream();

var ffmpegProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "ffmpeg",
Arguments =
$"-hwaccel auto -i \"{m3u8Url}\" -vf \"fps=10,format=rgb24,colorspace=bt709:iall=bt601-6-625:fast=1,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle\" -t 15 -f gif pipe:1",
Arguments = $"-hwaccel auto " +
$"-i \"{m3u8Url}\" " +
"-vf \"fps=fps=20\" " +
"-lossless 0 " +
"-compression_level 5 " +
"-loop 1 " +
"-f webp pipe:1",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
Expand All @@ -47,7 +51,7 @@ public static async Task<Stream> ConvertM3U8ToGifAsync(string m3u8Url)
{
ffmpegProcess.Start();

var outputTask = ffmpegProcess.StandardOutput.BaseStream.CopyToAsync(gifStream);
var outputTask = ffmpegProcess.StandardOutput.BaseStream.CopyToAsync(webpStream);

var errorBuilder = new StringBuilder();
var errorTask = Task.Run(async () =>
Expand Down Expand Up @@ -78,12 +82,12 @@ public static async Task<Stream> ConvertM3U8ToGifAsync(string m3u8Url)
$"FFmpeg failed with exit code: {ffmpegProcess.ExitCode}. Error output: {errorBuilder}");
}

gifStream.Position = 0;
return gifStream;
webpStream.Position = 0;
return webpStream;
}
catch (Exception ex)
{
Log.Error(ex, "Error during M3U8 to GIF conversion");
Log.Error(ex, "Error during M3U8 to WebP conversion");
throw;
}
finally
Expand Down
8 changes: 4 additions & 4 deletions src/FMBot.Bot/Builders/AlbumBuilders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ public async Task<ResponseModel> CoverAsync(
response.Embed.WithDescription(description.ToString());

response.Stream = image;
var extension = gifResult ? "gif" : "png";
var extension = gifResult ? "webp" : "png";
response.FileName =
$"cover-{StringExtensions.ReplaceInvalidChars($"{albumSearch.Album.ArtistName}_{albumSearch.Album.AlbumName}")}.{extension}";
response.Spoiler = safeForChannel == CensorService.CensorResult.Nsfw;
Expand All @@ -1180,14 +1180,14 @@ public async Task<ResponseModel> CoverAsync(
else
{
var cacheFilePath =
ChartService.AlbumUrlToCacheFilePath(albumSearch.Album.AlbumName, albumSearch.Album.ArtistName, ".gif");
ChartService.AlbumUrlToCacheFilePath(albumSearch.Album.AlbumName, albumSearch.Album.ArtistName, ".webp");
Stream gifStream;

if (!File.Exists(cacheFilePath))
{
var specificUrl = await this._appleMusicVideoService.GetVideoUrlFromM3U8(albumCoverUrl);
gifStream = await AppleMusicVideoService.ConvertM3U8ToGifAsync(specificUrl);
await ChartService.OverwriteCache(gifStream, cacheFilePath, SKEncodedImageFormat.Gif);
gifStream = await AppleMusicVideoService.ConvertM3U8ToWebPAsync(specificUrl);
await ChartService.OverwriteCache(gifStream, cacheFilePath, SKEncodedImageFormat.Webp);
}
else
{
Expand Down

0 comments on commit 998bf57

Please sign in to comment.