Skip to content

Commit

Permalink
Encoding force stop feature implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
vpoluyaktov committed Nov 17, 2023
1 parent 27388f2 commit 46f9122
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 32 deletions.
23 changes: 17 additions & 6 deletions internal/controller/buildController.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ func (c *BuildController) startBuild(cmd *dto.BuildCommand) {
go c.updateTotalProgress()
jd.Start()

c.stopFlag = true
c.mq.SendMessage(mq.BuildController, mq.Footer, &dto.SetBusyIndicator{Busy: false}, false)
c.mq.SendMessage(mq.BuildController, mq.Footer, &dto.UpdateStatus{Message: ""}, false)
c.mq.SendMessage(mq.BuildController, mq.BuildPage, &dto.BuildComplete{Audiobook: cmd.Audiobook}, true)
if !c.stopFlag {
c.mq.SendMessage(mq.BuildController, mq.BuildPage, &dto.BuildComplete{Audiobook: cmd.Audiobook}, true)
}
c.stopFlag = true
}

func (c *BuildController) createFilesLists(ab *dto.Audiobook) {
Expand Down Expand Up @@ -218,21 +220,30 @@ func (c *BuildController) buildAudiobookPart(ab *dto.Audiobook, partId int) {
logger.Error("FFMPEG Error: " + string(err.Error()))
} else {
// add Metadata, cover image and convert to .m4b
_, err := ffmpeg.NewFFmpeg().
ffmpeg := ffmpeg.NewFFmpeg().
Input(part.AACFile, "").
Input(part.MetadataFile, "").
Input(ab.CoverURL, "").
Output(part.M4BFile, "-map_metadata 1 -y -acodec copy -y -vf pad='width=ceil(iw/2)*2:height=ceil(ih/2)*2'").
Overwrite(true).
Params("-hide_banner -nostdin -nostats").
SendProgressTo("http://127.0.0.1:" + strconv.Itoa(port)).
Run()
if err != nil {
SendProgressTo("http://127.0.0.1:" + strconv.Itoa(port))

go c.killSwitch(ffmpeg)
_, err := ffmpeg.Run()
if err != nil && ! c.stopFlag {
logger.Error("FFMPEG Error: " + string(err.Error()))
}
}
}

func (c *BuildController) killSwitch(ffmpeg *ffmpeg.FFmpeg) {
for !c.stopFlag {
time.Sleep(mq.PullFrequency)
}
ffmpeg.Kill()
}

func (c *BuildController) startProgressListener(fileId int) (net.Listener, int) {

basePortNumber := 31000
Expand Down
5 changes: 4 additions & 1 deletion internal/controller/chaptersController.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ func (c *ChaptersController) createChapters(cmd *dto.ChaptersCreate) {

c.mq.SendMessage(mq.ChaptersController, mq.Footer, &dto.SetBusyIndicator{Busy: false}, false)
c.mq.SendMessage(mq.ChaptersController, mq.Footer, &dto.UpdateStatus{Message: ""}, false)
c.mq.SendMessage(mq.ChaptersController, mq.ChaptersPage, &dto.ChaptersReady{Audiobook: cmd.Audiobook}, true)
if !c.stopFlag {
c.mq.SendMessage(mq.ChaptersController, mq.ChaptersPage, &dto.ChaptersReady{Audiobook: cmd.Audiobook}, true)
}
c.stopFlag = true
}

func (c *ChaptersController) searchReplaceDescription(cmd *dto.SearchReplaceDescriptionCommand) {
Expand Down
6 changes: 4 additions & 2 deletions internal/controller/copyController.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ func (c *CopyController) startCopy(cmd *dto.CopyCommand) {
go c.updateTotalCopyProgress()
jd.Start()

c.stopFlag = true
c.mq.SendMessage(mq.CopyController, mq.Footer, &dto.SetBusyIndicator{Busy: false}, false)
c.mq.SendMessage(mq.CopyController, mq.Footer, &dto.UpdateStatus{Message: ""}, false)
c.mq.SendMessage(mq.CopyController, mq.BuildPage, &dto.CopyComplete{Audiobook: cmd.Audiobook}, true)
if !c.stopFlag {
c.mq.SendMessage(mq.CopyController, mq.BuildPage, &dto.CopyComplete{Audiobook: cmd.Audiobook}, true)
}
c.stopFlag = true
}

func (c *CopyController) stopCopy(cmd *dto.StopCommand) {
Expand Down
9 changes: 4 additions & 5 deletions internal/controller/downloadController.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,15 @@ func (c *DownloadController) startDownload(cmd *dto.DownloadCommand) {
jd.AddJob(i, ia.DownloadFile, c.ab.OutputDir, localFileName, item.Server, item.Dir, iaFile.Name, i, iaFile.Size, c.updateFileProgress)
}
go c.updateTotalProgress()
// if c.stopFlag {
// break
// }

jd.Start()

c.stopFlag = true
c.mq.SendMessage(mq.DownloadController, mq.Footer, &dto.SetBusyIndicator{Busy: false}, false)
c.mq.SendMessage(mq.DownloadController, mq.Footer, &dto.UpdateStatus{Message: ""}, false)
c.mq.SendMessage(mq.DownloadController, mq.DownloadPage, &dto.DownloadComplete{Audiobook: cmd.Audiobook}, true)
if ! c.stopFlag {
c.mq.SendMessage(mq.DownloadController, mq.DownloadPage, &dto.DownloadComplete{Audiobook: cmd.Audiobook}, true)
}
c.stopFlag = true
}

func (c *DownloadController) updateFileProgress(fileId int, fileName string, size int64, pos int64, percent int) {
Expand Down
23 changes: 17 additions & 6 deletions internal/controller/encodingController.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ func (c *EncodingController) startEncoding(cmd *dto.EncodeCommand) {
go c.updateTotalProgress()
jd.Start()

c.stopFlag = true
c.mq.SendMessage(mq.EncodingController, mq.Footer, &dto.SetBusyIndicator{Busy: false}, false)
c.mq.SendMessage(mq.EncodingController, mq.Footer, &dto.UpdateStatus{Message: ""}, false)
c.mq.SendMessage(mq.EncodingController, mq.EncodingPage, &dto.EncodingComplete{Audiobook: cmd.Audiobook}, true)
if !c.stopFlag {
c.mq.SendMessage(mq.EncodingController, mq.EncodingPage, &dto.EncodingComplete{Audiobook: cmd.Audiobook}, true)
}
c.stopFlag = true
}

func (c *EncodingController) encodeFile(fileId int, outputDir string) {
Expand All @@ -113,14 +115,16 @@ func (c *EncodingController) encodeFile(fileId int, outputDir string) {
go c.updateFileProgress(fileId, l)

// launch ffmpeg process
_, err := ffmpeg.NewFFmpeg().
ffmpeg := ffmpeg.NewFFmpeg().
Input(filePath, "-f mp3").
Output(tmpFile, fmt.Sprintf("-f mp3 -ab %dk -ar %d -vn", c.ab.Config.GetBitRate(), c.ab.Config.GetSampleRate())).
Overwrite(true).
Params("-hide_banner -nostdin -nostats -loglevel fatal").
SendProgressTo("http://127.0.0.1:" + strconv.Itoa(port)).
Run()
if err != nil {
SendProgressTo("http://127.0.0.1:" + strconv.Itoa(port))

go c.killSwitch(ffmpeg)
_, err := ffmpeg.Run()
if err != nil && !c.stopFlag {
logger.Error("FFMPEG Error: " + string(err.Error()))
} else {
err := os.Remove(filePath)
Expand All @@ -132,6 +136,13 @@ func (c *EncodingController) encodeFile(fileId int, outputDir string) {
}
}

func (c *EncodingController) killSwitch(ffmpeg *ffmpeg.FFmpeg) {
for !c.stopFlag {
time.Sleep(mq.PullFrequency)
}
ffmpeg.Kill()
}

func (c *EncodingController) startProgressListener(fileId int) (net.Listener, int) {
portNumber := config.Instance().GetBasePortNumber() + fileId
l, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(portNumber))
Expand Down
20 changes: 13 additions & 7 deletions internal/ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ffmpeg

import (
"os"
"os/exec"

"github.com/vpoluyaktov/abb_ia/internal/logger"
Expand All @@ -11,7 +10,7 @@ type FFmpeg struct {
input input
output output
params params
process *os.Process
cmd *exec.Cmd
}

type input struct {
Expand All @@ -33,7 +32,7 @@ func NewFFmpeg() *FFmpeg {
input: input{},
output: output{},
params: params{},
process: nil,
cmd: nil,
}
return ffmpeg
}
Expand Down Expand Up @@ -76,13 +75,20 @@ func (f *FFmpeg) Run() (string, *exitErr) {
args = args.AppendArgs("-i").AppendFileName(fileName)
}
args = args.AppendArgs(f.output.args).AppendFileName(f.output.fileName)
command := exec.Command(cmd, args.String()...)
logger.Debug("FFMPEG cmd: " + command.String())
f.process = command.Process
out, err := command.Output()
f.cmd = exec.Command(cmd, args.String()...)
logger.Debug("FFMPEG cmd: " + f.cmd.String())
out, err := f.cmd.Output()
if err != nil {
return string(out), ExitErr(err)
} else {
return string(out), nil
}
}

func (f *FFmpeg) Kill() error {
if f.cmd != nil && f.cmd.Process != nil {
return f.cmd.Process.Kill()
} else {
return nil
}
}
1 change: 0 additions & 1 deletion internal/ui/chaptersPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ func (p *ChaptersPage) stopConfirmation() {
}

func (p *ChaptersPage) stopChapters() {
// Stop the chapters here
p.mq.SendMessage(mq.ChaptersPage, mq.ChaptersController, &dto.StopCommand{Process: "Chapters", Reason: "User request"}, true)
p.mq.SendMessage(mq.ChaptersPage, mq.Frame, &dto.SwitchToPageCommand{Name: "SearchPage"}, true)
}
Expand Down
1 change: 0 additions & 1 deletion internal/ui/downloadPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func (p *DownloadPage) stopConfirmation() {
}

func (p *DownloadPage) stopDownload() {
// Stop the download here
p.mq.SendMessage(mq.DownloadPage, mq.DownloadController, &dto.StopCommand{Process: "Download", Reason: "User request"}, false)
p.mq.SendMessage(mq.DownloadPage, mq.Frame, &dto.SwitchToPageCommand{Name: "SearchPage"}, false)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/ui/encodingPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ func (p *EncodingPage) stopConfirmation() {
}

func (p *EncodingPage) stopEncoding() {
// Stop the encoding here
p.mq.SendMessage(mq.EncodingPage, mq.EncodingController, &dto.StopCommand{Process: "Encoding", Reason: "User request"}, false)
p.mq.SendMessage(mq.EncodingPage, mq.Frame, &dto.SwitchToPageCommand{Name: "SearchPage"}, false)
p.mq.SendMessage(mq.EncodingPage, mq.EncodingController, &dto.StopCommand{Process: "Encoding", Reason: "User request"}, true)
p.mq.SendMessage(mq.EncodingPage, mq.Frame, &dto.SwitchToPageCommand{Name: "SearchPage"}, true)
}

func (p *EncodingPage) updateFileProgress(dp *dto.EncodingFileProgress) {
Expand Down

0 comments on commit 46f9122

Please sign in to comment.