From fe3a8a8184e07a14859144caccd68fb135712627 Mon Sep 17 00:00:00 2001 From: orestonce Date: Tue, 8 Oct 2024 22:07:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E6=97=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.go | 5 ++++- cmd/main.go | 9 ++++++-- m3u8d-qt/m3u8d.cpp | 47 +++++++++++++++++++++-------------------- m3u8d-qt/m3u8d.h | 2 +- m3u8d-qt/mainwindow.cpp | 7 +++++- m3u8d-qt/mainwindow.ui | 39 +++++++++++++++------------------- m3u8dcpp/merge.go | 5 ++++- mp4time.go | 15 +++++-------- 8 files changed, 68 insertions(+), 61 deletions(-) diff --git a/api.go b/api.go index baf1544..afc391a 100644 --- a/api.go +++ b/api.go @@ -295,7 +295,10 @@ func (this *DownloadEnv) runDownload(req StartDownload_Req, skipInfo SkipTsInfo) return } if req.UseServerSideTime && len(tsFileList) > 0 { - if this.updateMp4Time(tsFileList[0], name) == false { + this.logToFile("更新mp4时间") + err = UpdateMp4Time(tsFileList[0], name) + if err != nil { + this.setErrMsg("更新mp4文件时间失败: " + err.Error()) return } } diff --git a/cmd/main.go b/cmd/main.go index 4a25e73..c961675 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -68,8 +68,9 @@ var curlCmd = &cobra.Command{ var gRunReq m3u8d.StartDownload_Req var gMergeReq struct { - InputTsDir string - OutputMp4Name string + InputTsDir string + OutputMp4Name string + UseFirstTsMTime bool } var mergeCmd = &cobra.Command{ @@ -114,6 +115,9 @@ var mergeCmd = &cobra.Command{ Ctx: context.Background(), Status: status, }) + if err == nil && gMergeReq.UseFirstTsMTime { + err = m3u8d.UpdateMp4Time(tsFileList[0], gMergeReq.OutputMp4Name) + } if err != nil { log.Fatalln("合并失败", err) return @@ -141,6 +145,7 @@ func init() { rootCmd.AddCommand(curlCmd) mergeCmd.Flags().StringVarP(&gMergeReq.InputTsDir, "InputTsDir", "", "", "存放ts文件的目录(默认为当前工作目录)") mergeCmd.Flags().StringVarP(&gMergeReq.OutputMp4Name, "OutputMp4Name", "", "", "输出mp4文件名(默认为输入ts文件的目录下的all.mp4)") + mergeCmd.Flags().BoolVarP(&gMergeReq.UseFirstTsMTime, "UseFirstTsMTime", "", false, "使用第一个ts文件的修改时间作为输出mp4文件的创建时间") rootCmd.AddCommand(mergeCmd) rootCmd.Version = m3u8d.GetVersion() } diff --git a/m3u8d-qt/m3u8d.cpp b/m3u8d-qt/m3u8d.cpp index c31cb4e..85a9135 100644 --- a/m3u8d-qt/m3u8d.cpp +++ b/m3u8d-qt/m3u8d.cpp @@ -773,28 +773,29 @@ std::string GetFileNameFromUrl(std::string in0){ return retValue; } -MergeTsDir_Resp MergeTsDir(std::string in0, std::string in1){ +MergeTsDir_Resp MergeTsDir(std::string in0, std::string in1, bool in2){ std::string in; { - uint32_t tmp9 = in0.length(); - char tmp10[4]; - tmp10[0] = (uint32_t(tmp9) >> 24) & 0xFF; - tmp10[1] = (uint32_t(tmp9) >> 16) & 0xFF; - tmp10[2] = (uint32_t(tmp9) >> 8) & 0xFF; - tmp10[3] = (uint32_t(tmp9) >> 0) & 0xFF; - in.append(tmp10, 4); + uint32_t tmp10 = in0.length(); + char tmp11[4]; + tmp11[0] = (uint32_t(tmp10) >> 24) & 0xFF; + tmp11[1] = (uint32_t(tmp10) >> 16) & 0xFF; + tmp11[2] = (uint32_t(tmp10) >> 8) & 0xFF; + tmp11[3] = (uint32_t(tmp10) >> 0) & 0xFF; + in.append(tmp11, 4); in.append(in0); } { - uint32_t tmp11 = in1.length(); - char tmp12[4]; - tmp12[0] = (uint32_t(tmp11) >> 24) & 0xFF; - tmp12[1] = (uint32_t(tmp11) >> 16) & 0xFF; - tmp12[2] = (uint32_t(tmp11) >> 8) & 0xFF; - tmp12[3] = (uint32_t(tmp11) >> 0) & 0xFF; - in.append(tmp12, 4); + uint32_t tmp12 = in1.length(); + char tmp13[4]; + tmp13[0] = (uint32_t(tmp12) >> 24) & 0xFF; + tmp13[1] = (uint32_t(tmp12) >> 16) & 0xFF; + tmp13[2] = (uint32_t(tmp12) >> 8) & 0xFF; + tmp13[3] = (uint32_t(tmp12) >> 0) & 0xFF; + in.append(tmp13, 4); in.append(in1); } + in.append((char*)(&in2), 1); char *out = NULL; int outLen = 0; Go2cppFn_MergeTsDir((char *)in.data(), in.length(), &out, &outLen); @@ -802,15 +803,15 @@ MergeTsDir_Resp MergeTsDir(std::string in0, std::string in1){ int outIdx = 0; { { - uint32_t tmp13 = 0; - uint32_t tmp14 = uint32_t(uint8_t(out[outIdx+0]) << 24); - uint32_t tmp15 = uint32_t(uint8_t(out[outIdx+1]) << 16); - uint32_t tmp16 = uint32_t(uint8_t(out[outIdx+2]) << 8); - uint32_t tmp17 = uint32_t(uint8_t(out[outIdx+3]) << 0); - tmp13 = tmp14 | tmp15 | tmp16 | tmp17; + uint32_t tmp14 = 0; + uint32_t tmp15 = uint32_t(uint8_t(out[outIdx+0]) << 24); + uint32_t tmp16 = uint32_t(uint8_t(out[outIdx+1]) << 16); + uint32_t tmp17 = uint32_t(uint8_t(out[outIdx+2]) << 8); + uint32_t tmp18 = uint32_t(uint8_t(out[outIdx+3]) << 0); + tmp14 = tmp15 | tmp16 | tmp17 | tmp18; outIdx+=4; - retValue.ErrMsg = std::string(out+outIdx, out+outIdx+tmp13); - outIdx+=tmp13; + retValue.ErrMsg = std::string(out+outIdx, out+outIdx+tmp14); + outIdx+=tmp14; } retValue.IsCancel = (bool) out[outIdx]; outIdx++; diff --git a/m3u8d-qt/m3u8d.h b/m3u8d-qt/m3u8d.h index 62de7b9..11cd202 100644 --- a/m3u8d-qt/m3u8d.h +++ b/m3u8d-qt/m3u8d.h @@ -55,7 +55,7 @@ struct MergeTsDir_Resp{ bool IsCancel; MergeTsDir_Resp(): IsCancel(false){} }; -MergeTsDir_Resp MergeTsDir(std::string in0, std::string in1); +MergeTsDir_Resp MergeTsDir(std::string in0, std::string in1, bool in2); void MergeStop(); struct MergeGetProgressPercent_Resp{ int32_t Percent; diff --git a/m3u8d-qt/mainwindow.cpp b/m3u8d-qt/mainwindow.cpp index f8fefac..dfd267d 100644 --- a/m3u8d-qt/mainwindow.cpp +++ b/m3u8d-qt/mainwindow.cpp @@ -161,11 +161,12 @@ void MainWindow::on_pushButton_startMerge_clicked() if(fileName.isEmpty()) fileName = ui->lineEdit_mergeFileName->placeholderText(); QString dir = ui->lineEdit_mergeDir->text(); + bool UseFirstTsMTime = ui->checkBox_UseFirstTsMTime->isChecked(); this->updateMergeUi(true); m_syncUi.AddRunFnOn_OtherThread([=](){ - auto resp = MergeTsDir(dir.toStdString(), fileName.toStdString()); + auto resp = MergeTsDir(dir.toStdString(), fileName.toStdString(), UseFirstTsMTime); m_syncUi.AddRunFnOn_UiThread([=](){ this->updateMergeUi(false); @@ -227,6 +228,7 @@ void MainWindow::updateMergeUi(bool runing) ui->pushButton_startMerge->setEnabled(!runing); ui->lineEdit_mergeFileName->setEnabled(!runing); ui->pushButton_returnDownload->setEnabled(!runing); + ui->checkBox_UseFirstTsMTime->setEnabled(!runing); } static const QString configPath = "m3u8d_config.json"; @@ -248,6 +250,7 @@ void MainWindow::saveUiConfig() obj["Skip_EXT_X_DISCONTINUITY"] = ui->checkBox_Skip_EXT_X_DISCONTINUITY->isChecked(); obj["DebugLog"] = ui->checkBox_DebugLog->isChecked(); obj["UseServerSideTime"] = ui->checkBox_UseServerSideTime->isChecked(); + obj["UseFirstTsMTime"] = ui->checkBox_UseFirstTsMTime->isChecked(); QJsonDocument doc; doc.setObject(obj); @@ -315,6 +318,8 @@ void MainWindow::loadUiConfig() ui->checkBox_DebugLog->setChecked(debugLog); bool useServerSideTime = obj["UseServerSideTime"].toBool(); ui->checkBox_UseServerSideTime->setChecked(useServerSideTime); + bool UseFirstTsMTime = obj["UseFirstTsMTime"].toBool(); + ui->checkBox_UseFirstTsMTime->setChecked(UseFirstTsMTime); } void MainWindow::closeEvent(QCloseEvent *event) diff --git a/m3u8d-qt/mainwindow.ui b/m3u8d-qt/mainwindow.ui index 2ac98fc..9263794 100644 --- a/m3u8d-qt/mainwindow.ui +++ b/m3u8d-qt/mainwindow.ui @@ -291,29 +291,8 @@ - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - QLayout::SetMaximumSize - - - 0 - + @@ -420,6 +399,22 @@ + + + + + + + + + + 使用第一个ts文件的修改时间作为输出mp4文件的创建时间 + + + + + + diff --git a/m3u8dcpp/merge.go b/m3u8dcpp/merge.go index a748c55..cea167a 100644 --- a/m3u8dcpp/merge.go +++ b/m3u8dcpp/merge.go @@ -30,7 +30,7 @@ func beginMerge() bool { return true } -func MergeTsDir(InputTsDir string, OutputMp4Name string) (resp MergeTsDir_Resp) { +func MergeTsDir(InputTsDir string, OutputMp4Name string, UseFirstTsMTime bool) (resp MergeTsDir_Resp) { if !beginMerge() { return resp } @@ -78,6 +78,9 @@ func MergeTsDir(InputTsDir string, OutputMp4Name string) (resp MergeTsDir_Resp) Ctx: ctx, Status: &gMergeStatus, }) + if err == nil && UseFirstTsMTime { + err = m3u8d.UpdateMp4Time(tsFileList[0], OutputMp4Name) + } if err != nil { resp.ErrMsg = "合并错误: " + err.Error() resp.IsCancel = m3u8d.IsContextCancel(ctx) diff --git a/mp4time.go b/mp4time.go index f50028a..fb7a162 100644 --- a/mp4time.go +++ b/mp4time.go @@ -10,27 +10,22 @@ import ( "time" ) -func (this *DownloadEnv) updateMp4Time(firstTsName string, mp4FileName string) bool { +func UpdateMp4Time(firstTsName string, mp4FileName string) error { stat, err := os.Stat(firstTsName) if err != nil { - this.setErrMsg("读取文件状态失败: " + err.Error()) - return false + return errors.New("读取文件状态失败: " + err.Error()) } mTime := stat.ModTime() - this.logToFile("更新文件时间为:" + mTime.String()) err = updateMp4CreateTime(mp4FileName, mTime) if err != nil { - this.setErrMsg("更新mp4创建时间失败: " + err.Error()) - return false + return errors.New("更新mp4创建时间失败: " + err.Error()) } err = setft.SetFileTime(mp4FileName, mTime, mTime, mTime) if err != nil { - this.setErrMsg("更新文件时间属性失败: " + err.Error()) - return false + return errors.New("更新文件时间属性失败: " + err.Error()) } - this.logToFile("更新成功") - return true + return nil } func mov_tag(tag [4]byte) uint32 {