Skip to content

Commit

Permalink
合并时更新时间
Browse files Browse the repository at this point in the history
  • Loading branch information
orestonce committed Oct 8, 2024
1 parent a339d2c commit fe3a8a8
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 61 deletions.
5 changes: 4 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
47 changes: 24 additions & 23 deletions m3u8d-qt/m3u8d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,44 +773,45 @@ 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);
MergeTsDir_Resp retValue;
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++;
Expand Down
2 changes: 1 addition & 1 deletion m3u8d-qt/m3u8d.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion m3u8d-qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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";
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 17 additions & 22 deletions m3u8d-qt/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -291,29 +291,8 @@
</widget>
<widget class="QWidget" name="page_2">
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QGridLayout" name="gridLayout_3" columnstretch="0,1,0">
<property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum>
</property>
<property name="spacing">
<number>0</number>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="label_7">
<property name="sizePolicy">
Expand Down Expand Up @@ -420,6 +399,22 @@
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string/>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QCheckBox" name="checkBox_UseFirstTsMTime">
<property name="text">
<string>使用第一个ts文件的修改时间作为输出mp4文件的创建时间</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
Expand Down
5 changes: 4 additions & 1 deletion m3u8dcpp/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 5 additions & 10 deletions mp4time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fe3a8a8

Please sign in to comment.