Documentation
¶
Overview ¶
Package media provides a pipeline for downloading videos from social platforms, extracting audio, and transcribing speech.
Index ¶
- Constants
- func DownloadFile(ctx context.Context, client HTTPDoer, url, destPath string, maxSize int64) error
- func ExtractAudioChunk(ctx context.Context, videoPath, outputPath string, offsetSec, durationSec int) error
- func ExtractVideoClip(ctx context.Context, videoPath, outputPath string, startSec, endSec float64) error
- func MergeAudioVideo(ctx context.Context, videoPath, audioPath, outputPath string) error
- func ProbeDuration(ctx context.Context, path string) (int, error)
- type Chunk
- type Extractor
- type HTTPDoer
- type Media
- type MediaStats
- type Options
- type Processor
- type ProcessorOption
- type Quality
- type Registry
- type Result
- type Transcriber
- type Transcription
- type VideoClip
Constants ¶
const ( // DefaultDownloadTimeout is the maximum time for downloading a video file. DefaultDownloadTimeout = 120 * time.Second // DefaultFFmpegTimeout is the maximum time for an ffmpeg operation. DefaultFFmpegTimeout = 60 * time.Second // DefaultProbeTimeout is the maximum time for ffprobe duration check. DefaultProbeTimeout = 10 * time.Second // DefaultChunkSec is the default audio chunk duration in seconds for transcription. DefaultChunkSec = 20 // DefaultClipPaddingSec is the default padding applied to video clips // before and after transcription chunk boundaries, to avoid cutting mid-word. DefaultClipPaddingSec = 0.5 // DefaultFadeDurationSec is the audio fade duration at clip boundaries // to prevent clicks/pops from non-zero-crossing cuts. DefaultFadeDurationSec = 0.03 )
Default configuration values for the media processing pipeline.
Variables ¶
This section is empty.
Functions ¶
func DownloadFile ¶
DownloadFile downloads a URL to a local file with timeout and size limit.
func ExtractAudioChunk ¶
func ExtractAudioChunk(ctx context.Context, videoPath, outputPath string, offsetSec, durationSec int) error
ExtractAudioChunk extracts a WAV audio chunk from a video file using ffmpeg. Output is 16kHz mono PCM suitable for Whisper.
func ExtractVideoClip ¶ added in v0.3.2
func ExtractVideoClip(ctx context.Context, videoPath, outputPath string, startSec, endSec float64) error
ExtractVideoClip extracts a video segment from startSec to endSec using ffmpeg. Video stream is copied (-c:v copy) for fast, lossless extraction. Audio gets a short fade-in/fade-out to prevent clicks at cut boundaries. The -ss flag is placed before -i for fast keyframe-level seeking.
func MergeAudioVideo ¶
MergeAudioVideo combines a video-only and audio-only file into a single MP4 using ffmpeg. Used for DASH streams where video and audio are separate.
Types ¶
type Chunk ¶
type Chunk struct {
Start float64 // segment start time in seconds
End float64 // segment end time in seconds
Text string // transcribed text for this segment
}
Chunk represents a single transcribed audio segment.
type Extractor ¶
type Extractor interface {
// Name returns the platform name (e.g. "instagram", "youtube").
Name() string
// Match returns true if the URL belongs to this platform.
Match(url string) bool
// Extract fetches media metadata including the direct video URL.
Extract(ctx context.Context, url string) (*Media, error)
}
Extractor fetches media metadata from a platform-specific URL.
type Media ¶
type Media struct {
Platform string // platform name: "instagram", "youtube", etc.
URL string // original input URL
VideoURL string // direct video CDN URL
AudioURL string // separate audio URL (for DASH merge)
LocalPath string // path to already-downloaded file (skips download)
Title string // post/video title
Description string // post caption or video description
Author string // author display name or @username
Duration time.Duration // video duration (zero if unknown)
Qualities []Quality // available quality options
Stats MediaStats // engagement stats (likes, views, etc.)
Metadata map[string]string // platform-specific key-value pairs
}
Media represents extracted metadata and download information for a video.
type MediaStats ¶
type MediaStats struct {
Views int64 // view/play count
Likes int64 // like/heart count
Comments int64 // comment/reply count
}
MediaStats holds engagement metrics for a media post.
type Options ¶
type Options struct {
MaxSize int64 // max video file size in bytes (0 = no limit)
ChunkSec int // audio chunk duration for transcription (default 20)
TempDir string // directory for temporary files (default os.TempDir())
ExtractClips bool // if true, extract video clips for each transcription chunk
ClipPaddingSec float64 // seconds to extend clips before/after chunk boundaries (default 0.5)
}
Options configures a single Process call.
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor orchestrates the full media pipeline: extract → download → transcribe.
func NewProcessor ¶
func NewProcessor(opts ...ProcessorOption) *Processor
NewProcessor creates a Processor with the given options.
type ProcessorOption ¶
type ProcessorOption func(*Processor)
ProcessorOption configures a Processor.
func WithExtractor ¶
func WithExtractor(e Extractor) ProcessorOption
WithExtractor registers a platform extractor.
func WithHTTPClient ¶
func WithHTTPClient(doer HTTPDoer) ProcessorOption
WithHTTPClient sets a custom HTTP client for video downloads.
func WithTranscriber ¶
func WithTranscriber(t Transcriber) ProcessorOption
WithTranscriber sets the transcription backend.
type Quality ¶
type Quality struct {
Label string // human label: "1080p", "720p", "360p"
URL string // direct download URL for this quality
Width int // pixels, 0 if unknown
Height int // pixels, 0 if unknown
Size int64 // estimated bytes, 0 if unknown
}
Quality represents a single video quality variant.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds registered extractors and dispatches URLs to the matching one.
func (*Registry) Match ¶
Match finds the first extractor that matches the given URL. Returns nil if no extractor matches.
type Result ¶
type Result struct {
Media *Media // extracted media metadata
VideoPath string // path to downloaded video file
Transcription *Transcription // transcription result (nil if not requested or no speech)
VideoClips []VideoClip // extracted video clips (nil if ExtractClips option not set)
}
Result is the output of a full processing pipeline.
type Transcriber ¶
type Transcriber interface {
// Transcribe processes an audio file and returns the transcription.
// audioPath must be a valid file path to a WAV/MP3/OGG file.
Transcribe(ctx context.Context, audioPath string) (*Transcription, error)
// Available reports whether the transcription backend is reachable.
Available() bool
}
Transcriber converts an audio file to text.
type Transcription ¶
type Transcription struct {
Text string // full concatenated text
Language string // detected language code (e.g. "en", "ru")
Duration float64 // audio duration in seconds
Chunks []Chunk // per-segment results with timestamps
FailedChunks int // number of chunks that failed extraction or transcription
}
Transcription holds the result of speech-to-text processing.
func ChunkAndTranscribe ¶
func ChunkAndTranscribe(ctx context.Context, videoPath, tempDir string, t Transcriber, opts Options) (*Transcription, error)
ChunkAndTranscribe splits audio into chunks and transcribes each one. Returns (nil, nil) if transcriber is nil (opt-out). Returns (nil, error) if the transcriber is unavailable or ffprobe fails. Returns (partial, error) if some chunks failed — the partial result contains the successfully transcribed text and FailedChunks is set.
type VideoClip ¶ added in v0.3.2
type VideoClip struct {
Path string // path to the extracted clip file
Start float64 // clip start time in seconds
End float64 // clip end time in seconds
Text string // transcription text for this clip
}
VideoClip is a short video segment extracted from the downloaded video, corresponding to a transcription chunk. The caller is responsible for cleaning up the clip files (Path) after use.
func ExtractVideoClipsFromChunks ¶ added in v0.3.2
func ExtractVideoClipsFromChunks(ctx context.Context, videoPath, tempDir string, chunks []Chunk, paddingSec, totalDuration float64) ([]VideoClip, int)
ExtractVideoClipsFromChunks extracts video clips for each transcription chunk. Clips are named "<base>_clip_<index>.mp4" in tempDir. Chunks with empty text are skipped. Each clip is extended by paddingSec before and after the chunk boundary (clamped to [0, totalDuration]) to avoid cutting mid-word. Returns the extracted clips and a count of failures.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
extract
|
|
|
instagram
Package instagram extracts video metadata from Instagram and Threads URLs using the go-threads client library.
|
Package instagram extracts video metadata from Instagram and Threads URLs using the go-threads client library. |
|
youtube
Package youtube extracts video metadata from YouTube URLs using a tiered backend approach: API → yt-dlp → ox-browser.
|
Package youtube extracts video metadata from YouTube URLs using a tiered backend approach: API → yt-dlp → ox-browser. |
|
transcribe
|
|
|
gostt
Package gostt provides a Transcriber that wraps the go-stt client library.
|
Package gostt provides a Transcriber that wraps the go-stt client library. |
|
openai
Package openai provides a Transcriber that uses an OpenAI-compatible STT API (works with ox-whisper, Groq, OpenAI, etc.).
|
Package openai provides a Transcriber that uses an OpenAI-compatible STT API (works with ox-whisper, Groq, OpenAI, etc.). |