Refactored the code a bit so that the Transcoder object handles more of its own setup

This commit is contained in:
Gregory Ballantine 2022-09-03 19:51:37 -04:00
parent 674327e0cf
commit 611d0dba9a
3 changed files with 82 additions and 56 deletions

View File

@ -1,15 +1,10 @@
package main
import (
"log"
"path/filepath"
"time"
"github.com/spf13/viper"
"git.metaunix.net/BitGoblin/adept/config"
"git.metaunix.net/BitGoblin/adept/transcoder"
"git.metaunix.net/BitGoblin/adept/util"
)
func main() {
@ -25,31 +20,7 @@ func main() {
// initialize the video repository
r := transcoder.NewRepository(viper.GetString("transcoder.repository"))
// main program loop - runs infinitely until externally terminated
for {
ingestFiles := r.SearchIngest()
if len(ingestFiles) < 1 {
log.Println("There were no files found in ingest to transcode; skipping run.")
} else {
for _, i := range ingestFiles {
// check if the file is open in another program (e.g. still being written to)
if util.IsFileLocked(filepath.Join(viper.GetString("transcoder.repository"), "ingest", i.Name())) {
log.Printf("%s appears to be open in another program; skipping it for this run.", i.Name())
continue
}
// archive file
r.ArchiveFile(i.Name())
// transcode file
transcoder.Transcode(i.Name())
// clean up source file
r.CleanupFile(i.Name())
}
}
// sleep for X minutes - specified in the adept.toml config file
interval := viper.GetInt("transcoder.interval")
time.Sleep(time.Duration(interval) * time.Minute)
}
// create transcoder object and start the main loop
t := transcoder.NewTranscoder(*r)
t.Start()
}

View File

@ -8,31 +8,41 @@ import (
)
type Repository struct {
basePath string
basePath string
ingestPath string
archivePath string
outputPath string
}
func NewRepository(path string) *Repository {
// make sure repository base directory exists
create_repo_dir(path)
// make sure the ingest, archive, and output directories exist
subDirs := []string{"ingest", "archive", "output"}
for _, d := range subDirs {
subPath := filepath.Join(path, d)
create_repo_dir(subPath)
}
r := new(Repository)
r.basePath = path
r.ingestPath = filepath.Join(path, "ingest")
r.archivePath = filepath.Join(path, "archive")
r.outputPath = filepath.Join(path, "output")
// make sure repository base directory exists
create_repo_dir(r.basePath)
// make sure the folder structure is setup
create_repo_dir(r.ingestPath)
create_repo_dir(r.archivePath)
create_repo_dir(r.archivePath)
return r
}
func (r *Repository) SearchIngest() []os.FileInfo {
ingestPath := filepath.Join(r.basePath, "ingest")
// Repository getters
func (r *Repository) GetIngestPath() string {
return r.ingestPath
}
func (r *Repository) GetOutputPath() string {
return r.outputPath
}
func (r *Repository) SearchIngest() []os.FileInfo {
log.Printf("Searching ingest directory for files to transcode...")
ingestDir, err := os.Open(ingestPath)
ingestDir, err := os.Open(r.ingestPath)
if err != nil {
log.Fatalf("Error opening ingest directory: %s", err)
os.Exit(1)
@ -48,13 +58,13 @@ func (r *Repository) SearchIngest() []os.FileInfo {
func (r *Repository) ArchiveFile(inFile string) {
// create ingest and archive paths
ingestPath := filepath.Join(r.basePath, "ingest", inFile)
archivePath := filepath.Join(r.basePath, "archive", inFile)
ingestFilePath := filepath.Join(r.ingestPath, inFile)
archiveFilePath := filepath.Join(r.archivePath, inFile)
log.Printf("Copying %s to the archive.", ingestPath)
log.Printf("Copying %s to the archive.", ingestFilePath)
// open ingest file
source, err := os.Open(ingestPath)
source, err := os.Open(ingestFilePath)
if err != nil {
log.Fatalf("Error opening file in ingest: %s.", err)
os.Exit(1)
@ -62,7 +72,7 @@ func (r *Repository) ArchiveFile(inFile string) {
defer source.Close()
// attempt to create destination file
destination, err := os.Create(archivePath)
destination, err := os.Create(archiveFilePath)
if err != nil {
log.Fatalf("Error opening archive file: %s.", err)
os.Exit(1)
@ -80,10 +90,10 @@ func (r *Repository) ArchiveFile(inFile string) {
func (r *Repository) CleanupFile(inFile string) {
// create ingest path
ingestPath := filepath.Join(r.basePath, "ingest", inFile)
ingestFilePath := filepath.Join(r.ingestPath, inFile)
// remove the file
os.Remove(ingestPath)
os.Remove(ingestFilePath)
}
func create_repo_dir(path string) {

View File

@ -6,14 +6,59 @@ import (
"os"
"os/exec"
"path/filepath"
"time"
"github.com/spf13/viper"
"git.metaunix.net/BitGoblin/adept/util"
)
func Transcode(inFile string) {
type Transcoder struct {
repo Repository
}
// Transcoder object constructor
func NewTranscoder(r Repository) *Transcoder {
t := new(Transcoder)
t.repo = r
return t
}
// Start the main transcoder loop
func (t *Transcoder) Start() {
// main program loop - runs infinitely until externally terminated
for {
ingestFiles := t.repo.SearchIngest()
if len(ingestFiles) < 1 {
log.Println("There were no files found in ingest to transcode; skipping run.")
} else {
for _, i := range ingestFiles {
// check if the file is open in another program (e.g. still being written to)
if util.IsFileLocked(filepath.Join(t.repo.GetIngestPath(), i.Name())) {
log.Printf("%s appears to be open in another program; skipping it for this run.", i.Name())
continue
}
// archive file
t.repo.ArchiveFile(i.Name())
// transcode file
t.Transcode(i.Name())
// clean up source file
t.repo.CleanupFile(i.Name())
}
}
// sleep for X minutes - specified in the adept.toml config file
interval := viper.GetInt("transcoder.interval")
time.Sleep(time.Duration(interval) * time.Minute)
}
}
func (t *Transcoder) Transcode(inFile string) {
// create ingest and archive paths
ingestPath := filepath.Join(viper.GetString("transcoder.repository"), "ingest", inFile)
outputPath := filepath.Join(viper.GetString("transcoder.repository"), "output", inFile)
ingestPath := filepath.Join(t.repo.GetIngestPath(), inFile)
outputPath := filepath.Join(t.repo.GetOutputPath(), inFile)
log.Printf("Transcoding video file %s.", ingestPath)