Compare commits

..

No commits in common. "076947ebf93e18f933fbd42d7bb01db695e959e5" and "674327e0cfcf9282b59471d2018914b092655b38" have entirely different histories.

3 changed files with 56 additions and 84 deletions

View File

@ -1,10 +1,15 @@
package main package main
import ( import (
"log"
"path/filepath"
"time"
"github.com/spf13/viper" "github.com/spf13/viper"
"git.metaunix.net/BitGoblin/adept/config" "git.metaunix.net/BitGoblin/adept/config"
"git.metaunix.net/BitGoblin/adept/transcoder" "git.metaunix.net/BitGoblin/adept/transcoder"
"git.metaunix.net/BitGoblin/adept/util"
) )
func main() { func main() {
@ -20,7 +25,31 @@ func main() {
// initialize the video repository // initialize the video repository
r := transcoder.NewRepository(viper.GetString("transcoder.repository")) r := transcoder.NewRepository(viper.GetString("transcoder.repository"))
// create transcoder object and start the main loop // main program loop - runs infinitely until externally terminated
t := transcoder.NewTranscoder(*r) for {
t.Start() 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)
}
} }

View File

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

View File

@ -6,61 +6,14 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"time"
"github.com/spf13/viper" "github.com/spf13/viper"
"git.metaunix.net/BitGoblin/adept/util"
) )
type Transcoder struct { func Transcode(inFile string) {
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 // create ingest and archive paths
ingestPath := filepath.Join(t.repo.GetIngestPath(), inFile) ingestPath := filepath.Join(viper.GetString("transcoder.repository"), "ingest", inFile)
outputName := strings.Join([]string{util.FilenameWithoutExtension(inFile), viper.GetString("transcoder.video_format")}, ".") outputPath := filepath.Join(viper.GetString("transcoder.repository"), "output", inFile)
outputPath := filepath.Join(t.repo.GetOutputPath(), outputName)
log.Printf("Transcoding video file %s.", ingestPath) log.Printf("Transcoding video file %s.", ingestPath)