Skip to content

Commit

Permalink
feat: add command to add bg music
Browse files Browse the repository at this point in the history
  • Loading branch information
ChetanXpro committed Aug 12, 2023
1 parent bea389c commit 3adde31
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ new.mp4
basicaudio.mp3
basicaudio.wav
basicaudio.wav.srt
shorts
shorts
bg.mp3
23 changes: 19 additions & 4 deletions src/video/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs'
import path from 'path'
import ffmpegPath from 'ffmpeg-static'
import ffmpegProb from 'ffprobe-static'
import { exec } from 'child_process'

if (ffmpegPath) {
ffmpeg.setFfprobePath(ffmpegProb.path)
Expand Down Expand Up @@ -40,6 +41,7 @@ export const mergeAudio = async ({

// console.log('videodata: ', videodata)
// console.log('audiodata: ', audiodata)
// const backgroundMusicFilePath = path.join(__dirname, '..', '..', 'bg.mp3')

const videoDurationInSeconds = videodata.format.duration

Expand All @@ -55,10 +57,13 @@ export const mergeAudio = async ({
const audioFilter = `atempo=${audioSpeed},aformat=sample_rates=44100:channel_layouts=stereo`
const srtFilePath = 'basicaudio.wav.srt'
const newSrtFilePath = path.join(__dirname, '..', '..', srtFilePath)
const backgroundMusicFilePath = path.join(__dirname, '..', '..', 'bg.mp3')

const out = path.join(__dirname, '..', '..', 'tryyyyyyyyy.mp3')
const subtitleStyle =
"force_style='Alignment=6,FontName=Trebuchet,FontSize=18,PrimaryColour=&Hffffff&,OutlineColour=&H00000000&,MarginV=25'"

// Merge the audio with the video
const backgroundAudiocommand = `${ffmpegPath} -i ${outputVideoPath} -i ${backgroundMusicFilePath} -filter_complex "[0:a]volume=1[a1];[1:a]volume=0.2[b1];[a1][b1]amix=inputs=2[aout]" -map 0:v -map "[aout]" -c:v copy -c:a aac -shortest ${out}`

return new Promise((resolve, reject) => {
ffmpeg(videoFilePath)
Expand All @@ -67,15 +72,17 @@ export const mergeAudio = async ({

.videoFilter(videoFilter)
.audioFilter(audioFilter)
.input(backgroundMusicFilePath)

.outputOptions([
'-vf',
`subtitles=${newSrtFilePath}:${subtitleStyle}`,
'-map',
'0:v',
'-map',
'1:a',
'-c:v libx264',
'-c:a aac',
'-vf',
`subtitles=${newSrtFilePath}:${subtitleStyle}`,
// '-apad',
])
.output(outputVideoPath)
Expand All @@ -84,7 +91,15 @@ export const mergeAudio = async ({
})
.on('end', () => {
console.log('Audio added to video complete!')
resolve('Audio added to video complete!')

exec(backgroundAudiocommand, (error, stdout, stderr) => {
if (error) {
console.error('Error:', error)
return
}
console.log('stdout:', stdout)
console.error('stderr:', stderr)
})
})
.on('error', err => {
console.error('Error during audio adding to video:', err.message)
Expand Down

0 comments on commit 3adde31

Please sign in to comment.