-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeVideoAndAudio.sh
executable file
·105 lines (94 loc) · 3.87 KB
/
MergeVideoAndAudio.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/sh
#================================================================================
# AUTHOR
# Clint Box
# https://www.youtube.com/bearcatjamboree
#
# FUNCTION
# Merge video and audio
#
# DETAILS
# This script will invoke ffmpeg with parameters required to take an input
# video file and input audio file, and merge them together, retaining audio
# channels from both the video and audio files.
#
# USAGE
# ${SCRIPT_NAME} "<input video>" "<input audio>"
#================================================================================
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
echo "${machine}"
input_video="$1"
input_audio="$2"
##############################################################################
# Check for file was passed. Show open file dialog if no argument and on Mac
###############################################################################
if ! [ -f "$input_video" ]; then
if [[ "$machine" == "Mac" ]]; then
input_video=$(osascript -e 'tell application (path to frontmost application as text)
set input_video to choose file with prompt "Please select a video to include:"
POSIX path of input_video
end')
elif [[ "$machine" == "Linux" ]]; then
input_video=$(dialog --title "Choose a file" --stdout --title "Please select a video to include:" --fselect /tmp/ 14 48)
elif [[ "$machine" == "Cygwin" ]]; then
input_video=$(dialog --title "Choose a file" --stdout --title "Please select a video to include:" --fselect /tmp/ 14 48)
elif [ "$#" -ne 2 ] || ! [ -f "$input_video" ]; then
echo "Usage: $0 input_video input_audio"
exit 1
fi
fi
if ! [ -f "$input_video" ]; then
echo "Usage: $0 input_video input_audio"
exit 1
fi
##############################################################################
# Check for file was passed. Show open file dialog if no argument and on Mac
###############################################################################
if ! [ -f "$input_audio" ]; then
if [[ "$machine" == "Mac" ]]; then
input_audio=$(osascript -e 'tell application (path to frontmost application as text)
set input_audio to choose file with prompt "Please select a audio to include:"
POSIX path of input_audio
end')
elif [[ "$machine" == "Linux" ]]; then
input_audio=$(dialog --title "Choose a file" --stdout --title "Please select a audio to include:" --fselect /tmp/ 14 48)
elif [[ "$machine" == "Cygwin" ]]; then
input_audio=$(dialog --title "Choose a file" --stdout --title "Please select a audio to include:" --fselect /tmp/ 14 48)
elif [ "$#" -ne 2 ] || ! [ -f "$input_audio" ]; then
echo "Usage: $0 input_video input_audio"
exit 1
fi
fi
if ! [ -f "$input_audio" ]; then
echo "Usage: $0 input_video input_audio"
exit 1
fi
####################################
# Remove backlashes from filepaths
####################################
file=$(echo "$input_video"|tr -d '\\')
####################################
# Separate file name from extension
####################################
ext="${file##*.}"
name="${file%.*}"
############################
# Get file path information
############################
outfile="$name"_merged
output_video=$outfile.$ext
####################################
# Make temp directory
####################################
tmp_dir=$(mktemp -d -t ci-$(date +%Y-%m-%d-%H-%M-%S)-XXXXXXXXXX)
ffmpeg -i "$input_video" -c:a copy -c:v libx264 -an "$tmp_dir/video.mp4" -vn "$tmp_dir/audio.mp3"
ffmpeg -i "$tmp_dir/audio.mp3" -i "$input_audio" -filter_complex amerge -c:a libmp3lame -q:a 4 "$tmp_dir/audiofinal.mp3"
ffmpeg -i "$tmp_dir/video.mp4" -i "$tmp_dir/audiofinal.mp3" "$output_video"
rm -rf $tmp_dir