Converted to new libmist JSON support, added dtscfix for metadata rewriting.
This commit is contained in:
parent
61abc0e0a1
commit
b54ee2dcd2
6 changed files with 185 additions and 37 deletions
|
@ -1,5 +1,6 @@
|
|||
AM_CPPFLAGS = $(MIST_CFLAGS)
|
||||
LDADD = $(MIST_LIBS)
|
||||
bin_PROGRAMS=MistDTSC2FLV MistFLV2DTSC
|
||||
bin_PROGRAMS=MistDTSC2FLV MistFLV2DTSC MistDTSCFix
|
||||
MistDTSC2FLV_SOURCES=dtsc2flv.cpp
|
||||
MistFLV2DTSC_SOURCES=flv2dtsc.cpp
|
||||
MistDTSCFix_SOURCES=dtscfix.cpp
|
||||
|
|
117
src/converters/dtscfix.cpp
Normal file
117
src/converters/dtscfix.cpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
/// \file dtscfix.cpp
|
||||
/// Contains the code that will attempt to fix the metadata contained in an DTSC file.
|
||||
|
||||
#include <string>
|
||||
#include <mist/dtsc.h>
|
||||
#include <mist/json.h>
|
||||
#include <mist/config.h>
|
||||
|
||||
/// Holds all code that converts filetypes to/from to DTSC.
|
||||
namespace Converters{
|
||||
|
||||
/// Reads an DTSC file and attempts to fix the metadata in it.
|
||||
int DTSCFix(Util::Config & conf) {
|
||||
DTSC::File F(conf.getString("filename"));
|
||||
std::string loader = F.getHeader();
|
||||
JSON::Value meta = JSON::fromDTMI(loader);
|
||||
JSON::Value pack;
|
||||
|
||||
long long unsigned int firstpack = 0;
|
||||
long long unsigned int nowpack = 0;
|
||||
long long unsigned int lastaudio = 0;
|
||||
long long unsigned int lastvideo = 0;
|
||||
long long unsigned int lastkey = 0;
|
||||
long long unsigned int totalvideo = 0;
|
||||
long long unsigned int totalaudio = 0;
|
||||
long long unsigned int keyframes = 0;
|
||||
long long unsigned int key_min = 0xffffffff;
|
||||
long long unsigned int key_max = 0;
|
||||
long long unsigned int vid_min = 0xffffffff;
|
||||
long long unsigned int vid_max = 0;
|
||||
long long unsigned int aud_min = 0xffffffff;
|
||||
long long unsigned int aud_max = 0;
|
||||
long long unsigned int bfrm_min = 0xffffffff;
|
||||
long long unsigned int bfrm_max = 0;
|
||||
long long unsigned int bps = 0;
|
||||
|
||||
while (loader.size() != 0){
|
||||
loader = F.getPacket();
|
||||
if (loader.size() != 0){
|
||||
pack = JSON::fromDTMI(loader);
|
||||
nowpack = pack["time"].asInt();
|
||||
if (firstpack == 0){firstpack = nowpack;}
|
||||
if (pack["datatype"].asString() == "audio"){
|
||||
if (lastaudio != 0 && (nowpack - lastaudio) != 0){
|
||||
bps = pack["data"].asString().size() / (nowpack - lastaudio);
|
||||
if (bps < aud_min){aud_min = bps;}
|
||||
if (bps > aud_max){aud_max = bps;}
|
||||
}
|
||||
totalaudio += pack["data"].asString().size();
|
||||
lastaudio = nowpack;
|
||||
}
|
||||
if (pack["datatype"].asString() == "video"){
|
||||
if (lastvideo != 0 && (nowpack - lastvideo) != 0){
|
||||
bps = pack["data"].asString().size() / (nowpack - lastvideo);
|
||||
if (bps < vid_min){vid_min = bps;}
|
||||
if (bps > vid_max){vid_max = bps;}
|
||||
}
|
||||
if (pack["keyframe"].asInt() != 0){
|
||||
if (lastkey != 0){
|
||||
bps = nowpack - lastkey;
|
||||
if (bps < key_min){key_min = bps;}
|
||||
if (bps > key_max){key_max = bps;}
|
||||
}
|
||||
keyframes++;
|
||||
lastkey = nowpack;
|
||||
}
|
||||
if (pack["offset"].asInt() != 0){
|
||||
bps = pack["offset"].asInt();
|
||||
if (bps < bfrm_min){bfrm_min = bps;}
|
||||
if (bps > bfrm_max){bfrm_max = bps;}
|
||||
}
|
||||
totalvideo += pack["data"].asString().size();
|
||||
lastvideo = nowpack;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::endl << "Summary:" << std::endl;
|
||||
meta["length"] = (long long int)((nowpack - firstpack)/1000);
|
||||
if (meta.isMember("audio")){
|
||||
meta["audio"]["bps"] = (long long int)(totalaudio / ((lastaudio - firstpack) / 1000));
|
||||
std::cout << " Audio: " << meta["audio"]["codec"].asString() << std::endl;
|
||||
std::cout << " Bitrate: " << meta["audio"]["bps"].asInt() << std::endl;
|
||||
}
|
||||
if (meta.isMember("video")){
|
||||
meta["video"]["bps"] = (long long int)(totalvideo / ((lastvideo - firstpack) / 1000));
|
||||
meta["video"]["keyms"] = (long long int)((lastvideo - firstpack) / keyframes);
|
||||
if (meta["video"]["keyms"].asInt() - key_min > key_max - meta["video"]["keyms"].asInt()){
|
||||
meta["video"]["keyvar"] = (long long int)(meta["video"]["keyms"].asInt() - key_min);
|
||||
}else{
|
||||
meta["video"]["keyvar"] = (long long int)(key_max - meta["video"]["keyms"].asInt());
|
||||
}
|
||||
std::cout << " Video: " << meta["video"]["codec"].asString() << std::endl;
|
||||
std::cout << " Bitrate: " << meta["video"]["bps"].asInt() << std::endl;
|
||||
std::cout << " Keyframes: " << meta["video"]["keyms"].asInt() << "~" << meta["video"]["keyvar"].asInt() << std::endl;
|
||||
std::cout << " B-frames: " << bfrm_min << " - " << bfrm_max << std::endl;
|
||||
}
|
||||
|
||||
std::cerr << "Re-writing header..." << std::endl;
|
||||
|
||||
loader = meta.toPacked();
|
||||
if (F.writeHeader(loader)){
|
||||
return 0;
|
||||
}else{
|
||||
return -1;
|
||||
}
|
||||
}//DTSCFix
|
||||
|
||||
};
|
||||
|
||||
/// Entry point for FLV2DTSC, simply calls Converters::FLV2DTSC().
|
||||
int main(int argc, char ** argv){
|
||||
Util::Config conf = Util::Config(argv[0], PACKAGE_VERSION);
|
||||
conf.addOption("filename", JSON::fromString("{\"arg_num\":1, \"arg\":\"string\", \"help\":\"Filename of the file to attempt to fix.\"}"));
|
||||
conf.parseArgs(argc, argv);
|
||||
return Converters::DTSCFix(conf);
|
||||
}//main
|
|
@ -10,9 +10,10 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <mist/flv_tag.h> //FLV support
|
||||
#include <mist/dtsc.h> //DTSC support
|
||||
#include <mist/amf.h> //AMF support
|
||||
#include <mist/flv_tag.h>
|
||||
#include <mist/dtsc.h>
|
||||
#include <mist/json.h>
|
||||
#include <mist/amf.h>
|
||||
#include <mist/config.h>
|
||||
|
||||
/// Holds all code that converts filetypes to/from to DTSC.
|
||||
|
@ -21,41 +22,46 @@ namespace Converters{
|
|||
/// Reads FLV from STDIN, outputs DTSC to STDOUT.
|
||||
int FLV2DTSC() {
|
||||
FLV::Tag FLV_in; // Temporary storage for incoming FLV data.
|
||||
DTSC::DTMI meta_out; // Storage for outgoing DTMI header data.
|
||||
DTSC::DTMI pack_out; // Storage for outgoing DTMI data.
|
||||
JSON::Value meta_out; // Storage for outgoing header data.
|
||||
JSON::Value pack_out; // Storage for outgoing data.
|
||||
std::stringstream prebuffer; // Temporary buffer before sending real data
|
||||
bool sending = false;
|
||||
unsigned int counter = 0;
|
||||
|
||||
while (!feof(stdin)){
|
||||
if (FLV_in.FileLoader(stdin)){
|
||||
pack_out = FLV_in.toDTSC(meta_out);
|
||||
if (pack_out.isEmpty()){continue;}
|
||||
pack_out = FLV_in.toJSON(meta_out);
|
||||
if (pack_out.isNull()){continue;}
|
||||
if (!sending){
|
||||
counter++;
|
||||
if (counter > 8){
|
||||
sending = true;
|
||||
meta_out.Pack(true);
|
||||
meta_out.packed.replace(0, 4, DTSC::Magic_Header);
|
||||
std::cout << meta_out.packed;
|
||||
std::string packed_header = meta_out.toPacked();
|
||||
unsigned int size = htonl(packed_header.size());
|
||||
std::cout << std::string(DTSC::Magic_Header, 4) << std::string((char*)&size, 4) << packed_header;
|
||||
std::cout << prebuffer.rdbuf();
|
||||
prebuffer.str("");
|
||||
std::cerr << "Buffer done, starting real-time output..." << std::endl;
|
||||
}else{
|
||||
prebuffer << pack_out.Pack(true);//buffer
|
||||
std::string packed_out = pack_out.toPacked();
|
||||
unsigned int size = htonl(packed_out.size());
|
||||
prebuffer << std::string(DTSC::Magic_Packet, 4) << std::string((char*)&size, 4) << packed_out;
|
||||
continue;//don't also write
|
||||
}
|
||||
}
|
||||
std::cout << pack_out.Pack(true);//simply write
|
||||
//simply write
|
||||
std::string packed_out = pack_out.toPacked();
|
||||
unsigned int size = htonl(packed_out.size());
|
||||
std::cout << std::string(DTSC::Magic_Packet, 4) << std::string((char*)&size, 4) << packed_out;
|
||||
}
|
||||
}
|
||||
|
||||
// if the FLV input is very short, do output it correctly...
|
||||
if (!sending){
|
||||
std::cerr << "EOF - outputting buffer..." << std::endl;
|
||||
meta_out.Pack(true);
|
||||
meta_out.packed.replace(0, 4, DTSC::Magic_Header);
|
||||
std::cout << meta_out.packed;
|
||||
std::string packed_header = meta_out.toPacked();
|
||||
unsigned int size = htonl(packed_header.size());
|
||||
std::cout << std::string(DTSC::Magic_Header, 4) << std::string((char*)&size, 4) << packed_header;
|
||||
std::cout << prebuffer.rdbuf();
|
||||
}
|
||||
std::cerr << "Done!" << std::endl;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue