mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-03 13:44:33 -04:00
Test with a c style array.
This commit is contained in:
parent
1f5a589f96
commit
adc36d7579
@ -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) { }
|
||||
|
@ -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<Stream> *subtitleStreams = new std::vector<Stream>();
|
||||
*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();
|
||||
}
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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<Stream>();
|
||||
//int size = Marshal.SizeOf<Stream>();
|
||||
|
||||
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<Stream>(streamsPtr), StreamType.Subtitle);
|
||||
streamsPtr += size;
|
||||
}
|
||||
}
|
||||
else
|
||||
tracks = null;
|
||||
// int j = 0;
|
||||
// for (int i = 0; i < arrayLength; i++)
|
||||
// {
|
||||
// Stream stream = Marshal.PtrToStructure<Stream>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,9 @@
|
||||
|
||||
<!-- Set this to true if you enable server-side prerendering -->
|
||||
<BuildServerSideRenderer>false</BuildServerSideRenderer>
|
||||
<Company>SDG</Company>
|
||||
<Authors>Anonymus-Raccoon</Authors>
|
||||
<RepositoryUrl>https://github.com/AnonymusRaccoon/Kyoo</RepositoryUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user