Skip to content

Commit

Permalink
feat: Handle image asset (for Clip) (#9)
Browse files Browse the repository at this point in the history
* Add ImageAsset

* doc: update readme
  • Loading branch information
DblK committed Aug 3, 2022
1 parent 74fb59e commit c2d4018
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ At the end of the road this section should either disappear or be full of `Yes`
| Clip | opacity | Not yet | |
| Clip | transform | Not yet | |
| Clip [`VideoAsset`] | all ✅ | Yes ✅ | |
| Clip [`ImageAsset`] | src | Partial 🛠 | Download asset only |
| Clip [`ImageAsset`] | crop | Not yet | |
| Clip [`ImageAsset`] | all ✅ | Yes ✅ | |
| Clip [`TitleAsset`] | all | Not yet | |
| Clip [`HTMLAsset`] | all | Not yet | |
| Clip [`AudioAsset`] | src | Partial 🛠 | Download asset only |
| Clip [`AudioAsset`] | trim | Not yet | |
| Clip [`AudioAsset`] | volume | Not yet | |
| Clip [`AudioAsset`] | effect | Not yet | |
| Clip [`LumaAsset`] | src | Partial 🛠 | Download asset only |
| Clip [`LumaAsset`] | trim | Not yet | |
| Output | format | Partial 🛠 | Only `mp4` at the moment |
| Output | resolution | Yes ✅ | |
| Output | aspectRatio | Not yet | |
Expand Down
21 changes: 17 additions & 4 deletions go/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strings"

"github.com/spf13/cast"
"golang.org/x/exp/slices"
)

type FFMPEGSource struct {
path string
path string
needLoop bool
}

type FFMPEGTrack struct {
Expand Down Expand Up @@ -60,9 +62,10 @@ func (s *FFMPEG) AddDefaultParams() error {
return nil
}

func (s *FFMPEG) AddSource(fileName string) error {
func (s *FFMPEG) AddSource(fileName string, needLoop bool) error {
var newSource = &FFMPEGSource{
path: fileName,
path: fileName,
needLoop: needLoop,
}
s.src = append(s.src, *newSource)
return nil
Expand Down Expand Up @@ -487,13 +490,19 @@ func (s *FFMPEG) generateOutputName() string {
return file.Name()
}

func (s *FFMPEG) isImagePath(path string) bool {
imageValues := []string{"jpeg", "jpg", "png", "bmp", "gif"}
return slices.Contains(imageValues, filepath.Ext(path)[1:])
}

func (s *FFMPEG) ToFFMPEG(renderQueue *RenderQueue, queue *ProcessingQueue) error {
_ = s.AddDefaultParams()
_ = s.SetOutputFormat(renderQueue.Data.Output.Format)
if renderQueue.Data.Output.Fps != nil {
_ = s.SetOutputFps(*renderQueue.Data.Output.Fps)
}
_ = s.SetDefaultBackground(renderQueue.Data.Timeline.Background)
// _ = s.SetOutputAspectRatio(renderQueue.Data.Output.AspectRatio)

// Handle Sources
var sourceClip = 0
Expand Down Expand Up @@ -521,7 +530,7 @@ func (s *FFMPEG) ToFFMPEG(renderQueue *RenderQueue, queue *ProcessingQueue) erro

sourceFileName := queue.FindSourceClip(trackNumber, iClip)
if sourceFileName != "" {
_ = s.AddSource(sourceFileName)
_ = s.AddSource(sourceFileName, s.isImagePath(sourceFileName))
}

_ = clip.ToFFMPEG(s, sourceClip, trackNumber, clipNumber)
Expand Down Expand Up @@ -585,6 +594,10 @@ func (s *FFMPEG) ToString() []string {
// Handle source
var maxSource = len(s.src) - 1
for _, source := range s.src {
if source.needLoop {
parameters = append(parameters, "-loop")
parameters = append(parameters, "1")
}
parameters = append(parameters, "-i")
parameters = append(parameters, source.path)
}
Expand Down
14 changes: 11 additions & 3 deletions go/model_clip.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,19 @@ func (s *Clip) ToFFMPEG(FFMPEGCommand FFMPEGCommand, sourceClip int, trackNumber
effects = append(effects, FFMPEGCommand.ClipFillerOverlay(sourceClip, trackNumber, currentClip, FFMPEGCommand.ClipCropOverlayPosition(currentAsset.Crop)))
}

// case ImageAssetType:
// handled = true
// effects = append(effects, FFMPEGCommand.ClipImage(sourceClip, trackNumber, currentClip, 0, s.Length))
case ImageAssetType:
var currentAsset = s.Asset.(*ImageAsset)
effects = append(effects, FFMPEGCommand.ClipImage(sourceClip, trackNumber, currentClip, 0, s.Length))

if currentAsset.Crop != nil {
effects = append(effects, FFMPEGCommand.ClipCrop(sourceClip, trackNumber, currentClip, currentAsset.Crop))
effects = append(effects, FFMPEGCommand.ClipFillerOverlay(sourceClip, trackNumber, currentClip, FFMPEGCommand.ClipCropOverlayPosition(currentAsset.Crop)))
} else {
effects = append(effects, FFMPEGCommand.ClipFillerOverlay(sourceClip, trackNumber, currentClip, "topLeft"))
}
default:
fmt.Println("Type not handled for converting to FFMPEG", typeAsset.String())
return nil
}

// Resize clip to ensure concat will work
Expand Down
2 changes: 1 addition & 1 deletion go/model_ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package openapi

type FFMPEGCommand interface {
AddSource(string) error
AddSource(string, bool) error
AddDefaultParams() error
ToString() []string
AddTrack(int) error
Expand Down
2 changes: 1 addition & 1 deletion go/model_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Output struct {
Resolution string `json:"resolution,omitempty"`

// The aspect ratio (shape) of the video or image. Useful for social media output formats. Options are: <ul> <li>`16:9` - regular landscape/horizontal aspect ratio (default)</li> <li>`9:16` - vertical/portrait aspect ratio</li> <li>`1:1` - square aspect ratio</li> <li>`4:5` - short vertical/portrait aspect ratio</li> <li>`4:3` - legacy TV aspect ratio</li> </ul>
AspectRatio string `json:"aspectRatio,omitempty"`
AspectRatio string `json:"aspectRatio,omitempty" default:"16:9"`

Size *Size `json:"size,omitempty"`

Expand Down
1 change: 1 addition & 0 deletions go/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (s *ProcessingQueue) FetchSrcAssets(trackNumber int, clipNumber int, clip C
case ImageAssetType:
asset := clip.Asset.(*ImageAsset)
assetSrc = asset.Src
assetHandled = true
case VideoAssetType:
asset := clip.Asset.(*VideoAsset)
assetSrc = asset.Src
Expand Down

0 comments on commit c2d4018

Please sign in to comment.