diff --git a/Kyoo.Transcoder/src/Stream.h b/Kyoo.Transcoder/src/Stream.h index 8e8f48a6..f17fe2cc 100644 --- a/Kyoo.Transcoder/src/Stream.h +++ b/Kyoo.Transcoder/src/Stream.h @@ -10,12 +10,15 @@ extern "C" struct Stream { - const char* title; - const char* language; - const char* codec; + const char *title; + const char *language; + const char *codec; bool isDefault; bool isForced; - char* path; + char *path; + + Stream() + : title(NULL), language(NULL), codec(NULL), isDefault(NULL), isForced(NULL), path(NULL) {} Stream(const char* title, const char* languageCode, const char* codec, bool isDefault, bool isForced, char* path) : title(title), language(languageCode), codec(codec), isDefault(isDefault), isForced(isForced), path(path) { } diff --git a/Kyoo.Transcoder/src/Transcoder.cpp b/Kyoo.Transcoder/src/Transcoder.cpp index be8615eb..170858f7 100644 --- a/Kyoo.Transcoder/src/Transcoder.cpp +++ b/Kyoo.Transcoder/src/Transcoder.cpp @@ -200,16 +200,18 @@ int Transmux(const char *path, const char *outPath) } -Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount) +Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount, int *subtitleCount) { AVFormatContext *inputContext = NULL; if (open_input_context(&inputContext, path) != 0) return 0; - std::vector *subtitleStreams = new std::vector(); + *streamCount = inputContext->nb_streams; + *subtitleCount = 0; + Stream *subtitleStreams = new Stream[*streamCount]; const unsigned int outputCount = inputContext->nb_streams; - AVFormatContext **outputList = new AVFormatContext*[outputCount]; + AVFormatContext **outputList = new AVFormatContext * [outputCount]; //Initialize output and set headers. for (unsigned int i = 0; i < inputContext->nb_streams; i++) @@ -255,8 +257,10 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount stream.path = _strdup(outStream.str().c_str()); - subtitleStreams->push_back(stream); - std::cout << "Stream #" << i << "(" << stream.language << "), stream type: " << inputCodecpar->codec_type << " codec: " << stream.codec << std::endl; + subtitleStreams[i] = stream; + *subtitleCount += 1; + //subtitleStreams->push_back(stream); + std::cout << "Stream #" << i << "(" << stream.language << "), stream type: " << inputCodecpar->codec_type << " codec: " << stream.codec << std::endl; AVFormatContext *outputContext = NULL; if (avformat_alloc_output_context2(&outputContext, NULL, NULL, stream.path) < 0) @@ -267,7 +271,7 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount av_dict_copy(&outputContext->metadata, inputContext->metadata, NULL); - AVStream* outputStream = copy_stream_to_output(outputContext, inputStream); + AVStream *outputStream = copy_stream_to_output(outputContext, inputStream); if (outputStream == NULL) goto end; @@ -299,7 +303,7 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount continue; AVFormatContext *outputContext = outputList[pkt.stream_index]; - if(outputContext == nullptr) + if (outputContext == nullptr) { av_packet_unref(&pkt); continue; @@ -337,6 +341,7 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount delete[] outputList; - *streamCount = subtitleStreams->size(); - return subtitleStreams->data(); + return subtitleStreams; + //*streamCount = subtitleStreams->size(); + //return subtitleStreams->data(); } \ No newline at end of file diff --git a/Kyoo.Transcoder/src/Transcoder.h b/Kyoo.Transcoder/src/Transcoder.h index 903f0af1..c2750476 100644 --- a/Kyoo.Transcoder/src/Transcoder.h +++ b/Kyoo.Transcoder/src/Transcoder.h @@ -12,6 +12,6 @@ extern "C" API int Init(); //Take the path of the file and the path of the output directory. It will return the list of subtitle streams in the streams variable. The int returned is the number of subtitles extracted. -extern "C" API Stream* ExtractSubtitles(const char* path, const char* outPath, int* streamsCount); +extern "C" API Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount, int *subtitleCount); -extern "C" API int Transmux(const char* path, const char* outPath); +extern "C" API int Transmux(const char *path, const char *outPath); diff --git a/Kyoo/InternalAPI/Crawler/Crawler.cs b/Kyoo/InternalAPI/Crawler/Crawler.cs index c0a5476e..5d65f991 100644 --- a/Kyoo/InternalAPI/Crawler/Crawler.cs +++ b/Kyoo/InternalAPI/Crawler/Crawler.cs @@ -166,32 +166,40 @@ namespace Kyoo.InternalAPI string path = Path.Combine(Path.GetDirectoryName(episode.Path), "Subtitles"); if(Directory.Exists(path)) { + bool ret = false; foreach (string sub in Directory.EnumerateFiles(path, "", SearchOption.AllDirectories)) { - string language = sub.Substring(Path.GetDirectoryName(sub).Length + Path.GetFileNameWithoutExtension(episode.Path).Length + 2, 3); - bool isDefault = sub.Contains("default"); - bool isForced = sub.Contains("forced"); + string episodeLink = Path.GetFileNameWithoutExtension(episode.Path); - string codec; - switch (Path.GetExtension(sub)) + if (sub.Contains(episodeLink)) { - case ".ass": - codec = "ass"; - break; - case ".str": - codec = "subrip"; - break; - default: - codec = null; - break; + string language = sub.Substring(Path.GetDirectoryName(sub).Length + episodeLink.Length + 2, 3); + bool isDefault = sub.Contains("default"); + bool isForced = sub.Contains("forced"); + + string codec; + switch (Path.GetExtension(sub)) + { + case ".ass": + codec = "ass"; + break; + case ".str": + codec = "subrip"; + break; + default: + codec = null; + break; + } + + + Track track = new Track(Models.Watch.StreamType.Subtitle, null, language, isDefault, isForced, codec, false, sub) { episodeID = episode.id }; + libraryManager.RegisterTrack(track); + + ret = true; } - - - Track track = new Track(Models.Watch.StreamType.Subtitle, null, language, isDefault, isForced, codec, false, sub) { episodeID = episode.id }; - libraryManager.RegisterTrack(track); } - return true; + return ret; } return false; diff --git a/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs b/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs index b222b609..656f4845 100644 --- a/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs +++ b/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs @@ -16,25 +16,33 @@ namespace Kyoo.InternalAPI.TranscoderLink public extern static int Transmux(string path, string outPath); [DllImport(TranscoderPath, CallingConvention = CallingConvention.Cdecl)] - private extern static IntPtr ExtractSubtitles(string path, string outPath, out int streams); + [return:MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStruct, SizeParamIndex = 3)] + private extern static Stream[] ExtractSubtitles(string path, string outPath, out int arrayLength/*, out int trackCount*/); public static void ExtractSubtitles(string path, string outPath, out Track[] tracks) { - int size = Marshal.SizeOf(); + //int size = Marshal.SizeOf(); - IntPtr streamsPtr = ExtractSubtitles(path, outPath, out int length); - if (length > 0) - { - tracks = new Track[length]; + Stream[] streamsPtr = ExtractSubtitles(path, outPath, out int count/*, out int trackCount*/); + tracks = null; + //if (trackCount > 0) + //{ + // tracks = new Track[trackCount]; - for (int i = 0; i < length; i++) - { - tracks[i] = Track.From(Marshal.PtrToStructure(streamsPtr), StreamType.Subtitle); - streamsPtr += size; - } - } - else - tracks = null; + // int j = 0; + // for (int i = 0; i < arrayLength; i++) + // { + // Stream stream = Marshal.PtrToStructure(streamsPtr); + // if (stream.Codec != null) //If the codec is null, the stream doesn't represent a subtitle. + // { + // tracks[j] = Track.From(stream, StreamType.Subtitle); + // j++; + // } + // streamsPtr += size; + // } + //} + //else + // tracks = null; } } } diff --git a/Kyoo/Kyoo.csproj b/Kyoo/Kyoo.csproj index 0a2a60e7..f73240a9 100644 --- a/Kyoo/Kyoo.csproj +++ b/Kyoo/Kyoo.csproj @@ -10,6 +10,9 @@ false + SDG + Anonymus-Raccoon + https://github.com/AnonymusRaccoon/Kyoo diff --git a/Kyoo/Models/Track.cs b/Kyoo/Models/Track.cs index 8627d9ce..1e95cced 100644 --- a/Kyoo/Models/Track.cs +++ b/Kyoo/Models/Track.cs @@ -62,11 +62,17 @@ namespace Kyoo.Models public static Track From(Stream stream) { + if (stream == null) + return null; + return new Track(StreamType.Unknow, stream.Title, stream.Language, stream.IsDefault, stream.IsForced, stream.Codec, false, stream.Path); } public static Track From(Stream stream, StreamType type) { + if (stream == null) + return null; + return new Track(type, stream.Title, stream.Language, stream.IsDefault, stream.IsForced, stream.Codec, false, stream.Path); }