mirror of
https://github.com/zoriya/Kyoo.Transcoder.git
synced 2025-12-06 06:26:11 +00:00
Cleaning up
This commit is contained in:
@@ -8,7 +8,9 @@ API int transmux(const char *path, const char *out_path, float *playable_duratio
|
||||
|
||||
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.
|
||||
//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);
|
||||
|
||||
void destroy_stream(stream *s);
|
||||
|
||||
19
src/helper.c
19
src/helper.c
@@ -1,5 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include "helper.h"
|
||||
#include "stream.h"
|
||||
|
||||
@@ -7,13 +6,13 @@ int open_input_context(AVFormatContext **in_ctx, const char *path)
|
||||
{
|
||||
if (avformat_open_input(in_ctx, path, NULL, NULL)) {
|
||||
fprintf(stderr, "Error: Can't open the file at %s.\n", path);
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
if (avformat_find_stream_info(*in_ctx, NULL) < 0) {
|
||||
fprintf(stderr,"Error: Could't find streams informations for the file at %s.\n", path);
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
AVStream *copy_stream_to_output(AVFormatContext *out_ctx, AVStream *in_stream)
|
||||
@@ -22,11 +21,11 @@ AVStream *copy_stream_to_output(AVFormatContext *out_ctx, AVStream *in_stream)
|
||||
|
||||
if (out_stream == NULL) {
|
||||
fprintf(stderr,"Error: Couldn't create stream.\n");
|
||||
return (NULL);
|
||||
return NULL;
|
||||
}
|
||||
if (avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar) < 0) {
|
||||
fprintf(stderr, "Error: Couldn't copy parameters to the output file.\n");
|
||||
return (NULL);
|
||||
return NULL;
|
||||
}
|
||||
out_stream->codecpar->codec_tag = 0;
|
||||
avformat_transfer_internal_stream_timing_info(out_ctx->oformat, out_stream, in_stream, AVFMT_TBCF_AUTO);
|
||||
@@ -35,7 +34,7 @@ AVStream *copy_stream_to_output(AVFormatContext *out_ctx, AVStream *in_stream)
|
||||
out_stream->disposition = in_stream->disposition;
|
||||
out_stream->avg_frame_rate = in_stream->avg_frame_rate;
|
||||
out_stream->r_frame_rate = in_stream->r_frame_rate;
|
||||
return (out_stream);
|
||||
return out_stream;
|
||||
}
|
||||
|
||||
int open_output_file_for_write(AVFormatContext *out_ctx, const char *out_path, AVDictionary **options)
|
||||
@@ -43,16 +42,16 @@ int open_output_file_for_write(AVFormatContext *out_ctx, const char *out_path, A
|
||||
if (!(out_ctx->oformat->flags & AVFMT_NOFILE)) {
|
||||
if (avio_open(&out_ctx->pb, out_path, AVIO_FLAG_WRITE) < 0) {
|
||||
fprintf(stderr, "Error: Couldn't open file at %s.\n", out_path);
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
printf("Output flag set to AVFMT_NOFILE.\n");
|
||||
if (avformat_write_header(out_ctx, options) < 0) {
|
||||
fprintf(stderr, "Error: Couldn't write headers to file at %s.\n", out_path);
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void process_packet(AVPacket *pkt, AVStream *in_stream, AVStream *out_stream)
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
int init()
|
||||
{
|
||||
puts("Kyoo's transcoder initiated.");
|
||||
return (sizeof(stream));
|
||||
return sizeof(stream);
|
||||
}
|
||||
|
||||
stream *get_track_info(const char *path, unsigned *stream_count, unsigned *track_count)
|
||||
@@ -18,7 +18,7 @@ stream *get_track_info(const char *path, unsigned *stream_count, unsigned *track
|
||||
stream *streams;
|
||||
|
||||
if (open_input_context(&ctx, path) != 0)
|
||||
return (NULL);
|
||||
return NULL;
|
||||
*stream_count = ctx->nb_streams;
|
||||
*track_count = 0;
|
||||
streams = malloc(sizeof(stream) * *stream_count);
|
||||
@@ -44,5 +44,5 @@ stream *get_track_info(const char *path, unsigned *stream_count, unsigned *track
|
||||
streams[i] = NULLSTREAM;
|
||||
}
|
||||
avformat_close_input(&ctx);
|
||||
return (streams);
|
||||
return streams;
|
||||
}
|
||||
@@ -17,11 +17,11 @@ char *strrnchr(const char *str, int c, int occ_to_skip)
|
||||
if (*str == c) {
|
||||
occ_to_skip--;
|
||||
if (occ_to_skip == 0)
|
||||
return ((char *)str);
|
||||
return (char *)str;
|
||||
}
|
||||
str--;
|
||||
}
|
||||
return (NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *path_getfolder(const char *path)
|
||||
@@ -33,9 +33,9 @@ char *path_getfolder(const char *path)
|
||||
start = strrnchr(path, '/', 1);
|
||||
end = strrchr(path, '/');
|
||||
if (!end)
|
||||
return (NULL);
|
||||
return NULL;
|
||||
folder = strndup(start, end - start);
|
||||
return (folder);
|
||||
return folder;
|
||||
}
|
||||
|
||||
char *path_getfilename(const char *path)
|
||||
@@ -43,13 +43,13 @@ char *path_getfilename(const char *path)
|
||||
const char *name = strrchr(path, '/') ? strrchr(path, '/') + 1 : path;
|
||||
int len = strrchr(path, '.') ? strrchr(path, '.') - name : 1024;
|
||||
|
||||
return (strndup(name, len));
|
||||
return strndup(name, len);
|
||||
}
|
||||
|
||||
char *get_extension_from_codec(char *codec)
|
||||
{
|
||||
if (!codec)
|
||||
return (NULL);
|
||||
return NULL;
|
||||
if (!strcmp(codec, "subrip"))
|
||||
return(".srt");
|
||||
else if (!strcmp(codec, "ass"))
|
||||
@@ -57,7 +57,7 @@ char *get_extension_from_codec(char *codec)
|
||||
else {
|
||||
printf("Unsupported subtitle codec: %s.\n", codec);
|
||||
free(codec);
|
||||
return (NULL);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,11 +67,11 @@ int path_mkdir(const char *path, int mode)
|
||||
struct stat s;
|
||||
|
||||
if (!path)
|
||||
return (-1);
|
||||
return -1;
|
||||
ret = kmkdir(path, mode);
|
||||
if (ret < 0 && errno == EEXIST && stat(path, &s) == 0) {
|
||||
if (S_ISDIR(s.st_mode))
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
@@ -4,14 +4,14 @@
|
||||
|
||||
#include "stream.h"
|
||||
#include "helper.h"
|
||||
#include "compatibility.h"
|
||||
#include "path_helper.h"
|
||||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <transcoder.h>
|
||||
|
||||
int asprintf(char **strp, const char *fmt, ...);
|
||||
|
||||
int get_subtitle_data(stream *substream, AVStream *in_stream, const char *file_name, const char *out_path)
|
||||
{
|
||||
AVDictionaryEntry *languageptr = av_dict_get(in_stream->metadata, "language", NULL, 0);
|
||||
@@ -20,7 +20,7 @@ int get_subtitle_data(stream *substream, AVStream *in_stream, const char *file_n
|
||||
char *folder_path;
|
||||
|
||||
if (!extension)
|
||||
return (-1);
|
||||
return -1;
|
||||
*substream = (stream) {
|
||||
NULL,
|
||||
languageptr ? strdup(languageptr->value) : NULL,
|
||||
@@ -34,7 +34,7 @@ int get_subtitle_data(stream *substream, AVStream *in_stream, const char *file_n
|
||||
if (path_mkdir(folder_path, 0733) < 0) {
|
||||
if (!folder_path)
|
||||
free(folder_path);
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
asprintf(&substream->path, "%s/%s.%s%s%s%s", folder_path,
|
||||
file_name,
|
||||
@@ -44,8 +44,8 @@ int get_subtitle_data(stream *substream, AVStream *in_stream, const char *file_n
|
||||
extension);
|
||||
free(folder_path);
|
||||
if (!substream->path)
|
||||
return (-1);
|
||||
return (0);
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int copy_subtitle_stream(AVFormatContext **out_ctx, stream *s, AVFormatContext *int_ctx, AVStream *in_stream)
|
||||
@@ -54,19 +54,19 @@ int copy_subtitle_stream(AVFormatContext **out_ctx, stream *s, AVFormatContext *
|
||||
|
||||
if (avformat_alloc_output_context2(out_ctx, NULL, NULL, s->path) < 0) {
|
||||
printf("Error: Couldn't create an output file.\n");
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
av_dict_copy(&(*out_ctx)->metadata, int_ctx->metadata, 0);
|
||||
out_stream = copy_stream_to_output(*out_ctx, in_stream);
|
||||
if (out_stream) {
|
||||
if (open_output_file_for_write(*out_ctx, s->path, NULL) == 0)
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
if (*out_ctx && !((*out_ctx)->flags & AVFMT_NOFILE))
|
||||
avio_closep(&(*out_ctx)->pb);
|
||||
avformat_free_context(*out_ctx);
|
||||
fprintf(stderr, "An error occured, cleaning up th output context for the %s stream.\n", s->language);
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void write_data(AVFormatContext *int_ctx, AVFormatContext **output_list, unsigned int out_count)
|
||||
@@ -114,7 +114,7 @@ int split_inputfile(AVFormatContext *int_ctx, AVFormatContext **output_list, str
|
||||
char *file_name = path_getfilename(path);
|
||||
|
||||
if (!file_name)
|
||||
return (-1);
|
||||
return -1;
|
||||
for (unsigned int i = 0; i < int_ctx->nb_streams; i++) {
|
||||
AVStream *in_stream = int_ctx->streams[i];
|
||||
|
||||
@@ -131,7 +131,7 @@ int split_inputfile(AVFormatContext *int_ctx, AVFormatContext **output_list, str
|
||||
output_list[i] = NULL;
|
||||
}
|
||||
free(file_name);
|
||||
return (subcount);
|
||||
return subcount;
|
||||
}
|
||||
|
||||
stream *extract_subtitles(char *path, const char *out_path, unsigned *stream_count, unsigned *subtitle_count)
|
||||
@@ -141,7 +141,7 @@ stream *extract_subtitles(char *path, const char *out_path, unsigned *stream_cou
|
||||
stream *streams;
|
||||
|
||||
if (open_input_context(&int_ctx, path) != 0)
|
||||
return (NULL);
|
||||
return NULL;
|
||||
*stream_count = int_ctx->nb_streams;
|
||||
streams = calloc(sizeof(stream), *stream_count);
|
||||
output_list = malloc(sizeof(AVFormatContext *) * *stream_count);
|
||||
@@ -150,7 +150,7 @@ stream *extract_subtitles(char *path, const char *out_path, unsigned *stream_cou
|
||||
if (*subtitle_count >= 0) {
|
||||
write_data(int_ctx, output_list, *stream_count);
|
||||
finish_up(int_ctx, output_list, *stream_count);
|
||||
return (streams);
|
||||
return streams;
|
||||
}
|
||||
}
|
||||
*subtitle_count = 0;
|
||||
@@ -158,5 +158,5 @@ stream *extract_subtitles(char *path, const char *out_path, unsigned *stream_cou
|
||||
free_streams(streams, (int)*stream_count);
|
||||
if (output_list)
|
||||
free(output_list);
|
||||
return (NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@
|
||||
static bool should_copy_to_transmuxed(enum AVMediaType codec_type)
|
||||
{
|
||||
if (codec_type == AVMEDIA_TYPE_VIDEO)
|
||||
return (true);
|
||||
return true;
|
||||
if (codec_type == AVMEDIA_TYPE_AUDIO)
|
||||
return (true);
|
||||
return (false);
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static int *prepare_streammap(AVFormatContext *in_ctx, AVFormatContext *out_ctx)
|
||||
@@ -24,7 +24,7 @@ static int *prepare_streammap(AVFormatContext *in_ctx, AVFormatContext *out_ctx)
|
||||
AVStream *stream;
|
||||
|
||||
if (!stream_map)
|
||||
return (NULL);
|
||||
return NULL;
|
||||
for (unsigned i = 0; i < in_ctx->nb_streams; i++) {
|
||||
stream = in_ctx->streams[i];
|
||||
if (should_copy_to_transmuxed(stream->codecpar->codec_type)) {
|
||||
@@ -32,12 +32,12 @@ static int *prepare_streammap(AVFormatContext *in_ctx, AVFormatContext *out_ctx)
|
||||
stream_count++;
|
||||
if (!copy_stream_to_output(out_ctx, stream)) {
|
||||
free(stream_map);
|
||||
return (NULL);
|
||||
return NULL;
|
||||
}
|
||||
} else
|
||||
stream_map[i] = -1;
|
||||
}
|
||||
return (stream_map);
|
||||
return stream_map;
|
||||
}
|
||||
|
||||
static AVDictionary *create_options_context(const char *out_path)
|
||||
@@ -48,15 +48,16 @@ static AVDictionary *create_options_context(const char *out_path)
|
||||
|
||||
sprintf(seg_path, "%.*s/segments/", folder_index, out_path);
|
||||
if (path_mkdir(seg_path, 0733) < 0) {
|
||||
fprintf(stderr, "Error: Couldn't create segment output folder. Part of the output path does not exist or you don't have write rights.\n");
|
||||
return (NULL);
|
||||
fprintf(stderr, "Error: Couldn't create segment output folder. "
|
||||
"Part of the output path does not exist or you don't have write rights.\n");
|
||||
return NULL;
|
||||
}
|
||||
strcat(seg_path, "%v-%03d.ts");
|
||||
av_dict_set(&options, "hls_segment_filename", seg_path, 0);
|
||||
av_dict_set(&options, "hls_base_url", "segment/", 0);
|
||||
av_dict_set(&options, "hls_list_size", "0", 0);
|
||||
av_dict_set(&options, "streaming", "1", 0);
|
||||
return (options);
|
||||
return options;
|
||||
}
|
||||
|
||||
static void write_to_output(AVFormatContext *in_ctx, AVFormatContext *out_ctx, int *stream_map, float *playable_duration)
|
||||
@@ -108,20 +109,20 @@ int transmux(const char *path, const char *out_path, float *playable_duration)
|
||||
av_log_set_level(AV_LOG_WARNING);
|
||||
if (open_input_context(&in_ctx, path) != 0) {
|
||||
fprintf(stderr, "Error: Coudln't open the input file.\n");
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
if (avformat_alloc_output_context2(&out_ctx, NULL, NULL, out_path) < 0) {
|
||||
fprintf(stderr, "Error: Couldn't create an output file.\n");
|
||||
avformat_close_input(&in_ctx);
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
stream_map = prepare_streammap(in_ctx, out_ctx);
|
||||
options = create_options_context(out_path);
|
||||
if (!stream_map || !options || open_output_file_for_write(out_ctx, out_path, &options) != 0) {
|
||||
destroy_context(in_ctx, out_ctx, options, stream_map);
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
write_to_output(in_ctx, out_ctx, stream_map, playable_duration);
|
||||
destroy_context(in_ctx, out_ctx, options, stream_map);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user