mirror of
https://github.com/zoriya/Kyoo.Transcoder.git
synced 2025-12-06 06:26:11 +00:00
Adding chapter extraction
This commit is contained in:
@@ -1,39 +1,39 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
||||
set (PROJECT_VERSION "1.0")
|
||||
set(PROJECT_VERSION "1.0")
|
||||
project(transcoder VERSION ${PROJECT_VERSION})
|
||||
|
||||
include_directories(ffmpeg)
|
||||
include_directories(include)
|
||||
|
||||
add_library(transcoder SHARED
|
||||
src/helper.c
|
||||
src/info.c
|
||||
src/transmuxer.c
|
||||
src/path_helper.c
|
||||
src/destroyer.c
|
||||
src/subtitles.c)
|
||||
add_library(transcoder SHARED
|
||||
src/helper.c
|
||||
src/info.c
|
||||
src/extractor.c
|
||||
src/transmuxer.c
|
||||
src/path_helper.c
|
||||
src/destroyer.c)
|
||||
set_property(TARGET transcoder PROPERTY C_STANDARD 11)
|
||||
|
||||
target_link_libraries(transcoder
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libavformat/libavformat.a
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libavcodec/libavcodec.a
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libavutil/libavutil.a
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libavdevice/libavdevice.a
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libswresample/libswresample.a)
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libavformat/libavformat.a
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libavcodec/libavcodec.a
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libavutil/libavutil.a
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libavdevice/libavdevice.a
|
||||
${CMAKE_SOURCE_DIR}/ffmpeg/libswresample/libswresample.a)
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(transcoder wsock32 ws2_32)
|
||||
target_link_libraries(transcoder Secur32 Secur32)
|
||||
target_link_libraries(transcoder Bcrypt Bcrypt)
|
||||
target_link_libraries(transcoder wsock32 ws2_32)
|
||||
target_link_libraries(transcoder Secur32 Secur32)
|
||||
target_link_libraries(transcoder Bcrypt Bcrypt)
|
||||
endif()
|
||||
|
||||
include(ExternalProject)
|
||||
ExternalProject_Add(FFMPEG
|
||||
SOURCE_DIR ${CMAKE_SOURCE_DIR}/ffmpeg
|
||||
BUILD_IN_SOURCE 1
|
||||
CONFIGURE_COMMAND ./configure --pkg-config-flags=--static --disable-shared --enable-static --disable-zlib --disable-iconv --disable-asm --disable-ffplay --disable-ffprobe
|
||||
BUILD_COMMAND make
|
||||
INSTALL_COMMAND cmake -E echo "Skipping install step."
|
||||
SOURCE_DIR ${CMAKE_SOURCE_DIR}/ffmpeg
|
||||
BUILD_IN_SOURCE 1
|
||||
CONFIGURE_COMMAND ./configure --pkg-config-flags=--static --disable-shared --enable-static --disable-zlib --disable-iconv --disable-asm --disable-ffplay --disable-ffprobe
|
||||
BUILD_COMMAND make
|
||||
INSTALL_COMMAND cmake -E echo "Skipping install step."
|
||||
)
|
||||
|
||||
@@ -32,5 +32,5 @@ void extract_track(stream *track,
|
||||
AVStream *stream,
|
||||
AVFormatContext *in_ctx,
|
||||
AVFormatContext **out_ctx);
|
||||
|
||||
void extract_font(stream *font, const char *out_path, AVStream *stream);
|
||||
void extract_font(stream *font, const char *out_path, AVStream *stream);
|
||||
void extract_chapters(AVFormatContext *ctx, const char *out_path);
|
||||
@@ -14,15 +14,24 @@
|
||||
// @return -2 on error, -1 if track has alreaady been extracted, 0 on success.
|
||||
int create_out_path(stream *track, const char *out_path)
|
||||
{
|
||||
char *extension = get_extension_from_codec(track->codec);
|
||||
char *folder_path;
|
||||
char *file_name;
|
||||
char *tmp;
|
||||
|
||||
if (!extension)
|
||||
asprintf(&folder_path, "%s/Subtitles/%s", out_path, track->language);
|
||||
if (!folder_path)
|
||||
return -2;
|
||||
file_name = path_getfilename(track->path);
|
||||
asprintf(&folder_path, "%s/%s", out_path, track->language);
|
||||
if (path_mkdir(folder_path, 0733) < 0) {
|
||||
tmp = strrchr(folder_path, '/');
|
||||
*tmp = '\0';
|
||||
if (path_mkdir(folder_path, 0733) < 0)
|
||||
return free(folder_path), -2;
|
||||
*tmp = '/';
|
||||
if (path_mkdir(folder_path, 0733) < 0)
|
||||
return free(folder_path), -2;
|
||||
|
||||
char *extension = get_extension_from_codec(track->codec);
|
||||
char *file_name = path_getfilename(track->path);
|
||||
|
||||
if (!extension || !file_name) {
|
||||
free(folder_path);
|
||||
free(file_name);
|
||||
return -2;
|
||||
@@ -80,11 +89,11 @@ void extract_font(stream *font, const char *out_path, AVStream *stream)
|
||||
if (!filename)
|
||||
return;
|
||||
free(font->path);
|
||||
font->path = malloc((strlen(out_path) + 8 + strlen(filename->value)) * sizeof(char));
|
||||
font->path = malloc((strlen(out_path) + 18 + strlen(filename->value)) * sizeof(char));
|
||||
if (!font->path)
|
||||
return;
|
||||
strcpy(font->path, out_path);
|
||||
strcat(font->path, "/fonts/");
|
||||
strcat(font->path, "/Subtitles/fonts/");
|
||||
if (path_mkdir(font->path, 0733) < 0)
|
||||
return free(font->path);
|
||||
strcat(font->path, filename->value);
|
||||
@@ -97,4 +106,39 @@ void extract_font(stream *font, const char *out_path, AVStream *stream)
|
||||
return perror("Kyoo couldn't extract a subtitle's font");
|
||||
write(fd, stream->codecpar->extradata, stream->codecpar->extradata_size);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void extract_chapters(AVFormatContext *ctx, const char *out_path)
|
||||
{
|
||||
const char *filename = strrchr(ctx->url, '/');
|
||||
char *path = malloc((strlen(filename) + strlen(out_path) + 14) * sizeof(char));
|
||||
char *tmp;
|
||||
|
||||
if (!path)
|
||||
return;
|
||||
strcpy(path, out_path);
|
||||
strcat(path, "/Chapters/");
|
||||
if (path_mkdir(path, 0733) < 0)
|
||||
return;
|
||||
strcat(path, filename);
|
||||
tmp = strrchr(path, '.');
|
||||
if (tmp)
|
||||
*tmp = '\0';
|
||||
strcat(path, ".txt");
|
||||
|
||||
int fd = open(path, O_WRONLY | O_CREAT, 0744);
|
||||
|
||||
for (unsigned i = 0; i < ctx->nb_chapters; i++) {
|
||||
AVDictionaryEntry *name = av_dict_get(ctx->chapters[i]->metadata, "title", NULL, 0);
|
||||
if (!name)
|
||||
continue;
|
||||
const AVChapter *chapter = ctx->chapters[i];
|
||||
if (chapter->start == AV_NOPTS_VALUE || chapter->end == AV_NOPTS_VALUE)
|
||||
continue;
|
||||
double start = chapter->start * av_q2d(chapter->time_base);
|
||||
double end = chapter->end * av_q2d(chapter->time_base);
|
||||
dprintf(fd, "%f-%f: %s\n", start, end, name->value);
|
||||
}
|
||||
close(fd);
|
||||
free(path);
|
||||
}
|
||||
@@ -83,6 +83,7 @@ stream *extract_infos(const char *path, const char *out_path, unsigned *stream_c
|
||||
output_list = calloc(ctx->nb_streams, sizeof(AVFormatContext *));
|
||||
|
||||
if (output_list && streams) {
|
||||
extract_chapters(ctx, out_path);
|
||||
for (unsigned i = 0; i < *stream_count; i++) {
|
||||
AVStream *stream = ctx->streams[i];
|
||||
type stream_type = type_fromffmpeg(stream);
|
||||
|
||||
Reference in New Issue
Block a user