Skip to content

Commit

Permalink
Fix not working with web link
Browse files Browse the repository at this point in the history
Change media type to be inferred by lua script, small changes to protocol
  • Loading branch information
datasone committed Mar 12, 2022
1 parent c837f5d commit d6b2621
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 103 deletions.
39 changes: 4 additions & 35 deletions MPVMediaControl/MediaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,7 @@ public class MCMediaFile
public string Artist;
public string Path;
public string ShotPath;

private static readonly string[] AudioFormats =
{
"m4a", "wma", "aac", "adt", "adts", "mp3", "wav", "ac3", "ec3", "flac", "ape", "tta", "tak", "ogg",
"opus",
};

private static readonly string[] VideoFormats =
{
"3g2", "3gp2", "3gp", "3gpp", "m4v", "mp4v", "mp4", "mov", "m2ts", "asf", "wmv", "avi", "mkv",
};

private static readonly string[] ImageFormats =
{
"tif", "tiff", "png", "jpg", "gif"
};

public MediaPlaybackType Type()
{
var fileName = Path.Split('\\').Last();
if (fileName.Split('.').Length == 1)
return MediaPlaybackType.Unknown;

var extName = fileName.Split('.').Last();
if (AudioFormats.Contains(extName))
return MediaPlaybackType.Music;
if (VideoFormats.Contains(extName))
return MediaPlaybackType.Video;
if (ImageFormats.Contains(extName))
return MediaPlaybackType.Image;

return MediaPlaybackType.Unknown;
}
public MediaPlaybackType Type;

public bool ThumbnailObtained = false;

Expand Down Expand Up @@ -101,12 +69,13 @@ public MCMediaFile File
_file.ThumbnailObtained = false;
_file.Path = value.Path;
_file.ShotPath = value.ShotPath.Replace('/', '\\');
_file.Type = value.Type;

_updater.ClearAll();

_updater.Type = _file.Type();
_updater.Type = _file.Type;

switch (_file.Type())
switch (_file.Type)
{
case MediaPlaybackType.Image:
_updater.ImageProperties.Title = _file.Title;
Expand Down
97 changes: 31 additions & 66 deletions MPVMediaControl/PipeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Text;
using System.Threading;
using Windows.Media;

namespace MPVMediaControl
{
Expand Down Expand Up @@ -86,48 +87,6 @@ private static void ServerThread(object data)
}
}

private static string ParseEDL(string edlFilePath)
{
// Only select first file as syncing play time to decide file is too much for thumbnail generation.
var content = File.ReadAllLines(edlFilePath);
foreach (var line in content)
{
if (line.StartsWith("#")) continue;
if (line.StartsWith("%"))
{
var byteLenStr = line.Split('%')[1];
var success = int.TryParse(byteLenStr, out var byteLength);
if (!success)
continue;
var restPart = line.Substring(2 + byteLenStr.Length);
var restPartInBytes = Encoding.UTF8.GetBytes(restPart);
var fileNameInBytes = restPartInBytes.Take(byteLength).ToArray();
return Encoding.UTF8.GetString(fileNameInBytes);
}
else
{
return line.Split(',')[0];
}
}

return "";
}

private static string ParseCUE(string cueFilePath)
{
var parentPath = cueFilePath.Substring(0, cueFilePath.LastIndexOf(Path.DirectorySeparatorChar) + 1);
var content = File.ReadAllLines(cueFilePath);
foreach (var line in content)
{
if (!line.StartsWith("FILE")) continue;
var prefixLen = 6; // "FILE \""
var suffixLen = line.Split('"').Last().Length + 1;
return parentPath + line.Substring(prefixLen, line.Length - prefixLen - suffixLen);
}

return "";
}

private static string FromHexString(string hexString)
{
var bytes = new byte[hexString.Length / 2];
Expand All @@ -143,37 +102,43 @@ private static void ParseFile(MediaController controller, Dictionary<string, str
{
var title = FromHexString(parameters["title"]);
var artist = FromHexString(parameters["artist"]);
var path = nonHexPath ? parameters["path"] : FromHexString(parameters["path"]);
var shotPath = FromHexString(parameters["shot_path"]);
var path = FromHexString(parameters["path"]);
var shotPath = parameters.ContainsKey("shot_path") ? FromHexString(parameters["shot_path"]) : String.Empty;

// Using MediaPlaybackType.Unknown will cause exception, so another default value has to be set
var type = MediaPlaybackType.Music;
if (parameters.ContainsKey("type"))
{
switch (parameters["type"])
{
case "video":
type = MediaPlaybackType.Video;
break;
case "music":
type = MediaPlaybackType.Music;
break;
case "image":
type = MediaPlaybackType.Image;
break;
}
}

// Processing metadata may take some time, so only checking path isn't enough.
if (title == controller.File.Title &&
artist == controller.File.Artist &&
path == controller.File.Path)
return;

if (path.Split('.').Last() == "edl")
{
parameters["path"] = ParseEDL(path);
ParseFile(controller, parameters, true);
}
else if (path.Split('.').Last() == "cue")
{
parameters["path"] = ParseCUE(path);
ParseFile(controller, parameters, true);
}
else
var file = new MediaController.MCMediaFile
{
var file = new MediaController.MCMediaFile
{
Title = title,
Artist = artist,
Path = path,
ShotPath = shotPath.Replace('/', '\\'),
};

controller.File = file;
}
Title = title,
Artist = artist,
Path = path,
ShotPath = shotPath.Replace('/', '\\'),
Type = type,
};

controller.File = file;
}

private static void ParseCommand(string command)
Expand Down Expand Up @@ -209,7 +174,7 @@ private static void ParseCommand(string command)
switch (commandName)
{
case "setFile":
ParseFile(controller, parameters, false);
ParseFile(controller, parameters);
break;

case "setState":
Expand Down
14 changes: 12 additions & 2 deletions notify_media.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@ function save_shot(path)
end
end

function notify_current_file()
function media_type()
fps = mp.get_property_native("estimated-vf-fps")

if fps and fps > 1 then
return "video"
else
return "music"
end
end

function notify_metadata_updated()
metadata = mp.get_property_native("metadata")
debug_log(metadata)
if not metadata then
Expand Down Expand Up @@ -132,7 +142,7 @@ function notify_current_file()
shot_path = user_path .. "\\" .. pid .. ".jpg"
save_shot(shot_path)

message_content = "^[setFile](pid=" .. pid .. ")(title=" .. title .. ")(artist=" .. artist .. ")(path=" .. path .. ")$"
message_content = "^[setFile](pid=" .. pid .. ")(title=" .. title .. ")(artist=" .. artist .. ")(path=" .. path .. ")(type=" .. media_type() .. ")$"
write_to_socket(message_content)
end

Expand Down

0 comments on commit d6b2621

Please sign in to comment.