Commit e7062267 authored by Linshizhi's avatar Linshizhi

new api: addaudioBuffer and mergeMP4.

parent a39b6827
...@@ -2,3 +2,9 @@ ...@@ -2,3 +2,9 @@
path = extensions/loadablemodule path = extensions/loadablemodule
url = git@gitlab.laihua.com:linshizhi/loadablemodule.git url = git@gitlab.laihua.com:linshizhi/loadablemodule.git
branch = main branch = main
[submodule "extensions/--force"]
path = extensions/--force
url = git@gitlab.laihua.com:linshizhi/encoder.git
[submodule "extensions/encoder"]
path = extensions/encoder
url = git@gitlab.laihua.com:linshizhi/encoder.git
Subproject commit acf7b7681996005565b9633d99ea9a69d248eb12 Subproject commit d7cbcc5acfbb09311ca09ec74dd1bf40ad685fa5
Subproject commit 7123dbdba735a1b43bfd586e2110e25bcdaab9ed
...@@ -626,6 +626,8 @@ namespace internal { ...@@ -626,6 +626,8 @@ namespace internal {
CPP(Close) \ CPP(Close) \
CPP(GetVideo) \ CPP(GetVideo) \
CPP(Save) \ CPP(Save) \
CPP(AddAudioBuffer) \
CPP(MergeMP4) \
\ \
/* Web snapshots */ \ /* Web snapshots */ \
CPP(WebSnapshotSerialize) \ CPP(WebSnapshotSerialize) \
......
...@@ -15,6 +15,7 @@ namespace internal { ...@@ -15,6 +15,7 @@ namespace internal {
namespace { namespace {
bool inited = false; bool inited = false;
LAIPIC_ENCODER::Encoder *enc; LAIPIC_ENCODER::Encoder *enc;
char *audioPath;
} }
BUILTIN(InitEncodeContext) { BUILTIN(InitEncodeContext) {
...@@ -140,6 +141,7 @@ BUILTIN(Close) { ...@@ -140,6 +141,7 @@ BUILTIN(Close) {
return *isolate->factory()->ToBoolean(true); return *isolate->factory()->ToBoolean(true);
} }
std::remove(enc->getOutputPath().c_str());
enc->close(); enc->close();
enc = nullptr; enc = nullptr;
inited = false; inited = false;
...@@ -174,8 +176,6 @@ BUILTIN(GetVideo) { ...@@ -174,8 +176,6 @@ BUILTIN(GetVideo) {
videoFile.read(buffer, fileLength); videoFile.read(buffer, fileLength);
videoFile.close(); videoFile.close();
std::remove(enc->getOutputPath().c_str());
return *videoContent; return *videoContent;
} }
...@@ -222,6 +222,72 @@ BUILTIN(Save) { ...@@ -222,6 +222,72 @@ BUILTIN(Save) {
return *isolate->factory()->ToBoolean(true); return *isolate->factory()->ToBoolean(true);
} }
BUILTIN(AddAudioBuffer) {
HandleScope scope(isolate);
Handle<JSTypedArray> array;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, array,
JSTypedArray::Validate(isolate, args.atOrUndefined(isolate, 1), "ENCODER.encode()"));
Handle<JSArrayBuffer> arrayBuffer = array->GetBuffer();
std::shared_ptr<BackingStore> store = arrayBuffer->GetBackingStore();
uint8_t* buffer = (uint8_t*)store->buffer_start();
int bufferLen = static_cast<int>(store->byte_length());
std::string outpath = std::tmpnam(nullptr);
outpath += ".aac";
audioPath = strdup(outpath.c_str());
std::ofstream audioFile { outpath };
audioFile.write(reinterpret_cast<char*>(buffer), bufferLen);
audioFile.close();
return ReadOnlyRoots(isolate).undefined_value();
}
BUILTIN(MergeMP4) {
HandleScope scope(isolate);
Handle<Object> libpath = args.atOrUndefined(isolate, 1);
// Libav path
std::string libpath_cppstr;
if (libpath->IsUndefined()) {
libpath_cppstr = "";
} else {
Handle<String> libpath_str;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, libpath_str, Object::ToString(isolate, libpath));
libpath_cppstr = libpath_str->ToCString().get();
}
Handle<Object> outpath = args.atOrUndefined(isolate, 2);
std::string outpath_cppstr;
if (outpath->IsUndefined()) {
outpath_cppstr = std::tmpnam(nullptr);
outpath_cppstr += ".mp4";
} else {
Handle<String> outpath_str;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, outpath_str, Object::ToString(isolate, outpath));
outpath_cppstr = outpath_str->ToCString().get();
}
LAIPIC_ENCODER::AVMerger merger(libpath_cppstr, outpath_cppstr, enc->getOutputPath(), audioPath);
merger.merge();
std::remove(audioPath);
Handle<String> mergepath = isolate->factory()->NewStringFromAsciiChecked(outpath_cppstr.c_str());
free(audioPath);
audioPath = nullptr;
return *mergepath;
}
} // internal } // internal
} // v8 } // v8
...@@ -2804,6 +2804,10 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object, ...@@ -2804,6 +2804,10 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
Builtin::kGetVideo, 0, true); Builtin::kGetVideo, 0, true);
SimpleInstallFunction(isolate_, mp4, "save", SimpleInstallFunction(isolate_, mp4, "save",
Builtin::kSave, 2, true); Builtin::kSave, 2, true);
SimpleInstallFunction(isolate_, mp4, "addAudioBuffer",
Builtin::kAddAudioBuffer, 1, true);
SimpleInstallFunction(isolate_, mp4, "mergeMP4",
Builtin::kMergeMP4, 2, true);
InstallToStringTag(isolate_, media_codex, "MediaCodex"); InstallToStringTag(isolate_, media_codex, "MediaCodex");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment