diff --git a/README.md b/README.md index 49ce072..f432bb6 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,11 @@ type Options struct { Bitrate int // Bitrate. Format string // Format of audio. Codec string // Audio Codec. - Video string // File path for Video to use. + StreamFile string // File path for extra stream data. } ``` -The `Options.Video` parameter is intended for users who wish to alter an audio stream from a video. Instead of having to process the audio and store in a file and then combine with the video later, the user can simply pass in the original video file path via the `Options.Video` parameter. This will combine the audio with all other streams in the given video file (Video, Subtitle, Data, and Attachments Streams) and will cut all streams to be the same length. **Note that `aio` is not a audio/video editing library.** +The `Options.StreamFile` parameter is intended for users who wish to alter an audio stream from a video. Instead of having to process the audio and store in a file and then combine with the video later, the user can simply pass in the original video file path via the `Options.StreamFile` parameter. This will combine the audio with all other streams in the given video file (Video, Subtitle, Data, and Attachments Streams) and will cut all streams to be the same length. **Note that `aio` is not a audio/video editing library.** Note that this means that adding extra stream data from a file will only work if the `filename` being written to is a container format, i.e attempting to add video streams to a `wav` file will result in a undefined behavior. @@ -74,7 +74,7 @@ Total() int Duration() float64 Format() string Codec() string -HasVideo() bool +HasStreams() bool Buffer() []byte MetaData() map[string]string Samples() interface{} @@ -92,7 +92,7 @@ Close() aio.NewAudioWriter(filename string, options *aio.Options) (*aio.AudioWriter, error) FileName() string -Video() string +StreamFile() string SampleRate() int Channels() int Bitrate() int @@ -240,7 +240,7 @@ options := aio.Options{ Bitrate: audio.Bitrate(), Format: audio.Format(), Codec: "aac", - Video: "movie.mov", + StreamFile: "movie.mov", } writer, _ := aio.NewAudioWriter("output.mp4", &options) diff --git a/aio_test.go b/aio_test.go index c950627..e221186 100644 --- a/aio_test.go +++ b/aio_test.go @@ -166,6 +166,7 @@ func TestAudioIO(t *testing.T) { assertEquals(audio.Codec(), "mp3") assertEquals(audio.BitsPerSample(), 16) assertEquals(audio.Stream(), 0) + assertEquals(audio.HasStreams(), false) assertEquals(len(audio.Buffer()), 0) fmt.Println("Audio File IO test passed") diff --git a/audio.go b/audio.go index 86b5102..990c036 100644 --- a/audio.go +++ b/audio.go @@ -22,7 +22,7 @@ type Audio struct { format string // Format of audio samples. codec string // Codec used for video encoding. ended bool // Flag storing whether Audio reading has ended. - hasvideo bool // Flag storing whether file contains Video. + hasstreams bool // Flag storing whether file has additional data streams. buffer []byte // Raw audio data. metadata map[string]string // Audio Metadata. pipe *io.ReadCloser // Stdout pipe for ffmpeg process. @@ -81,8 +81,9 @@ func (audio *Audio) Codec() string { return audio.codec } -func (audio *Audio) HasVideo() bool { - return audio.hasvideo +// Returns true if file has any video, subtitle, data or attachment streams. +func (audio *Audio) HasStreams() bool { + return audio.hasstreams } func (audio *Audio) Buffer() []byte { @@ -165,20 +166,28 @@ func NewAudioStreams(filename string, options *Options) ([]*Audio, error) { bps := int(parse(regexp.MustCompile(`\d{1,2}`).FindString(format))) // Bits per sample. - videoData, err := ffprobe(filename, "v") - if err != nil { - return nil, err + // Loop over all stream types. v: Video, s: Subtitle, d: Data, t: Attachments + hasstream := false + for _, c := range "vsdt" { + data, err := ffprobe(filename, string(c)) + if err != nil { + return nil, err + } + if len(data) > 0 { + hasstream = true + break + } } streams := make([]*Audio, len(audioData)) for i, data := range audioData { audio := &Audio{ - filename: filename, - format: format, - bps: bps, - stream: i, - hasvideo: len(videoData) > 0, - metadata: data, + filename: filename, + format: format, + bps: bps, + stream: i, + hasstreams: hasstream, + metadata: data, } audio.addAudioData(data) diff --git a/audiowriter.go b/audiowriter.go index eb110cf..822fd37 100644 --- a/audiowriter.go +++ b/audiowriter.go @@ -11,7 +11,7 @@ import ( type AudioWriter struct { filename string // Output filename. - video string // Extra stream data filename. + streamfile string // Extra stream data filename. samplerate int // Audio Sample Rate in Hz. channels int // Number of audio channels. bitrate int // Bitrate for audio encoding. @@ -26,8 +26,8 @@ func (writer *AudioWriter) FileName() string { } // File used to fill in extra stream data. -func (writer *AudioWriter) Video() string { - return writer.video +func (writer *AudioWriter) StreamFile() string { + return writer.streamfile } // Audio Sample Rate in Hz. @@ -67,10 +67,10 @@ func NewAudioWriter(filename string, options *Options) (*AudioWriter, error) { } writer := &AudioWriter{ - filename: filename, - video: options.Video, - bitrate: options.Bitrate, - codec: options.Codec, + filename: filename, + streamfile: options.StreamFile, + bitrate: options.Bitrate, + codec: options.Codec, } writer.samplerate = 44100 // 44100 Hz sampling rate by default. @@ -92,11 +92,11 @@ func NewAudioWriter(filename string, options *Options) (*AudioWriter, error) { } } - if options.Video != "" { - if !exists(options.Video) { - return nil, fmt.Errorf("file %s does not exist", options.Video) + if options.StreamFile != "" { + if !exists(options.StreamFile) { + return nil, fmt.Errorf("file %s does not exist", options.StreamFile) } - writer.video = options.Video + writer.streamfile = options.StreamFile } return writer, nil @@ -119,10 +119,10 @@ func (writer *AudioWriter) init() error { } // Assumes "writer.file" is a container format. - if writer.video != "" { + if writer.streamfile != "" { command = append( command, - "-i", writer.video, + "-i", writer.streamfile, "-map", "0:a:0", "-map", "1:v?", // Add Video streams if present. "-c:v", "copy", diff --git a/options.go b/options.go index 8dfd23b..e95a257 100644 --- a/options.go +++ b/options.go @@ -7,5 +7,5 @@ type Options struct { Bitrate int // Bitrate. Format string // Format of audio. Codec string // Audio Codec. - Video string // File path for Video to use. + StreamFile string // File path for extra stream data. }