Cleaning up the info prober

This commit is contained in:
Zoe Roux
2019-12-29 05:03:15 +01:00
parent 72aea102f2
commit c80b4d8fd1
9 changed files with 72 additions and 32 deletions
+1 -1
View File
@@ -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
+3 -1
View File
@@ -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);
void process_packet(AVPacket *pkt, AVStream *in_stream, AVStream *out_stream);
type type_fromffmpeg(int type);
+11 -1
View File
@@ -2,6 +2,14 @@
#include <stdbool.h>
#include <stddef.h>
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 \
}
+3 -3
View File
@@ -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);
+25
View File
@@ -0,0 +1,25 @@
//
// Created by anonymus-raccoon on 12/29/19.
//
#include "stream.h"
#include <stdlib.h>
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);
}
+12 -16
View File
@@ -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;
}
}
+6 -6
View File
@@ -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
+2 -1
View File
@@ -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)
+9 -3
View File
@@ -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);
}