2011-06-21 20:42:55 +02:00
|
|
|
// Copyright (c) 2011 The WebM project authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the LICENSE file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
2012-01-19 21:28:38 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2012-08-15 01:40:33 +02:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
2011-06-21 20:42:55 +02:00
|
|
|
|
|
|
|
// libwebm parser includes
|
|
|
|
#include "mkvreader.hpp"
|
|
|
|
#include "mkvparser.hpp"
|
|
|
|
|
|
|
|
// libwebm muxer includes
|
|
|
|
#include "mkvmuxer.hpp"
|
|
|
|
#include "mkvwriter.hpp"
|
|
|
|
#include "mkvmuxerutil.hpp"
|
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
#include "sample_muxer_metadata.h"
|
|
|
|
|
2014-01-23 08:26:26 +01:00
|
|
|
using mkvmuxer::int64;
|
2012-08-15 01:40:33 +02:00
|
|
|
using mkvmuxer::uint64;
|
|
|
|
|
2014-01-16 00:00:59 +01:00
|
|
|
#ifdef _MSC_VER
|
2014-01-22 23:03:40 +01:00
|
|
|
// Disable MSVC warnings that suggest making code non-portable.
|
2014-04-14 21:06:47 +02:00
|
|
|
#pragma warning(disable : 4996)
|
2014-01-16 00:00:59 +01:00
|
|
|
#endif
|
|
|
|
|
2011-06-21 20:42:55 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void Usage() {
|
|
|
|
printf("Usage: sample_muxer -i input -o output [options]\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("Main options:\n");
|
|
|
|
printf(" -h | -? show help\n");
|
|
|
|
printf(" -video <int> >0 outputs video\n");
|
|
|
|
printf(" -audio <int> >0 outputs audio\n");
|
|
|
|
printf(" -live <int> >0 puts the muxer into live mode\n");
|
|
|
|
printf(" 0 puts the muxer into file mode\n");
|
|
|
|
printf(" -output_cues <int> >0 outputs cues element\n");
|
|
|
|
printf(" -cues_on_video_track <int> >0 outputs cues on video track\n");
|
2011-08-14 16:49:50 +02:00
|
|
|
printf(" -cues_on_audio_track <int> >0 outputs cues on audio track\n");
|
2011-06-21 20:42:55 +02:00
|
|
|
printf(" -max_cluster_duration <double> in seconds\n");
|
|
|
|
printf(" -max_cluster_size <int> in bytes\n");
|
|
|
|
printf(" -switch_tracks <int> >0 switches tracks in output\n");
|
2011-08-14 18:33:30 +02:00
|
|
|
printf(" -audio_track_number <int> >0 Changes the audio track number\n");
|
|
|
|
printf(" -video_track_number <int> >0 Changes the video track number\n");
|
2011-07-19 22:47:47 +02:00
|
|
|
printf(" -chunking <string> Chunk output\n");
|
2011-06-21 20:42:55 +02:00
|
|
|
printf("\n");
|
|
|
|
printf("Video options:\n");
|
|
|
|
printf(" -display_width <int> Display width in pixels\n");
|
|
|
|
printf(" -display_height <int> Display height in pixels\n");
|
|
|
|
printf(" -stereo_mode <int> 3D video mode\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("Cues options:\n");
|
|
|
|
printf(" -output_cues_block_number <int> >0 outputs cue block number\n");
|
2013-05-25 00:50:13 +02:00
|
|
|
printf(" -cues_before_clusters <int> >0 puts Cues before Clusters\n");
|
2012-08-15 01:40:33 +02:00
|
|
|
printf("\n");
|
|
|
|
printf("Metadata options:\n");
|
2014-04-14 21:06:47 +02:00
|
|
|
printf(" -webvtt-subtitles <vttfile> ");
|
|
|
|
printf("add WebVTT subtitles as metadata track\n");
|
|
|
|
printf(" -webvtt-captions <vttfile> ");
|
|
|
|
printf("add WebVTT captions as metadata track\n");
|
|
|
|
printf(" -webvtt-descriptions <vttfile> ");
|
|
|
|
printf("add WebVTT descriptions as metadata track\n");
|
|
|
|
printf(" -webvtt-metadata <vttfile> ");
|
|
|
|
printf("add WebVTT subtitles as metadata track\n");
|
|
|
|
printf(" -webvtt-chapters <vttfile> ");
|
|
|
|
printf("add WebVTT chapters as MKV chapters element\n");
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
struct MetadataFile {
|
|
|
|
const char* name;
|
|
|
|
SampleMuxerMetadata::Kind kind;
|
|
|
|
};
|
2011-06-21 20:42:55 +02:00
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
typedef std::list<MetadataFile> metadata_files_t;
|
|
|
|
|
|
|
|
// Cache the WebVTT filenames specified as command-line args.
|
2014-04-14 21:06:47 +02:00
|
|
|
bool LoadMetadataFiles(const metadata_files_t& files,
|
|
|
|
SampleMuxerMetadata* metadata) {
|
2012-08-15 01:40:33 +02:00
|
|
|
typedef metadata_files_t::const_iterator iter_t;
|
|
|
|
|
|
|
|
iter_t i = files.begin();
|
|
|
|
const iter_t j = files.end();
|
|
|
|
|
|
|
|
while (i != j) {
|
|
|
|
const metadata_files_t::value_type& v = *i++;
|
|
|
|
|
|
|
|
if (!metadata->Load(v.name, v.kind))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:06:47 +02:00
|
|
|
int ParseArgWebVTT(char* argv[], int* argv_index, int argc_check,
|
|
|
|
metadata_files_t* metadata_files) {
|
2012-08-15 01:40:33 +02:00
|
|
|
int& i = *argv_index;
|
|
|
|
|
2012-10-23 01:20:20 +02:00
|
|
|
enum { kCount = 5 };
|
2014-04-14 21:06:47 +02:00
|
|
|
struct Arg {
|
|
|
|
const char* name;
|
|
|
|
SampleMuxerMetadata::Kind kind;
|
2012-08-15 01:40:33 +02:00
|
|
|
};
|
2014-04-14 21:06:47 +02:00
|
|
|
const Arg args[kCount] = {
|
|
|
|
{"-webvtt-subtitles", SampleMuxerMetadata::kSubtitles},
|
|
|
|
{"-webvtt-captions", SampleMuxerMetadata::kCaptions},
|
|
|
|
{"-webvtt-descriptions", SampleMuxerMetadata::kDescriptions},
|
|
|
|
{"-webvtt-metadata", SampleMuxerMetadata::kMetadata},
|
|
|
|
{"-webvtt-chapters", SampleMuxerMetadata::kChapters}};
|
2012-08-15 01:40:33 +02:00
|
|
|
|
|
|
|
for (int idx = 0; idx < kCount; ++idx) {
|
|
|
|
const Arg& arg = args[idx];
|
|
|
|
|
|
|
|
if (strcmp(arg.name, argv[i]) != 0) // no match
|
|
|
|
continue;
|
|
|
|
|
|
|
|
++i; // consume arg name here
|
|
|
|
|
|
|
|
if (i > argc_check) {
|
|
|
|
printf("missing value for %s\n", arg.name);
|
|
|
|
return -1; // error
|
|
|
|
}
|
|
|
|
|
|
|
|
MetadataFile f;
|
|
|
|
f.name = argv[i]; // arg value is consumed via caller's loop idx
|
|
|
|
f.kind = arg.kind;
|
|
|
|
|
|
|
|
metadata_files->push_back(f);
|
|
|
|
return 1; // successfully parsed WebVTT arg
|
|
|
|
}
|
2011-06-21 20:42:55 +02:00
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
return 0; // not a WebVTT arg
|
|
|
|
}
|
|
|
|
|
2012-10-08 21:50:41 +02:00
|
|
|
} // end namespace
|
2012-08-15 01:40:33 +02:00
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2011-06-21 20:42:55 +02:00
|
|
|
char* input = NULL;
|
|
|
|
char* output = NULL;
|
|
|
|
|
|
|
|
// Segment variables
|
|
|
|
bool output_video = true;
|
|
|
|
bool output_audio = true;
|
|
|
|
bool live_mode = false;
|
|
|
|
bool output_cues = true;
|
2013-05-25 00:50:13 +02:00
|
|
|
bool cues_before_clusters = false;
|
2011-06-21 20:42:55 +02:00
|
|
|
bool cues_on_video_track = true;
|
2012-03-15 21:27:49 +01:00
|
|
|
bool cues_on_audio_track = false;
|
2011-06-21 20:42:55 +02:00
|
|
|
uint64 max_cluster_duration = 0;
|
|
|
|
uint64 max_cluster_size = 0;
|
|
|
|
bool switch_tracks = false;
|
2012-10-23 01:20:20 +02:00
|
|
|
int audio_track_number = 0; // 0 tells muxer to decide.
|
|
|
|
int video_track_number = 0; // 0 tells muxer to decide.
|
2011-07-19 22:47:47 +02:00
|
|
|
bool chunking = false;
|
|
|
|
const char* chunk_name = NULL;
|
2011-06-21 20:42:55 +02:00
|
|
|
|
|
|
|
bool output_cues_block_number = true;
|
|
|
|
|
|
|
|
uint64 display_width = 0;
|
|
|
|
uint64 display_height = 0;
|
|
|
|
uint64 stereo_mode = 0;
|
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
metadata_files_t metadata_files;
|
|
|
|
|
2012-03-15 21:27:49 +01:00
|
|
|
const int argc_check = argc - 1;
|
2011-06-21 20:42:55 +02:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
char* end;
|
|
|
|
|
|
|
|
if (!strcmp("-h", argv[i]) || !strcmp("-?", argv[i])) {
|
|
|
|
Usage();
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_SUCCESS;
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-i", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
input = argv[++i];
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-o", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
output = argv[++i];
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-video", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
output_video = strtol(argv[++i], &end, 10) == 0 ? false : true;
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-audio", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
output_audio = strtol(argv[++i], &end, 10) == 0 ? false : true;
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-live", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
live_mode = strtol(argv[++i], &end, 10) == 0 ? false : true;
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-output_cues", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
output_cues = strtol(argv[++i], &end, 10) == 0 ? false : true;
|
2013-05-25 00:50:13 +02:00
|
|
|
} else if (!strcmp("-cues_before_clusters", argv[i]) && i < argc_check) {
|
|
|
|
cues_before_clusters = strtol(argv[++i], &end, 10) == 0 ? false : true;
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-cues_on_video_track", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
cues_on_video_track = strtol(argv[++i], &end, 10) == 0 ? false : true;
|
2012-03-15 21:27:49 +01:00
|
|
|
if (cues_on_video_track)
|
|
|
|
cues_on_audio_track = false;
|
|
|
|
} else if (!strcmp("-cues_on_audio_track", argv[i]) && i < argc_check) {
|
2011-08-14 16:49:50 +02:00
|
|
|
cues_on_audio_track = strtol(argv[++i], &end, 10) == 0 ? false : true;
|
2012-03-15 21:27:49 +01:00
|
|
|
if (cues_on_audio_track)
|
|
|
|
cues_on_video_track = false;
|
|
|
|
} else if (!strcmp("-max_cluster_duration", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
const double seconds = strtod(argv[++i], &end);
|
2014-04-14 21:06:47 +02:00
|
|
|
max_cluster_duration = static_cast<uint64>(seconds * 1000000000.0);
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-max_cluster_size", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
max_cluster_size = strtol(argv[++i], &end, 10);
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-switch_tracks", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
switch_tracks = strtol(argv[++i], &end, 10) == 0 ? false : true;
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-audio_track_number", argv[i]) && i < argc_check) {
|
2011-08-14 18:33:30 +02:00
|
|
|
audio_track_number = strtol(argv[++i], &end, 10);
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-video_track_number", argv[i]) && i < argc_check) {
|
2011-08-14 18:33:30 +02:00
|
|
|
video_track_number = strtol(argv[++i], &end, 10);
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-chunking", argv[i]) && i < argc_check) {
|
2011-07-19 22:47:47 +02:00
|
|
|
chunking = true;
|
|
|
|
chunk_name = argv[++i];
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-display_width", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
display_width = strtol(argv[++i], &end, 10);
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-display_height", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
display_height = strtol(argv[++i], &end, 10);
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-stereo_mode", argv[i]) && i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
stereo_mode = strtol(argv[++i], &end, 10);
|
2012-03-15 21:27:49 +01:00
|
|
|
} else if (!strcmp("-output_cues_block_number", argv[i]) &&
|
|
|
|
i < argc_check) {
|
2011-06-21 20:42:55 +02:00
|
|
|
output_cues_block_number =
|
|
|
|
strtol(argv[++i], &end, 10) == 0 ? false : true;
|
2012-08-15 01:40:33 +02:00
|
|
|
} else if (int e = ParseArgWebVTT(argv, &i, argc_check, &metadata_files)) {
|
|
|
|
if (e < 0)
|
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input == NULL || output == NULL) {
|
|
|
|
Usage();
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get parser header info
|
|
|
|
mkvparser::MkvReader reader;
|
|
|
|
|
|
|
|
if (reader.Open(input)) {
|
|
|
|
printf("\n Filename is invalid or error while opening.\n");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
long long pos = 0;
|
|
|
|
mkvparser::EBMLHeader ebml_header;
|
|
|
|
ebml_header.Parse(&reader, pos);
|
|
|
|
|
|
|
|
mkvparser::Segment* parser_segment;
|
2014-04-14 21:06:47 +02:00
|
|
|
long long ret =
|
|
|
|
mkvparser::Segment::CreateInstance(&reader, pos, parser_segment);
|
2011-06-21 20:42:55 +02:00
|
|
|
if (ret) {
|
|
|
|
printf("\n Segment::CreateInstance() failed.");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = parser_segment->Load();
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("\n Segment::Load() failed.");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const mkvparser::SegmentInfo* const segment_info = parser_segment->GetInfo();
|
|
|
|
const long long timeCodeScale = segment_info->GetTimeCodeScale();
|
|
|
|
|
|
|
|
// Set muxer header info
|
|
|
|
mkvmuxer::MkvWriter writer;
|
|
|
|
|
2013-06-14 00:25:03 +02:00
|
|
|
char* temp_file = tmpnam(NULL);
|
|
|
|
if (!writer.Open(cues_before_clusters ? temp_file : output)) {
|
2011-06-21 20:42:55 +02:00
|
|
|
printf("\n Filename is invalid or error while opening.\n");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set Segment element attributes
|
2012-01-20 02:19:37 +01:00
|
|
|
mkvmuxer::Segment muxer_segment;
|
|
|
|
|
|
|
|
if (!muxer_segment.Init(&writer)) {
|
|
|
|
printf("\n Could not initialize muxer segment!\n");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2012-01-20 02:19:37 +01:00
|
|
|
}
|
|
|
|
|
2011-06-21 20:42:55 +02:00
|
|
|
if (live_mode)
|
|
|
|
muxer_segment.set_mode(mkvmuxer::Segment::kLive);
|
|
|
|
else
|
|
|
|
muxer_segment.set_mode(mkvmuxer::Segment::kFile);
|
|
|
|
|
2011-07-19 22:47:47 +02:00
|
|
|
if (chunking)
|
|
|
|
muxer_segment.SetChunking(true, chunk_name);
|
|
|
|
|
2011-06-21 20:42:55 +02:00
|
|
|
if (max_cluster_duration > 0)
|
|
|
|
muxer_segment.set_max_cluster_duration(max_cluster_duration);
|
|
|
|
if (max_cluster_size > 0)
|
|
|
|
muxer_segment.set_max_cluster_size(max_cluster_size);
|
|
|
|
muxer_segment.OutputCues(output_cues);
|
|
|
|
|
|
|
|
// Set SegmentInfo element attributes
|
2011-07-18 20:28:12 +02:00
|
|
|
mkvmuxer::SegmentInfo* const info = muxer_segment.GetSegmentInfo();
|
2011-06-21 20:42:55 +02:00
|
|
|
info->set_timecode_scale(timeCodeScale);
|
|
|
|
info->set_writing_app("sample_muxer");
|
|
|
|
|
|
|
|
// Set Tracks element attributes
|
2011-07-18 20:28:12 +02:00
|
|
|
const mkvparser::Tracks* const parser_tracks = parser_segment->GetTracks();
|
2011-06-21 20:42:55 +02:00
|
|
|
unsigned long i = 0;
|
2012-10-23 01:20:20 +02:00
|
|
|
uint64 vid_track = 0; // no track added
|
|
|
|
uint64 aud_track = 0; // no track added
|
2011-06-21 20:42:55 +02:00
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
using mkvparser::Track;
|
|
|
|
|
2011-06-21 20:42:55 +02:00
|
|
|
while (i != parser_tracks->GetTracksCount()) {
|
|
|
|
int track_num = i++;
|
|
|
|
if (switch_tracks)
|
|
|
|
track_num = i % parser_tracks->GetTracksCount();
|
|
|
|
|
|
|
|
const mkvparser::Track* const parser_track =
|
|
|
|
parser_tracks->GetTrackByIndex(track_num);
|
|
|
|
|
|
|
|
if (parser_track == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO(fgalligan): Add support for language to parser.
|
2011-07-18 20:28:12 +02:00
|
|
|
const char* const track_name = parser_track->GetNameAsUTF8();
|
2011-06-21 20:42:55 +02:00
|
|
|
|
|
|
|
const long long track_type = parser_track->GetType();
|
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
if (track_type == Track::kVideo && output_video) {
|
2011-06-21 20:42:55 +02:00
|
|
|
// Get the video track from the parser
|
|
|
|
const mkvparser::VideoTrack* const pVideoTrack =
|
|
|
|
static_cast<const mkvparser::VideoTrack*>(parser_track);
|
2014-04-14 21:06:47 +02:00
|
|
|
const long long width = pVideoTrack->GetWidth();
|
2011-06-21 20:42:55 +02:00
|
|
|
const long long height = pVideoTrack->GetHeight();
|
|
|
|
|
|
|
|
// Add the video track to the muxer
|
|
|
|
vid_track = muxer_segment.AddVideoTrack(static_cast<int>(width),
|
2011-08-14 18:33:30 +02:00
|
|
|
static_cast<int>(height),
|
|
|
|
video_track_number);
|
2011-06-21 20:42:55 +02:00
|
|
|
if (!vid_track) {
|
|
|
|
printf("\n Could not add video track.\n");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
2014-04-14 21:06:47 +02:00
|
|
|
mkvmuxer::VideoTrack* const video = static_cast<mkvmuxer::VideoTrack*>(
|
|
|
|
muxer_segment.GetTrackByNumber(vid_track));
|
2011-06-21 20:42:55 +02:00
|
|
|
if (!video) {
|
|
|
|
printf("\n Could not get video track.\n");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (track_name)
|
|
|
|
video->set_name(track_name);
|
|
|
|
|
2014-01-09 23:56:15 +01:00
|
|
|
video->set_codec_id(pVideoTrack->GetCodecId());
|
|
|
|
|
2011-06-21 20:42:55 +02:00
|
|
|
if (display_width > 0)
|
|
|
|
video->set_display_width(display_width);
|
|
|
|
if (display_height > 0)
|
|
|
|
video->set_display_height(display_height);
|
|
|
|
if (stereo_mode > 0)
|
|
|
|
video->SetStereoMode(stereo_mode);
|
|
|
|
|
|
|
|
const double rate = pVideoTrack->GetFrameRate();
|
|
|
|
if (rate > 0.0) {
|
|
|
|
video->set_frame_rate(rate);
|
|
|
|
}
|
2012-08-15 01:40:33 +02:00
|
|
|
} else if (track_type == Track::kAudio && output_audio) {
|
2011-06-21 20:42:55 +02:00
|
|
|
// Get the audio track from the parser
|
|
|
|
const mkvparser::AudioTrack* const pAudioTrack =
|
|
|
|
static_cast<const mkvparser::AudioTrack*>(parser_track);
|
2014-04-14 21:06:47 +02:00
|
|
|
const long long channels = pAudioTrack->GetChannels();
|
2011-06-21 20:42:55 +02:00
|
|
|
const double sample_rate = pAudioTrack->GetSamplingRate();
|
|
|
|
|
|
|
|
// Add the audio track to the muxer
|
|
|
|
aud_track = muxer_segment.AddAudioTrack(static_cast<int>(sample_rate),
|
2011-08-14 18:33:30 +02:00
|
|
|
static_cast<int>(channels),
|
|
|
|
audio_track_number);
|
2011-06-21 20:42:55 +02:00
|
|
|
if (!aud_track) {
|
|
|
|
printf("\n Could not add audio track.\n");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
2014-04-14 21:06:47 +02:00
|
|
|
mkvmuxer::AudioTrack* const audio = static_cast<mkvmuxer::AudioTrack*>(
|
|
|
|
muxer_segment.GetTrackByNumber(aud_track));
|
2011-06-21 20:42:55 +02:00
|
|
|
if (!audio) {
|
|
|
|
printf("\n Could not get audio track.\n");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (track_name)
|
|
|
|
audio->set_name(track_name);
|
|
|
|
|
2014-01-09 23:56:15 +01:00
|
|
|
audio->set_codec_id(pAudioTrack->GetCodecId());
|
|
|
|
|
2011-06-21 20:42:55 +02:00
|
|
|
size_t private_size;
|
2011-07-18 20:28:12 +02:00
|
|
|
const unsigned char* const private_data =
|
2011-06-21 20:42:55 +02:00
|
|
|
pAudioTrack->GetCodecPrivate(private_size);
|
|
|
|
if (private_size > 0) {
|
|
|
|
if (!audio->SetCodecPrivate(private_data, private_size)) {
|
|
|
|
printf("\n Could not add audio private data.\n");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const long long bit_depth = pAudioTrack->GetBitDepth();
|
|
|
|
if (bit_depth > 0)
|
|
|
|
audio->set_bit_depth(bit_depth);
|
2014-01-09 21:03:24 +01:00
|
|
|
|
|
|
|
if (pAudioTrack->GetCodecDelay())
|
|
|
|
audio->set_codec_delay(pAudioTrack->GetCodecDelay());
|
|
|
|
if (pAudioTrack->GetSeekPreRoll())
|
|
|
|
audio->set_seek_pre_roll(pAudioTrack->GetSeekPreRoll());
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
// We have created all the video and audio tracks. If any WebVTT
|
|
|
|
// files were specified as command-line args, then parse them and
|
|
|
|
// add a track to the output file corresponding to each metadata
|
|
|
|
// input file.
|
|
|
|
|
|
|
|
SampleMuxerMetadata metadata;
|
2012-10-08 21:50:41 +02:00
|
|
|
|
|
|
|
if (!metadata.Init(&muxer_segment)) {
|
|
|
|
printf("\n Could not initialize metadata cache.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2012-08-15 01:40:33 +02:00
|
|
|
|
|
|
|
if (!LoadMetadataFiles(metadata_files, &metadata))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2012-10-23 01:20:20 +02:00
|
|
|
if (!metadata.AddChapters())
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2011-06-21 20:42:55 +02:00
|
|
|
// Set Cues element attributes
|
2011-07-18 20:28:12 +02:00
|
|
|
mkvmuxer::Cues* const cues = muxer_segment.GetCues();
|
2011-06-21 20:42:55 +02:00
|
|
|
cues->set_output_block_number(output_cues_block_number);
|
2011-08-14 18:33:30 +02:00
|
|
|
if (cues_on_video_track && vid_track)
|
|
|
|
muxer_segment.CuesTrack(vid_track);
|
|
|
|
if (cues_on_audio_track && aud_track)
|
|
|
|
muxer_segment.CuesTrack(aud_track);
|
2011-06-21 20:42:55 +02:00
|
|
|
|
|
|
|
// Write clusters
|
|
|
|
unsigned char* data = NULL;
|
|
|
|
int data_len = 0;
|
|
|
|
|
|
|
|
const mkvparser::Cluster* cluster = parser_segment->GetFirst();
|
|
|
|
|
|
|
|
while ((cluster != NULL) && !cluster->EOS()) {
|
2012-02-24 02:55:37 +01:00
|
|
|
const mkvparser::BlockEntry* block_entry;
|
|
|
|
|
|
|
|
long status = cluster->GetFirst(block_entry);
|
|
|
|
|
2012-10-23 01:20:20 +02:00
|
|
|
if (status) {
|
2014-04-14 21:06:47 +02:00
|
|
|
printf("\n Could not get first block of cluster.\n");
|
|
|
|
return EXIT_FAILURE;
|
2012-02-24 02:55:37 +01:00
|
|
|
}
|
2011-06-21 20:42:55 +02:00
|
|
|
|
|
|
|
while ((block_entry != NULL) && !block_entry->EOS()) {
|
|
|
|
const mkvparser::Block* const block = block_entry->GetBlock();
|
|
|
|
const long long trackNum = block->GetTrackNumber();
|
|
|
|
const mkvparser::Track* const parser_track =
|
2014-04-14 21:06:47 +02:00
|
|
|
parser_tracks->GetTrackByNumber(static_cast<unsigned long>(trackNum));
|
2014-05-16 22:42:13 +02:00
|
|
|
|
|
|
|
// When |parser_track| is NULL, it means that the track number in the
|
|
|
|
// Block is invalid (i.e.) the was no TrackEntry corresponding to the
|
|
|
|
// track number. So we reject the file.
|
|
|
|
if (!parser_track) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-06-21 20:42:55 +02:00
|
|
|
const long long track_type = parser_track->GetType();
|
2012-08-15 01:40:33 +02:00
|
|
|
const long long time_ns = block->GetTime(cluster);
|
2011-06-21 20:42:55 +02:00
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
// Flush any metadata frames to the output file, before we write
|
|
|
|
// the current block.
|
|
|
|
if (!metadata.Write(time_ns))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
if ((track_type == Track::kAudio && output_audio) ||
|
|
|
|
(track_type == Track::kVideo && output_video)) {
|
2011-06-21 20:42:55 +02:00
|
|
|
const int frame_count = block->GetFrameCount();
|
|
|
|
const bool is_key = block->IsKey();
|
2014-01-23 08:26:26 +01:00
|
|
|
const int64 discard_padding = block->GetDiscardPadding();
|
2011-06-21 20:42:55 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < frame_count; ++i) {
|
|
|
|
const mkvparser::Block::Frame& frame = block->GetFrame(i);
|
|
|
|
|
|
|
|
if (frame.len > data_len) {
|
2014-04-14 21:06:47 +02:00
|
|
|
delete[] data;
|
2011-06-21 20:42:55 +02:00
|
|
|
data = new unsigned char[frame.len];
|
|
|
|
if (!data)
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
data_len = frame.len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.Read(&reader, data))
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
|
|
|
|
uint64 track_num = vid_track;
|
2012-08-15 01:40:33 +02:00
|
|
|
if (track_type == Track::kAudio)
|
2011-06-21 20:42:55 +02:00
|
|
|
track_num = aud_track;
|
|
|
|
|
2014-01-23 08:26:26 +01:00
|
|
|
bool frame_added = false;
|
|
|
|
if (discard_padding) {
|
2014-04-14 21:06:47 +02:00
|
|
|
frame_added = muxer_segment.AddFrameWithDiscardPadding(
|
|
|
|
data, frame.len, discard_padding, track_num, time_ns, is_key);
|
2014-01-23 08:26:26 +01:00
|
|
|
} else {
|
2014-04-14 21:06:47 +02:00
|
|
|
frame_added = muxer_segment.AddFrame(data, frame.len, track_num,
|
|
|
|
time_ns, is_key);
|
2014-01-23 08:26:26 +01:00
|
|
|
}
|
|
|
|
if (!frame_added) {
|
2011-06-21 20:42:55 +02:00
|
|
|
printf("\n Could not add frame.\n");
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_FAILURE;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 02:55:37 +01:00
|
|
|
status = cluster->GetNext(block_entry, block_entry);
|
|
|
|
|
2012-10-23 01:20:20 +02:00
|
|
|
if (status) {
|
2014-04-14 21:06:47 +02:00
|
|
|
printf("\n Could not get next block of cluster.\n");
|
|
|
|
return EXIT_FAILURE;
|
2012-02-24 02:55:37 +01:00
|
|
|
}
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cluster = parser_segment->GetNext(cluster);
|
|
|
|
}
|
|
|
|
|
2012-08-15 01:40:33 +02:00
|
|
|
// We have exhausted all video and audio frames in the input file.
|
|
|
|
// Flush any remaining metadata frames to the output file.
|
|
|
|
if (!metadata.Write(-1))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2012-10-23 01:20:20 +02:00
|
|
|
if (!muxer_segment.Finalize()) {
|
|
|
|
printf("Finalization of segment failed.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2011-06-21 20:42:55 +02:00
|
|
|
|
2013-06-14 00:25:03 +02:00
|
|
|
reader.Close();
|
|
|
|
writer.Close();
|
|
|
|
|
|
|
|
if (cues_before_clusters) {
|
|
|
|
if (reader.Open(temp_file)) {
|
|
|
|
printf("\n Filename is invalid or error while opening.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (!writer.Open(output)) {
|
|
|
|
printf("\n Filename is invalid or error while opening.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (!muxer_segment.CopyAndMoveCuesBeforeClusters(&reader, &writer)) {
|
|
|
|
printf("\n Unable to copy and move cues before clusters.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
reader.Close();
|
|
|
|
writer.Close();
|
|
|
|
remove(temp_file);
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:06:47 +02:00
|
|
|
delete[] data;
|
2011-06-21 20:42:55 +02:00
|
|
|
delete parser_segment;
|
2011-07-18 20:28:12 +02:00
|
|
|
|
2012-01-19 21:28:38 +01:00
|
|
|
return EXIT_SUCCESS;
|
2011-06-21 20:42:55 +02:00
|
|
|
}
|