From c80b4d8fd1f69d7cb0957bfb46fa157abdf442b0 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 29 Dec 2019 05:03:15 +0100 Subject: [PATCH] Cleaning up the info prober --- CMakeLists.txt | 2 +- include/helper.h | 4 +++- include/stream.h | 12 +++++++++++- include/transcoder.h | 6 +++--- src/destroyer.c | 25 +++++++++++++++++++++++++ src/helper.c | 28 ++++++++++++---------------- src/info.c | 12 ++++++------ src/subtitles.c | 3 ++- tests/test_main.c | 12 +++++++++--- 9 files changed, 72 insertions(+), 32 deletions(-) create mode 100644 src/destroyer.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 5261463..f0d3ff3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ include_directories(include) add_library(transcoder SHARED src/helper.c src/subtitles.c - src/info.c src/path_helper.c) + src/info.c src/path_helper.c src/destroyer.c) set_property(TARGET transcoder PROPERTY C_STANDARD 11) target_link_libraries(transcoder diff --git a/include/helper.h b/include/helper.h index 719c8bf..378cbc2 100644 --- a/include/helper.h +++ b/include/helper.h @@ -7,8 +7,10 @@ #include "libavformat/avformat.h" #include "libavutil/dict.h" #include "libavutil/timestamp.h" +#include "stream.h" int open_input_context(AVFormatContext **inputContext, const char *path); AVStream *copy_stream_to_output(AVFormatContext *out_ctx, AVStream *in_stream); int open_output_file_for_write(AVFormatContext *out_ctx, const char *out_path, AVDictionary **options); -void process_packet(AVPacket *pkt, AVStream *in_stream, AVStream *out_stream); \ No newline at end of file +void process_packet(AVPacket *pkt, AVStream *in_stream, AVStream *out_stream); +type type_fromffmpeg(int type); \ No newline at end of file diff --git a/include/stream.h b/include/stream.h index 1ed712a..a5ab217 100644 --- a/include/stream.h +++ b/include/stream.h @@ -2,6 +2,14 @@ #include #include +typedef enum +{ + none = 0, + video = 1, + audio = 2, + subtitle = 3 +} type; + typedef struct stream { char *title; @@ -10,6 +18,7 @@ typedef struct stream bool is_default; bool is_forced; char *path; + type type; } stream; #define NULLSTREAM (struct stream) { \ @@ -18,5 +27,6 @@ typedef struct stream NULL, \ false, \ false, \ - NULL \ + NULL, \ + none \ } diff --git a/include/transcoder.h b/include/transcoder.h index c96e887..c7771c8 100644 --- a/include/transcoder.h +++ b/include/transcoder.h @@ -3,10 +3,10 @@ #include "stream.h" //API int transmux(const char *path, const char *out_path, float *playable_duration); -// + //API int transcode(const char *path, const char *out_path, float *playable_duration); -// -//API stream *get_track_info(const char *path, int *stream_count, int *track_count); + +API stream *get_track_info(const char *path, unsigned *stream_count, unsigned *track_count); //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. API stream *extract_subtitles(char *path, const char *out_path, unsigned *stream_count, unsigned *subtitle_count); diff --git a/src/destroyer.c b/src/destroyer.c new file mode 100644 index 0000000..39ce9c7 --- /dev/null +++ b/src/destroyer.c @@ -0,0 +1,25 @@ +// +// Created by anonymus-raccoon on 12/29/19. +// + +#include "stream.h" +#include + +void destroy_stream(stream *s) +{ + if (s->title) + free(s->title); + if (s->language) + free(s->language); + if (s->codec) + free(s->codec); + if (s->path) + free(s->path); +} + +void free_streams(stream *s, int count) +{ + for (unsigned i = 0; i < count; i++) + destroy_stream(s + i); + free(s); +} \ No newline at end of file diff --git a/src/helper.c b/src/helper.c index f6ddf62..61f2e8a 100644 --- a/src/helper.c +++ b/src/helper.c @@ -64,21 +64,17 @@ void process_packet(AVPacket *pkt, AVStream *in_stream, AVStream *out_stream) pkt->pos = -1; } -void destroy_stream(stream *s) +type type_fromffmpeg(int type) { - if (s->title) - free(s->title); - if (s->language) - free(s->language); - if (s->codec) - free(s->codec); - if (s->path) - free(s->path); -} - -void free_streams(stream *s, int count) -{ - for (unsigned i = 0; i < count; i++) - destroy_stream(s + i); - free(s); + switch (type) + { + case AVMEDIA_TYPE_VIDEO: + return video; + case AVMEDIA_TYPE_AUDIO: + return audio; + case AVMEDIA_TYPE_SUBTITLE: + return subtitle; + default: + return none; + } } \ No newline at end of file diff --git a/src/info.c b/src/info.c index 414946b..592003e 100644 --- a/src/info.c +++ b/src/info.c @@ -12,7 +12,7 @@ int init() return (42); } -stream *get_track_info(const char *path, int *stream_count, int *track_count) +stream *get_track_info(const char *path, unsigned *stream_count, unsigned *track_count) { AVFormatContext *ctx = NULL; stream *streams; @@ -22,22 +22,22 @@ stream *get_track_info(const char *path, int *stream_count, int *track_count) *stream_count = ctx->nb_streams; *track_count = 0; streams = malloc(sizeof(stream) * *stream_count); - - for (int i = 0; i < *stream_count; i++) { + for (unsigned i = 0; i < *stream_count; i++) { AVStream *stream = ctx->streams[i]; const AVCodecParameters *codecpar = stream->codecpar; - if (codecpar->codec_type == AVMEDIA_TYPE_VIDEO || codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { + if (type_fromffmpeg(codecpar->codec_type) != none) { AVDictionaryEntry *languageptr = av_dict_get(stream->metadata, "language", NULL, 0); *track_count += 1; streams[i] = (struct stream){ - codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? "VIDEO" : NULL, + NULL, languageptr ? strdup(languageptr->value) : NULL, strdup(avcodec_get_name(codecpar->codec_id)), stream->disposition & AV_DISPOSITION_DEFAULT, stream->disposition & AV_DISPOSITION_FORCED, - strdup(path) + strdup(path), + type_fromffmpeg(codecpar->codec_type) }; } else diff --git a/src/subtitles.c b/src/subtitles.c index 3db0973..8a8b781 100644 --- a/src/subtitles.c +++ b/src/subtitles.c @@ -27,7 +27,8 @@ int get_subtitle_data(stream *substream, AVStream *in_stream, const char *file_n codec, in_stream->disposition & AV_DISPOSITION_DEFAULT, in_stream->disposition & AV_DISPOSITION_FORCED, - NULL + NULL, + subtitle }; asprintf(&folder_path, "%s/%s", out_path, substream->language); if (!folder_path) diff --git a/tests/test_main.c b/tests/test_main.c index 656697a..5d5625d 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -14,11 +14,17 @@ int main(int argc, char **argv) unsigned subtitle_count = 0; stream *streams; - if (argc == 3 && !strcmp(argv[1], "subextract")) { + if (argc == 3 && !strcmp(argv[1], "info")) { + streams = get_track_info(argv[2], &stream_count, &subtitle_count); + free_streams(streams, stream_count); + } + else if (argc == 3 && !strcmp(argv[1], "subextract")) { streams = extract_subtitles(argv[2], ".", &stream_count, &subtitle_count); free_streams(streams, stream_count); - } else + } + else printf("\nUsage:\n\n\ - %s subextr video_path - Test subtitle extractions\n", argv[0]); + %s INFO video_path - Test info prober\n\ + %s subextr video_path - Test subtitle extractions\n", argv[0], argv[0]); return (0); } \ No newline at end of file