This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Ffmpeg.sh

95 lines
2.9 KiB
Bash
Raw Normal View History

2023-12-07 05:11:26 +03:00
# Mux audio into containers. File names in sound and current dirrectories must match.
2023-12-07 04:02:47 +03:00
# Usage: ffmpeg_mux_audio <SOUND> <OUTPUT DIR>
2023-12-07 01:44:42 +03:00
function ffmpeg_mux_audio() {
2023-12-07 04:02:47 +03:00
if [[ "${1}" = "" ]]; then
help ffmpeg_mux_audio
2023-12-05 21:50:45 +03:00
return 2
fi
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
for file in *; do ffmpeg -i "$file" -i "$1"/"$file" -c copy -map 0:v:0 -map 1:a:0 -shortest "$2"/"$file"; done
2023-08-08 16:24:15 +03:00
}
2023-11-26 01:23:30 +03:00
# Mux cover into music file.
# Usage: ffmpeg_mux_cover <FORMAT> <COVER>
2023-12-07 01:44:42 +03:00
function ffmpeg_mux_cover() {
2023-12-05 21:50:45 +03:00
if [[ "${1}" = "" ]]; then
2023-12-07 04:02:47 +03:00
help ffmpeg_mux_cover
2023-12-05 21:50:45 +03:00
return 2
fi
local format="${1}"
local cover="${2}"
mkdir out
case "${format}" in
# "mka"|"mkv")
# for file in *.${format}; do
# ffmpeg -i "${file}" -attach "${cover}" -map 0 -c copy -metadata:s:t mimetype="image/${cover##*.}" -metadata:s:t:0 filename="cover.${cover##*.}" "./out/${file}" || return 1
# done
# ;;
*)
for file in *.${format}; do
ffmpeg -i "${file}" -i "${cover}" -map 0 -map 0:-v? -map 1 -codec copy -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" -disposition:v attached_pic ./out/"${file}" || return 1
done
;;
esac
mv out/* .
rm -d out/ && rm "${2}"
2023-11-26 01:23:30 +03:00
}
2023-12-07 04:02:47 +03:00
# Generate music metadata from directory structure.
2023-12-07 05:02:12 +03:00
# Top dir is the Artist name like this: `The_Beatles`.
# Next are albums like this: `2010_My_love`.
# Inside are songs like this: `01_sample.flac`.
# Usage: ffmpeg_music_meta <FORMAT>
2023-12-07 01:44:42 +03:00
function ffmpeg_music_meta() {
2023-12-05 21:50:45 +03:00
if [[ "${1}" = "" ]]; then
2023-12-07 04:02:47 +03:00
help ffmpeg_music_meta
2023-12-05 21:50:45 +03:00
return 2
fi
2023-11-30 02:00:14 +03:00
2023-12-05 21:50:45 +03:00
local format="${1}"
2023-12-05 21:50:45 +03:00
ls *.${format} &> /dev/null || return 1
2023-12-05 21:50:45 +03:00
local artist="${PWD%/*}"; artist="${artist##*/}"; artist="${artist//_/ }"
local album="${PWD##*/}"; album="${album#*_}"; album="${album//_/ }"
local year="${PWD##*/}"; year="${year%%_*}"
# local total=$(ls *.${format} | wc -l)
2023-11-30 02:00:14 +03:00
2023-12-05 21:50:45 +03:00
mkdir out
2023-11-30 02:00:14 +03:00
2023-12-05 21:50:45 +03:00
for file in *.${format}; do
local track="${file%%_*}"; track=$((10#${track})); [[ "${track}" = "" ]] && track=0
local title="${file#*_}"; title="${title%.*}"; title="${title//_/ }"
2023-12-05 21:50:45 +03:00
# echo "${artist}; ${album}; ${year}; ${track}; ${title}"
# TODO: make it format-specific.
ffmpeg -i "${file}" -map 0 -c copy -metadata "artist=${artist}" -metadata "album_artist=${artist}" -metadata "album=${album}" -metadata "date=${year}" -metadata "year=${year}" -metadata "date_released=${year}" -metadata "track=${track}" -metadata "part_number=${track}" -metadata "title=${title}" ./out/"${file}" || return 1
done
2023-11-30 02:00:14 +03:00
2023-12-05 21:50:45 +03:00
mv out/* .
rm -d out/
2023-11-30 02:00:14 +03:00
}
2023-12-07 04:02:47 +03:00
# Get video FPS.
2023-12-07 01:44:42 +03:00
function _ffprobe_fps() {
2023-12-05 21:50:45 +03:00
local fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "${1}")
2023-12-13 16:45:52 +03:00
[[ "${fps}" = "" ]] && fps=24 || fps=$((fps))
2023-12-05 21:50:45 +03:00
echo "${fps}"
}
2023-12-07 04:02:47 +03:00
# Get recommended keyframe interval for a file.
2023-12-05 23:56:04 +03:00
_ffprobe_keyint() {
2023-12-05 21:50:45 +03:00
local fps=$(_ffprobe_fps "${1}")
echo $((fps*5))
}
2023-12-07 04:02:47 +03:00
# Get audio bitrage. 128 by default.
2023-12-07 01:44:42 +03:00
function _ffprobe_ba() {
2023-12-05 21:50:45 +03:00
local ba=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "${1}")
[[ "${ba}" != "N/A" ]] && echo $((ba/1024)) || echo 128
}