Skip to content

Commit e7915cb

Browse files
Add FFmpeg service and integrate with video handler for HLS generation
1 parent 8fcd0f1 commit e7915cb

5 files changed

Lines changed: 125 additions & 7 deletions

File tree

Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Builder
2+
FROM golang:1.25 AS builder
3+
4+
WORKDIR /app
5+
6+
COPY go.mod go.sum ./
7+
RUN go mod download
8+
9+
COPY . .
10+
11+
RUN CGO_ENABLED=0 GOOS=linux go build -o worker ./cmd/worker
12+
13+
# Runtime
14+
FROM debian:bookworm-slim
15+
16+
RUN apt-get update && \
17+
apt-get install -y ffmpeg ca-certificates && \
18+
rm -rf /var/lib/apt/lists/*
19+
20+
WORKDIR /app
21+
22+
COPY --from=builder /app/worker .
23+
24+
CMD ["./worker"]

cmd/worker/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55

66
"github.com/Prateet-Github/worker-go/internal/config"
7+
"github.com/Prateet-Github/worker-go/internal/ffmpeg"
78
"github.com/Prateet-Github/worker-go/internal/handlers"
89
"github.com/Prateet-Github/worker-go/internal/queue"
910
"github.com/Prateet-Github/worker-go/internal/s3"
@@ -17,9 +18,11 @@ func main() {
1718
server := queue.NewServer(cfg)
1819

1920
s3Client := s3.NewClient(cfg)
21+
ffmpegService := ffmpeg.NewService()
2022
videoHandler := handlers.NewVideoHandler(
2123
s3Client,
2224
cfg,
25+
ffmpegService,
2326
)
2427

2528
mux := asynq.NewServeMux()

internal/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ type Config struct {
2020
}
2121

2222
func Load() *Config {
23-
err := godotenv.Load()
24-
if err != nil {
25-
log.Fatal("Error loading .env file")
23+
24+
if err := godotenv.Load(); err != nil {
25+
log.Println(".env not found, using environment variables")
2626
}
2727

2828
return &Config{

internal/ffmpeg/service.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ffmpeg
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
)
9+
10+
type Service struct{}
11+
12+
func NewService() *Service {
13+
return &Service{}
14+
}
15+
16+
func (s *Service) GenerateHLS(
17+
ctx context.Context,
18+
inputPath string,
19+
outputDir string,
20+
) error {
21+
22+
if err := os.MkdirAll(outputDir, 0755); err != nil {
23+
return err
24+
}
25+
26+
args := []string{
27+
"-y",
28+
"-i", inputPath,
29+
30+
"-c:v", "libx264",
31+
"-c:a", "aac",
32+
33+
"-f", "hls",
34+
"-hls_time", "6",
35+
"-hls_playlist_type", "vod",
36+
37+
"-hls_segment_filename",
38+
fmt.Sprintf("%s/segment_%%03d.ts", outputDir),
39+
40+
fmt.Sprintf("%s/master.m3u8", outputDir),
41+
}
42+
43+
cmd := exec.CommandContext(
44+
ctx,
45+
"ffmpeg",
46+
args...,
47+
)
48+
49+
cmd.Stdout = os.Stdout
50+
cmd.Stderr = os.Stderr
51+
52+
return cmd.Run()
53+
}

internal/handlers/video.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77

88
"github.com/Prateet-Github/worker-go/internal/config"
9+
"github.com/Prateet-Github/worker-go/internal/ffmpeg"
910
"github.com/Prateet-Github/worker-go/internal/queue"
1011
"github.com/Prateet-Github/worker-go/internal/s3"
1112

@@ -18,15 +19,18 @@ import (
1819
type VideoHandler struct {
1920
s3Client *awss3.Client
2021
cfg *config.Config
22+
ffmpeg *ffmpeg.Service
2123
}
2224

2325
func NewVideoHandler(
2426
s3Client *awss3.Client,
2527
cfg *config.Config,
28+
ffmpeg *ffmpeg.Service,
2629
) *VideoHandler {
2730
return &VideoHandler{
2831
s3Client: s3Client,
2932
cfg: cfg,
33+
ffmpeg: ffmpeg,
3034
}
3135
}
3236

@@ -41,23 +45,57 @@ func (h *VideoHandler) ProcessVideo(
4145
return err
4246
}
4347

44-
localPath := filepath.Join(
48+
// localPath := filepath.Join(
49+
// "temp",
50+
// payload.VideoID+".mp4",
51+
// )
52+
53+
workspace := filepath.Join(
4554
"temp",
46-
payload.VideoID+".mp4",
55+
payload.VideoID,
56+
)
57+
58+
inputPath := filepath.Join(
59+
workspace,
60+
"input.mp4",
4761
)
4862

63+
outputDir := filepath.Join(
64+
workspace,
65+
"hls",
66+
)
67+
68+
log.Println("Starting download...")
69+
4970
err := s3.DownloadFile(
5071
h.s3Client,
5172
h.cfg.S3RawBucket,
5273
payload.S3Key,
53-
localPath,
74+
inputPath,
5475
)
5576

5677
if err != nil {
5778
return err
5879
}
5980

60-
log.Println("Download completed:", localPath)
81+
log.Println("Download complete")
82+
83+
log.Println("Calling FFmpeg...")
84+
85+
if err := h.ffmpeg.GenerateHLS(
86+
ctx,
87+
inputPath,
88+
outputDir,
89+
); err != nil {
90+
log.Printf("GenerateHLS failed: %v\n", err)
91+
return err
92+
}
93+
94+
log.Println("FFmpeg finished")
95+
96+
log.Println("HLS generated successfully")
97+
98+
log.Println("Download completed:", inputPath)
6199

62100
log.Println("Processing video...")
63101
log.Println("Video ID:", payload.VideoID)

0 commit comments

Comments
 (0)