multithread
This commit is contained in:
parent
c263ccbda0
commit
204c73d46c
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/akamensky/argparse"
|
"github.com/akamensky/argparse"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -16,6 +17,7 @@ type CopyCommandArgs struct {
|
||||||
Source *string
|
Source *string
|
||||||
Dest *string
|
Dest *string
|
||||||
TranscodePreset *string
|
TranscodePreset *string
|
||||||
|
Threads *int
|
||||||
SkipExisting *bool
|
SkipExisting *bool
|
||||||
SingleDirectory *bool
|
SingleDirectory *bool
|
||||||
}
|
}
|
||||||
|
@ -107,6 +109,10 @@ func RegisterCopyCommand(parser *argparse.Parser) (*argparse.Command, *CopyComma
|
||||||
Default: false,
|
Default: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
arguments.Threads = cmd.Int("", "threads", &argparse.Options{
|
||||||
|
Default: 0,
|
||||||
|
})
|
||||||
|
|
||||||
return cmd, arguments
|
return cmd, arguments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,10 +215,33 @@ func (c *CopyCommand) transcode_files() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Could not get transcode preset")
|
log.Fatal().Err(err).Msg("Could not get transcode preset")
|
||||||
}
|
}
|
||||||
|
if *c.Args.Threads == 0 {
|
||||||
|
for _, file := range c.Files {
|
||||||
|
c._transcode_file(file, trans_preset.Config)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
threads := *c.Args.Threads
|
||||||
|
jobs := make(chan *types.File, threads)
|
||||||
|
|
||||||
for _, file := range c.Files {
|
for i := 1; i <= threads; i++ {
|
||||||
c._transcode_file(file, trans_preset.Config)
|
go func() {
|
||||||
|
for file := range jobs {
|
||||||
|
defer wg.Done()
|
||||||
|
c._transcode_file(file, trans_preset.Config)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range c.Files {
|
||||||
|
wg.Add(1)
|
||||||
|
jobs <- file
|
||||||
|
}
|
||||||
|
|
||||||
|
close(jobs)
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CopyCommand) _copy_file(file *types.File) {
|
func (c *CopyCommand) _copy_file(file *types.File) {
|
||||||
|
|
Loading…
Reference in a new issue