Loop/repeat a sound track

Is it possible to loop a sound track? i tried to create a video with a short sound track. the video is 1.5 minutes and sound track was about 55 seconds after rendering the video i noted that the sound track did not continue till the end of he video. so is it possible to iterate the sound track until the full video lenght?

1 Like

We would also like to enable this functionality

1 Like

The soundtrack property does not support looping but there are a couple of ways you can do this.

  1. Prepare the audio so that it already loops, i.e. join the audio track together multiple times so it is longer than the longest video you expect to make. There are a number of tools online that can do this and you can even do this with Shotstack as I will show you in option 2.

  2. You can use the audio asset: Shotstack v1 API Reference Documentation - this works in much the same way as the video asset and lets you set the start and length of an audio file. You can use multiple audio assets with the same mp3 file and start them one after another. You can use the audio asset as part of your video edit or as mentioned you could pre-render an mp3 file with the audio looping and then use that as the soundtrack.

Here is a JSON example showing how to merge 3 audio clips together into a longer audio file:

{
    "timeline": {
        "tracks": [
            {
                "clips": [
                    {
                        "asset": {
                            "type": "audio",
                            "src": "https://shotstack-assets.s3.ap-southeast-2.amazonaws.com/music/claps.mp3"
                        },
                        "start": 0,
                        "length": 16
                    },
                    {
                        "asset": {
                            "type": "audio",
                            "src": "https://shotstack-assets.s3.ap-southeast-2.amazonaws.com/music/claps.mp3"
                        },
                        "start": 16,
                        "length": 16
                    },
                    {
                        "asset": {
                            "type": "audio",
                            "src": "https://shotstack-assets.s3.ap-southeast-2.amazonaws.com/music/claps.mp3"
                        },
                        "start": 32,
                        "length": 16
                    }
                ]
            }
        ]
    },
    "output": {
        "format": "mp3",
        "resolution": "hd"
    }
}

And the result:

1 Like

To understand whether you need to loop your audio or not you can use the probe endpoint. This endpoint allows you to get the duration of the video you may be working with - giving you the data you need to understand how many times your audio is to loop.

Example code

Probe endpoint

https://api.shotstack.io/edit/v1/probe/https%3A%2F%2Fshotstack-assets.s3.amazonaws.com%2Ffootage%2Fnight-sky.mp4

{
    "success": true,
    "message": "ok",
    "response": {
        "metadata": {
            "streams": [
                { ... }
            ],
            "chapters": [],
            "format": {
                ...
                "duration": "13.880000",
                ...
            }
        }
    }
}

Audio loop

let clips = [];
if (audioDuration < videoDuration) {
    let loops = Math.floor(videoDuration / audioDuration);
    let remainder = (videoDuration / audioDuration) % 1;
    let start = 0;
    for (let i = 0; i < loops; i++) {
        clips.push({
            "asset": {
                "type": "audio",
                "src": "https://shotstack-assets.s3.amazonaws.com/music/moment.mp3"
            },
            "start": start,
            "length": audioDuration
        });
        start += audioDuration;
    }
    clips.push({
        "asset": {
            "type": "audio",
            "src": "https://shotstack-assets.s3.amazonaws.com/music/moment.mp3"
        },
        "start": loops * audioDuration,
        "length": (audioDuration * remainder)
    });
} else {
    clips = [{
        "asset": {
            "type": "audio",
            "src": "https://shotstack-assets.s3.amazonaws.com/music/moment.mp3",
        },
        "start": 0,
        "length": videoDuration
    }];
}
1 Like