tidying up a *lot*; add QoL options
This commit is contained in:
parent
84de96df31
commit
2954689784
6 changed files with 261 additions and 154 deletions
56
config/config.go
Normal file
56
config/config.go
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Config struct {
|
||||||
|
Google GoogleConfig `toml:"google"`
|
||||||
|
}
|
||||||
|
|
||||||
|
GoogleConfig struct {
|
||||||
|
ApiKey string `toml:"api_key"`
|
||||||
|
ClientID string `toml:"client_id"`
|
||||||
|
ClientSecret string `toml:"client_secret"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var defaultConfig = Config{
|
||||||
|
Google: GoogleConfig{
|
||||||
|
ApiKey: "<your API key here>",
|
||||||
|
ClientID: "<your client ID here>",
|
||||||
|
ClientSecret: "<your client secret here>",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const CONFIG_FILENAME = "config.toml"
|
||||||
|
|
||||||
|
func ReadConfig(filename string) (*Config, error) {
|
||||||
|
cfgBytes, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
if err == os.ErrNotExist {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to open file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
config := Config{}
|
||||||
|
err = toml.Unmarshal(cfgBytes, &config)
|
||||||
|
if err != nil { return &config, fmt.Errorf("failed to parse: %v", err) }
|
||||||
|
|
||||||
|
return &config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateConfig(filename string) error {
|
||||||
|
file, err := os.OpenFile(filename, os.O_CREATE, 0644)
|
||||||
|
if err != nil { return err }
|
||||||
|
|
||||||
|
err = toml.NewEncoder(file).Encode(defaultConfig)
|
||||||
|
if err != nil { return err }
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
245
main.go
245
main.go
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
_ "embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -9,41 +10,26 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
toml "github.com/pelletier/go-toml/v2"
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/google"
|
"golang.org/x/oauth2/google"
|
||||||
"google.golang.org/api/youtube/v3"
|
"google.golang.org/api/youtube/v3"
|
||||||
|
|
||||||
|
"arimelody.space/live-vod-uploader/config"
|
||||||
"arimelody.space/live-vod-uploader/scanner"
|
"arimelody.space/live-vod-uploader/scanner"
|
||||||
vid "arimelody.space/live-vod-uploader/video"
|
vid "arimelody.space/live-vod-uploader/video"
|
||||||
yt "arimelody.space/live-vod-uploader/youtube"
|
yt "arimelody.space/live-vod-uploader/youtube"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
const segmentExtension = "mkv"
|
||||||
Config struct {
|
|
||||||
Google GoogleConfig `toml:"google"`
|
|
||||||
}
|
|
||||||
|
|
||||||
GoogleConfig struct {
|
//go:embed res/help.txt
|
||||||
ApiKey string `toml:"api_key"`
|
var helpText string
|
||||||
ClientID string `toml:"client_id"`
|
|
||||||
ClientSecret string `toml:"client_secret"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const CONFIG_FILENAME = "config.toml"
|
|
||||||
|
|
||||||
func showHelp() {
|
func showHelp() {
|
||||||
execSplits := strings.Split(os.Args[0], "/")
|
execSplits := strings.Split(os.Args[0], "/")
|
||||||
execName := execSplits[len(execSplits) - 1]
|
execName := execSplits[len(execSplits) - 1]
|
||||||
fmt.Printf(
|
fmt.Printf(helpText, execName)
|
||||||
"usage: %s [options] [directory]\n\n" +
|
}
|
||||||
"options:\n" +
|
|
||||||
"\t-h, --help: Show this help message.\n" +
|
|
||||||
"\t-v, --verbose: Show verbose logging output.\n" +
|
|
||||||
"\t--init: Initialise `directory` as a VOD directory.\n",
|
|
||||||
execName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) < 2 || os.Args[1] == "--help" || os.Args[1] == "-h" {
|
if len(os.Args) < 2 || os.Args[1] == "--help" || os.Args[1] == "-h" {
|
||||||
|
|
@ -51,9 +37,11 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
var directory string
|
|
||||||
var initDirectory bool = false
|
|
||||||
var verbose bool = false
|
var verbose bool = false
|
||||||
|
var initDirectory bool = false
|
||||||
|
var deleteFullVod bool = false
|
||||||
|
var forceUpload bool = false
|
||||||
|
var directory string
|
||||||
|
|
||||||
for i, arg := range os.Args {
|
for i, arg := range os.Args {
|
||||||
if i == 0 { continue }
|
if i == 0 { continue }
|
||||||
|
|
@ -66,14 +54,24 @@ func main() {
|
||||||
showHelp()
|
showHelp()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|
||||||
case "--init":
|
|
||||||
initDirectory = true
|
|
||||||
|
|
||||||
case "-v":
|
case "-v":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "--verbose":
|
case "--verbose":
|
||||||
verbose = true
|
verbose = true
|
||||||
|
|
||||||
|
case "--init":
|
||||||
|
initDirectory = true
|
||||||
|
|
||||||
|
case "-d":
|
||||||
|
fallthrough
|
||||||
|
case "-deleteAfter":
|
||||||
|
deleteFullVod = true
|
||||||
|
|
||||||
|
case "-f":
|
||||||
|
fallthrough
|
||||||
|
case "--force":
|
||||||
|
forceUpload = true
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(os.Stderr, "Unknown option `%s`\n", arg)
|
fmt.Fprintf(os.Stderr, "Unknown option `%s`\n", arg)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
@ -84,89 +82,68 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := Config{}
|
// config
|
||||||
cfgBytes, err := os.ReadFile(CONFIG_FILENAME)
|
cfg, err := config.ReadConfig(config.CONFIG_FILENAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to read config file: %v", err)
|
log.Fatalf("Failed to read config: %v", err)
|
||||||
|
|
||||||
tomlBytes, err := toml.Marshal(&cfg)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to marshal json: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.WriteFile(CONFIG_FILENAME, tomlBytes, 0o644)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to write config file: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("New config file created. Please edit this before running again!")
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
err = toml.Unmarshal(cfgBytes, &cfg)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to parse config: %v", err)
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
if cfg == nil {
|
||||||
if initDirectory {
|
log.Printf(
|
||||||
dirInfo, err := os.Stat(directory)
|
"New config file created (%s). " +
|
||||||
if err != nil {
|
"Please edit this file before running again!",
|
||||||
if err == os.ErrNotExist {
|
config.CONFIG_FILENAME,
|
||||||
log.Fatalf("No such directory: %s", directory)
|
)
|
||||||
os.Exit(1)
|
os.Exit(0)
|
||||||
}
|
|
||||||
log.Fatalf("Failed to open directory: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
if !dirInfo.IsDir() {
|
|
||||||
log.Fatalf("Not a directory: %s", directory)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
dirEntry, err := os.ReadDir(directory)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to open directory: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
for _, entry := range dirEntry {
|
|
||||||
if !entry.IsDir() && entry.Name() == "metadata.toml" {
|
|
||||||
log.Printf("Directory `%s` already initialised", directory)
|
|
||||||
os.Exit(0)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultMetadata := scanner.DefaultMetadata()
|
|
||||||
metadataStr, _ := toml.Marshal(defaultMetadata)
|
|
||||||
err = os.WriteFile(path.Join(directory, "metadata.toml"), metadataStr, 0o644)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to write to file: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
log.Printf("Directory successfully initialised")
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata, err := scanner.FetchMetadata(directory)
|
// initialising directory (--init)
|
||||||
|
if initDirectory {
|
||||||
|
err = initialiseDirectory(directory)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to initialise directory: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
log.Printf("Directory successfully initialised")
|
||||||
|
}
|
||||||
|
|
||||||
|
// read directory metadata
|
||||||
|
metadata, err := scanner.ReadMetadata(directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to fetch VOD metadata: %v", err)
|
log.Fatalf("Failed to fetch VOD metadata: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if metadata == nil {
|
if metadata == nil {
|
||||||
log.Fatal("Directory contained no metadata. Use `--init` to initialise this directory.")
|
log.Fatal(
|
||||||
|
"Directory contained no metadata. " +
|
||||||
|
"Use `--init` to initialise this directory.",
|
||||||
|
)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
vodFiles, err := scanner.FetchVideos(metadata.FootageDir)
|
|
||||||
|
// skip uploading if already done
|
||||||
|
if metadata.Uploaded == !forceUpload {
|
||||||
|
log.Printf(
|
||||||
|
"VOD has already been uploaded. " +
|
||||||
|
"Use --force to override, or update the %s.",
|
||||||
|
scanner.METADATA_FILENAME,
|
||||||
|
)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan for VOD segments
|
||||||
|
vodFiles, err := scanner.ScanSegments(metadata.FootageDir, segmentExtension)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to fetch VOD filenames: %v", err)
|
log.Fatalf("Failed to fetch VOD filenames: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(vodFiles) == 0 {
|
if len(vodFiles) == 0 {
|
||||||
log.Fatal("Directory contained no VOD files (expecting .mkv)")
|
log.Fatalf(
|
||||||
|
"Directory contained no VOD files (expecting .%s)",
|
||||||
|
segmentExtension,
|
||||||
|
)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if verbose {
|
if verbose {
|
||||||
enc := json.NewEncoder(os.Stdout)
|
enc := json.NewEncoder(os.Stdout)
|
||||||
enc.SetIndent("", "\t")
|
enc.SetIndent("", "\t")
|
||||||
|
|
@ -176,6 +153,7 @@ func main() {
|
||||||
enc.Encode(vodFiles)
|
enc.Encode(vodFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// build video template for upload
|
||||||
video, err := yt.BuildVideo(metadata)
|
video, err := yt.BuildVideo(metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to build video template: %v", err)
|
log.Fatalf("Failed to build video template: %v", err)
|
||||||
|
|
@ -202,17 +180,76 @@ func main() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = vid.ConcatVideo(video, vodFiles)
|
// concatenate VOD segments into full VOD
|
||||||
|
err = vid.ConcatVideo(video, vodFiles, verbose)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to concatenate VOD files: %v", err)
|
log.Fatalf("Failed to concatenate VOD segments: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// okay actual youtube stuff now
|
// youtube oauth flow
|
||||||
|
|
||||||
// TODO: tidy up oauth flow with localhost webserver
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
config := &oauth2.Config{
|
token, err := completeOAuth(&ctx, cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("OAuth flow failed: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// okay actually upload now!
|
||||||
|
ytVideo, err := yt.UploadVideo(ctx, token, video)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to upload video: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if verbose {
|
||||||
|
jsonString, err := json.MarshalIndent(ytVideo, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to marshal video data json: %v", err)
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonString))
|
||||||
|
}
|
||||||
|
log.Print("Video uploaded successfully!")
|
||||||
|
|
||||||
|
// update metadata to reflect VOD is uploaded
|
||||||
|
metadata.Uploaded = true
|
||||||
|
err = scanner.WriteMetadata(directory, metadata)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to update metadata: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete full VOD after upload, if requested
|
||||||
|
if deleteFullVod {
|
||||||
|
err = os.Remove(path.Join(directory, scanner.METADATA_FILENAME))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to delete full VOD: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialiseDirectory(directory string) error {
|
||||||
|
dirInfo, err := os.Stat(directory)
|
||||||
|
if err != nil {
|
||||||
|
if err == os.ErrNotExist {
|
||||||
|
return fmt.Errorf("no such directory: %s", directory)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("failed to open directory: %v", err)
|
||||||
|
}
|
||||||
|
if !dirInfo.IsDir() {
|
||||||
|
return fmt.Errorf("not a directory: %s", directory)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = os.Stat(path.Join(directory, scanner.METADATA_FILENAME))
|
||||||
|
if err == nil {
|
||||||
|
return fmt.Errorf("directory already initialised: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = scanner.WriteMetadata(directory, scanner.DefaultMetadata())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func completeOAuth(ctx *context.Context, cfg *config.Config) (*oauth2.Token, error) {
|
||||||
|
oauth2Config := &oauth2.Config{
|
||||||
ClientID: cfg.Google.ClientID,
|
ClientID: cfg.Google.ClientID,
|
||||||
ClientSecret: cfg.Google.ClientSecret,
|
ClientSecret: cfg.Google.ClientSecret,
|
||||||
Endpoint: google.Endpoint,
|
Endpoint: google.Endpoint,
|
||||||
|
|
@ -220,21 +257,25 @@ func main() {
|
||||||
RedirectURL: "http://localhost:8090",
|
RedirectURL: "http://localhost:8090",
|
||||||
}
|
}
|
||||||
verifier := oauth2.GenerateVerifier()
|
verifier := oauth2.GenerateVerifier()
|
||||||
url := config.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
|
|
||||||
log.Printf("Visit URL to initiate OAuth2: %s", url)
|
|
||||||
|
|
||||||
|
url := oauth2Config.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
|
||||||
|
fmt.Printf("Sign in to YouTube: %s\n", url)
|
||||||
|
|
||||||
|
// TODO: tidy up oauth flow with localhost webserver
|
||||||
var code string
|
var code string
|
||||||
fmt.Print("Enter OAuth2 code: ")
|
fmt.Print("Enter OAuth2 code: ")
|
||||||
if _, err := fmt.Scan(&code); err != nil {
|
if _, err := fmt.Scan(&code); err != nil {
|
||||||
log.Fatalf("Failed to read oauth2 code: %v", err)
|
return nil, fmt.Errorf("failed to read code: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := config.Exchange(ctx, code, oauth2.VerifierOption(verifier))
|
token, err := oauth2Config.Exchange(*ctx, code, oauth2.VerifierOption(verifier))
|
||||||
log.Printf("Token expires on %s\n", token.Expiry.Format("02 Jan 2006"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not exchange OAuth2 code: %v", err)
|
log.Fatalf("Could not exchange OAuth2 code: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
yt.UploadVideo(ctx, token, video)
|
// TODO: save this token; look into token refresh
|
||||||
|
log.Printf("Token expires on %s\n", token.Expiry.Format("02 Jan 2006"))
|
||||||
|
|
||||||
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
res/help.txt
Normal file
16
res/help.txt
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
ari's VOD uploader
|
||||||
|
|
||||||
|
USAGE: %s [options] [directory]
|
||||||
|
|
||||||
|
This tool stitches together VOD segments and automatically uploads them to
|
||||||
|
YouTube. `directory` is assumed to be a directory containing a `metadata.toml`
|
||||||
|
(created with `--init`), and some `.mkv` files.
|
||||||
|
|
||||||
|
OPTIONS:
|
||||||
|
-h, --help: Show this help message.
|
||||||
|
-v, --verbose: Show verbose logging output.
|
||||||
|
--init: Initialise `directory` as a VOD directory.
|
||||||
|
-d, --deleteAfter: Deletes the full VOD after upload.
|
||||||
|
-f, --force: Force uploading the VOD, even if it already exists.
|
||||||
|
|
||||||
|
made with <3 by ari melody, 2026
|
||||||
|
|
@ -2,7 +2,7 @@ package scanner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -22,10 +22,13 @@ type (
|
||||||
Part int
|
Part int
|
||||||
FootageDir string
|
FootageDir string
|
||||||
Category *Category
|
Category *Category
|
||||||
|
Uploaded bool
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func FetchVideos(directory string) ([]string, error) {
|
const METADATA_FILENAME = "metadata.toml"
|
||||||
|
|
||||||
|
func ScanSegments(directory string, extension string) ([]string, error) {
|
||||||
entries, err := os.ReadDir(directory)
|
entries, err := os.ReadDir(directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -35,49 +38,46 @@ func FetchVideos(directory string) ([]string, error) {
|
||||||
|
|
||||||
for _, item := range entries {
|
for _, item := range entries {
|
||||||
if item.IsDir() { continue }
|
if item.IsDir() { continue }
|
||||||
if !strings.HasSuffix(item.Name(), ".mkv") { continue }
|
if !strings.HasSuffix(item.Name(), "." + extension) { continue }
|
||||||
files = append(files, item.Name())
|
files = append(files, item.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
return files, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchMetadata(directory string) (*Metadata, error) {
|
func ReadMetadata(directory string) (*Metadata, error) {
|
||||||
entries, err := os.ReadDir(directory)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range entries {
|
|
||||||
if item.IsDir() { continue }
|
|
||||||
if item.Name() == "metadata.toml" {
|
|
||||||
metadata, err := ParseMetadata(filepath.Join(directory, item.Name()))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
metadata.FootageDir = filepath.Join(directory, metadata.FootageDir)
|
|
||||||
return metadata, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseMetadata(filename string) (*Metadata, error) {
|
|
||||||
metadata := &Metadata{}
|
metadata := &Metadata{}
|
||||||
file, err := os.OpenFile(filename, os.O_RDONLY, 0o644)
|
file, err := os.OpenFile(
|
||||||
|
path.Join(directory, METADATA_FILENAME),
|
||||||
|
os.O_RDONLY, os.ModePerm,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == os.ErrNotExist {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = toml.NewDecoder(file).Decode(metadata)
|
err = toml.NewDecoder(file).Decode(metadata)
|
||||||
if err != nil {
|
if err != nil { return nil, err }
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return metadata, nil
|
return metadata, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultMetadata() Metadata {
|
func WriteMetadata(directory string, metadata *Metadata) (error) {
|
||||||
return Metadata{
|
file, err := os.OpenFile(
|
||||||
|
path.Join(directory, METADATA_FILENAME),
|
||||||
|
os.O_CREATE, 0644,
|
||||||
|
)
|
||||||
|
if err != nil { return err }
|
||||||
|
|
||||||
|
err = toml.NewEncoder(file).Encode(metadata)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultMetadata() *Metadata {
|
||||||
|
return &Metadata{
|
||||||
Title: "Untitled Stream",
|
Title: "Untitled Stream",
|
||||||
Date: time.Now().Format("2006-01-02"),
|
Date: time.Now().Format("2006-01-02"),
|
||||||
Part: 0,
|
Part: 0,
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func ConcatVideo(video *youtube.Video, vodFiles []string) error {
|
func ConcatVideo(video *youtube.Video, vodFiles []string, verbose bool) error {
|
||||||
fileListPath := path.Join(
|
fileListPath := path.Join(
|
||||||
path.Dir(video.Filename),
|
path.Dir(video.Filename),
|
||||||
"files.txt",
|
"files.txt",
|
||||||
|
|
@ -45,18 +45,20 @@ func ConcatVideo(video *youtube.Video, vodFiles []string) error {
|
||||||
err := os.WriteFile(
|
err := os.WriteFile(
|
||||||
fileListPath,
|
fileListPath,
|
||||||
[]byte(fileListString),
|
[]byte(fileListString),
|
||||||
0o644,
|
0644,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write file list: %v", err)
|
return fmt.Errorf("failed to write file list: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ffmpeg.Input(fileListPath, ffmpeg.KwArgs{
|
stream := ffmpeg.Input(fileListPath, ffmpeg.KwArgs{
|
||||||
"f": "concat",
|
"f": "concat",
|
||||||
"safe": "0",
|
"safe": "0",
|
||||||
}).Output(video.Filename, ffmpeg.KwArgs{
|
}).Output(video.Filename, ffmpeg.KwArgs{
|
||||||
"c": "copy",
|
"c": "copy",
|
||||||
}).OverWriteOutput().ErrorToStdOut().Run()
|
}).OverWriteOutput()
|
||||||
|
if verbose { stream = stream.ErrorToStdOut() }
|
||||||
|
err = stream.Run()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("ffmpeg error: %v", err)
|
return fmt.Errorf("ffmpeg error: %v", err)
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package youtube
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -161,14 +160,14 @@ func BuildDescription(video *Video) (string, error) {
|
||||||
return out.String(), nil
|
return out.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UploadVideo(ctx context.Context, token *oauth2.Token, video *Video) error {
|
func UploadVideo(ctx context.Context, token *oauth2.Token, video *Video) (*youtube.Video, error) {
|
||||||
title, err := BuildTitle(video)
|
title, err := BuildTitle(video)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to build title: %v", err)
|
return nil, fmt.Errorf("failed to build title: %v", err)
|
||||||
}
|
}
|
||||||
description, err := BuildDescription(video)
|
description, err := BuildDescription(video)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to build description: %v", err)
|
return nil, fmt.Errorf("failed to build description: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
service, err := youtube.NewService(
|
service, err := youtube.NewService(
|
||||||
|
|
@ -178,7 +177,7 @@ func UploadVideo(ctx context.Context, token *oauth2.Token, video *Video) error {
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create youtube service: %v\n", err)
|
log.Fatalf("Failed to create youtube service: %v\n", err)
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
videoService := youtube.NewVideosService(service)
|
videoService := youtube.NewVideosService(service)
|
||||||
|
|
@ -208,24 +207,17 @@ func UploadVideo(ctx context.Context, token *oauth2.Token, video *Video) error {
|
||||||
file, err := os.Open(video.Filename)
|
file, err := os.Open(video.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to open file: %v\n", err)
|
log.Fatalf("Failed to open file: %v\n", err)
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
call.Media(file)
|
call.Media(file)
|
||||||
|
|
||||||
log.Println("Uploading video...")
|
log.Println("Uploading video...")
|
||||||
|
|
||||||
res, err := call.Do()
|
ytVideo, err := call.Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to upload video: %v\n", err)
|
log.Fatalf("Failed to upload video: %v\n", err)
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := json.MarshalIndent(res, "", " ")
|
return ytVideo, err
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to marshal video data json: %v\n", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(string(data))
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue