LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2013, 02:03 AM   #1
H5X00R
Member
 
Registered: Apr 2013
Posts: 39

Rep: Reputation: 1
Need help with script: Mix video and audio files with ffmpeg


Hi,

I need scripting help. I have 3 video files and 3 audio files.

I want to use ffmpeg to mix the videos with its corresponded audio. Below is the list. This is just an example:

audio-1.wav
audio-2.wav
audio-3.wav
video-1.mp4
video-2.mp4
video-3.mp4

Here is my script thus far

Code:
#!/bin/bash 

for i in *.mp4
do
for j in *.wav
do
echo ffmpeg -i "$j" -i "$i" -vcodec copy tmp/"$i"
done
done
Here is the output from the script
Quote:
ffmpeg -i audio-1.wav -i video-1.mp4 -vcodec copy tmp/video-1.mp4
ffmpeg -i audio-2.wav -i video-1.mp4 -vcodec copy tmp/video-1.mp4
ffmpeg -i audio-3.wav -i video-1.mp4 -vcodec copy tmp/video-1.mp4
ffmpeg -i audio-1.wav -i video-2.mp4 -vcodec copy tmp/video-2.mp4
ffmpeg -i audio-2.wav -i video-2.mp4 -vcodec copy tmp/video-2.mp4
ffmpeg -i audio-3.wav -i video-2.mp4 -vcodec copy tmp/video-2.mp4
ffmpeg -i audio-1.wav -i video-3.mp4 -vcodec copy tmp/video-3.mp4
ffmpeg -i audio-2.wav -i video-3.mp4 -vcodec copy tmp/video-3.mp4
ffmpeg -i audio-3.wav -i video-3.mp4 -vcodec copy tmp/video-3.mp4
I want the script to do this

Quote:
ffmpeg -i audio-1.wav -i video-1.mp4 -vcodec copy tmp/video-1.mp4
ffmpeg -i audio-2.wav -i video-2.mp4 -vcodec copy tmp/video-2.mp4
ffmpeg -i audio-3.wav -i video-3.mp4 -vcodec copy tmp/video-3.mp4
I appreciate any suggestions or a better approach for my solution. Thanks

Last edited by H5X00R; 04-09-2013 at 02:04 AM.
 
Old 04-09-2013, 05:56 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If you're using bash or another shell with array support, you can use paired arrays instead.

Code:
vidarray=( *.mp4 )
audarray=( *.wav )

for i in "${!vidarray[@]}"; do
    echo ffmpeg -i "${vidarray[i]}" -i "${audarray[i]}" -vcodec copy "./tmp/${vidarray[i]}"
done
This assumes that the shell will sort the two lists of files identically, of course. If you have many files, you should ensure that the filenames are zero-padded*, or set the arrays manually.




(*I recommend always zero-padding numbers in your filenames; it makes handling them like this so much easier. Try to avoid using spaces or other shell-reserved characters too.)
 
Old 04-09-2013, 06:58 PM   #3
H5X00R
Member
 
Registered: Apr 2013
Posts: 39

Original Poster
Rep: Reputation: 1
Thanks David for the code,

I did solved my solution last night after experimenting for 30 minutes. I appreciate it.

Here is the code I used:
Code:
#!/bin/bash 
list=(*.wav)
x=0
for i in *.mp4
do
echo ffmpeg -i "${list[$x]}" -i "$i" -vcodec copy tmp/"$i"
(( x++ ))
done
But your version has less lines and in shell scripting the less coding the better.

+1
 
Old 04-09-2013, 07:22 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yeah, that'll work too. It's really the same as mine except that it uses only one array, replacing the other with a direct globbing loop.

A quick suggestion, though:

Code:
"${list[x++]}"
Regular array [index] fields operate in an arithmetic context, meaning you can do everything in them that you can do inside ((..)). You don't need '$' in front of variables (except in complex cases), and you can do the post-incrementing there instead of afterwards. Just be sure not to add the increment operator until the last instance that uses the current value!

Setting x=0 first isn't really necessary either. Uninitialized variables are treated as having a default value of zero when expanded in arithmetic contexts.

arithmetic expressions
 
Old 04-09-2013, 08:01 PM   #5
H5X00R
Member
 
Registered: Apr 2013
Posts: 39

Original Poster
Rep: Reputation: 1
Quote:
"${list[x++]}"
Thanks for the tip
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FFmpeg - Replace Audio With Audio From Another Video golmschenk Linux - Software 1 10-17-2012 02:10 PM
ffmpeg mixing audio/video - trying to end vid after video stream completes joejoe99 Linux - Software 2 06-21-2012 11:44 PM
ffmpeg grabbing audio and video glvorange Linux - Newbie 2 10-01-2011 09:56 AM
LXer: Converting video & audio files using ffmpeg in GNU/Linux LXer Syndicated Linux News 0 05-30-2009 04:31 AM
Script to automatically convert video files using ffmpeg jroyce Linux - Software 3 01-04-2008 11:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration