diff --git a/workflow/duration.bash b/workflow/duration.bash index 91c9580..d66d2fc 100755 --- a/workflow/duration.bash +++ b/workflow/duration.bash @@ -2,7 +2,14 @@ # Copyright Ken Fallon - Released into the public domain. http://creativecommons.org/publicdomain/ #============================================================ -find ${HOME}/processing/ -type f | egrep -v '/sponsor-anhonesthost.com-hpr15.flac|/outro.flac|/intro.flac|/sponsor-archive.org.flac' | while read mediafile +search_dir="${HOME}/processing/" + +if [ -d "${1}" ] +then + search_dir="${1}" +fi + +find ${search_dir} -type f | grep -vP '/sponsor-anhonesthost.com-hpr15.flac|/outro.flac|/intro.flac|/sponsor-archive.org.flac' | while read mediafile do duration=$( mediainfo --full --Output=XML "${mediafile}" | xmlstarlet sel -T -t -m "_:MediaInfo/_:media/_:track[@type='Audio']/_:Duration[1]" -v "." -n - | awk -F '.' '{print $1}' ) if [ "${duration}" != "" ] diff --git a/workflow/process_episode.bash b/workflow/process_episode.bash index b7471e6..73b0929 100755 --- a/workflow/process_episode.bash +++ b/workflow/process_episode.bash @@ -20,6 +20,12 @@ intro_srt="${processing_dir}/intro.srt" # The intro subtitle template file piper_bin="/opt/bin/piper/piper/piper" # The location of the text to speech engine piper_voice="/opt/bin/piper/piper/piper-voices/en/en_US/lessac/medium/en_US-lessac-medium.onnx" +working_dir_bypass="false" +skip_post_show="false" +acceptable_duration_difference="2" # Seconds +unsafe_episode_variables=( shownotes_json shownotes_html hostid artist email title summary series_id series_name explicit license ep_date ep_num tags host_license host_profile remote_media shownotes_json_sanatised ) +audio_formats=( flac wav mp3 ogg opus spx ) + ################################################# # Display Error message, display help and exit @@ -39,6 +45,13 @@ function echo_debug() { fi } +################################################# +# Display Information message + +function abs_diff() { + echo $(($1 >= $2 ? $1 - $2 : $2 - $1)) +} + ################################################# # Display Help @@ -88,14 +101,50 @@ function program_checks() { } +################################################# +# Allows override of arguments from the commandline + +function argument_override() { + + echo_debug "Overriding values with terminal arguments. argument_override()" + + + + # Argument Override + if [ $# -gt 0 ] + then + for argument + do + if [[ $argument =~ ^[^=]+=.*$ ]] + then + this_key="${argument%=*}" + this_value="${argument#*=}" + for this_episode_variable in "${unsafe_episode_variables[@]}" + do + if [ "$( echo "${this_episode_variable}" | grep --perl-regexp --count "^${this_key}$" )" -ne "0" ] + then + this_value="$( echo "${this_value}" | jq --slurp --raw-input @uri | sed -e 's/%0A"$//g' -e 's/^"//g' )" + echo_debug "URL encoding \"${this_key}\" as \"${this_value}\"." + fi + done + eval "${this_key}=${this_value}" + echo_debug "Replacing \"${this_key}\" with \"${this_value}\"." + check_variable_is_correct ${this_key} + if [ "${this_key}" == "working_dir" ] + then + working_dir_bypass="true" + set_working_processing_dir + fi + fi + done + fi +} + ################################################# # Variable Checks -#album artist assets_csv assets_json comment date duration duration_iso8601 email email_unpadded ep_date episode_summary_json ep_num explicit files_json files_xml genre HOME hostid intro_srt key license license_url outro_flac outro_srt piper_bin piper_voice processing_dir shownotes_edited shownotes_html silence summary synopsis tags theme timestamp_epoc title track working_dir year - function check_variable_is_correct() { - for argument in "$@" do case "${argument}" in @@ -296,14 +345,20 @@ function check_variable_is_correct() { echo_error "The \"media\" variable is missing." fi ;; + media_basename) + if [[ ! -d "${media_basename}" || -z "${media_basename}" ]] + then + echo_error "The \"media_basename\" variable is missing or is not a directory." + fi + ;; outro_flac) - if [[ ! -d "${outro_flac}" || -z "${outro_flac}" ]] + if [[ ! -s "${outro_flac}" || -z "${outro_flac}" ]] then echo_error "The \"outro_flac\" variable is missing or file \"${outro_flac}\" does not exist." fi ;; outro_srt) - if [[ ! -d "${outro_srt}" || -z "${outro_srt}" ]] + if [[ ! -s "${outro_srt}" || -z "${outro_srt}" ]] then echo_error "The \"outro_srt\" variable is missing or file \"${outro_srt}\" does not exist." fi @@ -346,6 +401,21 @@ function check_variable_is_correct() { # echo_error "The \"shownotes_html\" variable has not a valid \"text/html\" mime type. \"${shownotes_html}\"" # fi ;; + shownotes_json) + if [[ ! -s "${shownotes_json}" || -z "${shownotes_json}" ]] + then + echo_error "The \"shownotes_json\" variable/file is missing." + fi + if [ "$( file --brief --mime-type "${shownotes_json}" | grep --count 'application/json' )" -ne "1" ] + then + echo_error "The \"shownotes_json\" variable has not a valid \"application/json\" mime type." + fi + jq '.' "${shownotes_json}" >/dev/null 2>&1 + if [ $? -ne 0 ] + then + echo_error "The file \"${shownotes_json}\" is not valid json." + fi + ;; silence) if [[ ! -d "${silence}" || -z "${silence}" ]] then @@ -371,7 +441,7 @@ function check_variable_is_correct() { fi ;; theme) - if [[ ! -d "${theme}" || -z "${theme}" ]] + if [[ ! -s "${theme}" || -z "${theme}" ]] then echo_error "The \"theme\" variable is missing or file \"${theme}\" does not exist." fi @@ -400,6 +470,12 @@ function check_variable_is_correct() { echo_error "The \"working_dir\" variable is missing or is not a directory." fi ;; + working_processing_dir) + if [[ ! -d "${working_processing_dir}" || -z "${working_processing_dir}" ]] + then + echo_error "The \"working_processing_dir\" variable is missing or is not a directory." + fi + ;; year) if [[ -z "${year}" || "${year}" == "null" ]] then @@ -549,15 +625,25 @@ function get_working_dir() { echo_debug "Found working directory as \"${working_dir}\"" - if [ ! -d "${working_dir}/processing/" ] - then - mkdir -v "${working_dir}/processing/" - if [ ! -d "${working_dir}/processing/" ] - then - echo_error "Could not create the processing directory \"${working_dir}/processing/\"." - fi - fi +} +################################################# +# Create a sub dir to store technical data. + +function set_working_processing_dir() { + + echo_debug "Checking the processing directory in the current working directory. set_working_processing_dir()" + + check_variable_is_correct working_dir + + working_processing_dir="${working_dir}/processing" + + if [ ! -d "${working_processing_dir}/" ] + then + mkdir -v "${working_processing_dir}/" + fi + + check_variable_is_correct working_processing_dir } ################################################# @@ -566,6 +652,14 @@ function get_working_dir() { function get_episode_metadata() { echo_debug "Extracting the episode metadata. get_episode_metadata()" + + if [[ -n "${skip_post_show}" && "${skip_post_show}" == "true" ]] + then + echo_debug "The Episode hpr${ep_num} has already been posted. Skipping get_episode_metadata()" + return + fi + + check_variable_is_correct working_dir if [[ -s "${working_dir}/shownotes.json" && "$( file --brief --mime-type "${working_dir}/shownotes.json" | grep --count "application/json" )" -eq 0 ]] then @@ -575,6 +669,8 @@ function get_episode_metadata() { shownotes_json="${working_dir}/shownotes.json" shownotes_html="${working_dir}/shownotes.html" shownotes_edited="${working_dir}/shownotes_edited.html" + + check_variable_is_correct shownotes_json hostid="$( jq --raw-output '.host.Host_ID' ${shownotes_json} )" artist="$( jq --raw-output '.host.Host_Name' ${shownotes_json} )" @@ -598,11 +694,8 @@ function get_episode_metadata() { echo_debug "Extracting shownotes html from json file." jq --raw-output '.episode.Show_Notes' "${shownotes_json}" > "${shownotes_html}" - - if [ ! -s "${shownotes_html}" ] - then - echo_error "Failed to extract the shownote html file \"${shownotes_html}\"" - fi + + check_variable_is_correct shownotes_html ( echo '
' cat "${shownotes_html}" @@ -610,9 +703,9 @@ function get_episode_metadata() { ' ) | sponge "${shownotes_html}" - episode_variables=( shownotes_json shownotes_html hostid artist email title summary series_id series_name explicit license ep_date ep_num tags host_license host_profile remote_media shownotes_json_sanatised ) + unsafe_episode_variables=( shownotes_json shownotes_html hostid artist email title summary series_id series_name explicit license ep_date ep_num tags host_license host_profile remote_media shownotes_json_sanatised ) - for episode_variable in "${episode_variables[@]}" + for episode_variable in "${unsafe_episode_variables[@]}" do if [[ -z ${!episode_variable} && "${episode_variable}" != "remote_media" ]] then # indirect expansion here @@ -622,26 +715,7 @@ function get_episode_metadata() { fi done - # Argument Override - if [ $# -gt 0 ] - then - for argument - do - if [[ $argument =~ ^[^=]+=.*$ ]] - then - this_key="${argument%=*}" - if [ "$( echo "${episode_variables[*]}" | grep --count "${this_key}" )" -eq "0" ] - then - echo_debug "Skipping replacing \"${this_key}\"."; - continue - fi - this_value="${argument#*=}" - this_value="$( echo "${this_value}" | jq --slurp --raw-input @uri | sed -e 's/%0A"$//g' -e 's/^"//g' )" - eval "${this_key}=${this_value}" - echo_debug "Replacing \"${this_key}\" with \"${this_value}\"." - fi - done - fi + argument_override "$@" # Hosts need to exist in the database if [ "${hostid}" == '0' ] @@ -657,6 +731,12 @@ function get_episode_metadata() { function extract_images_brute_force() { echo_debug "Extracting images with grep. extract_images_brute_force()" + + if [[ -n "${skip_post_show}" && "${skip_post_show}" == "true" ]] + then + echo_debug "The Episode hpr${ep_num} has already been posted. Skipping extract_images_brute_force()" + return + fi if [ -s "${shownotes_edited}" ] then @@ -803,6 +883,8 @@ function extract_images_brute_force() { function media_checks() { echo_debug "Running media checks. media_checks()" + + check_variable_is_correct working_dir working_processing_dir if [[ -n "${remote_media}" && "${remote_media}" != "null" ]] then @@ -849,6 +931,8 @@ function media_checks() { then echo_error "Could not find the subtitles for media \"${media}\"" fi + + #TODO fix close duration # Find duration duration=$( mediainfo --full --Output=JSON "${media}" | jq --raw-output '.media.track | .[] | select(."@type"=="Audio") | .Duration' | awk -F '.' '{print $1}' ) @@ -857,6 +941,10 @@ function media_checks() { echo_error "Invalid duration found in \"${media}\"" >&2 fi echo_debug "The Duration is \"${duration}\" seconds from \"${media}\"" + + + + #TODO fix close duration # Find number of channels ( 1=mono or 2=stereo) supplied_channels=$( mediainfo --full --Output=JSON "${media}" | jq --raw-output '.media.track | .[] | select(."@type"=="Audio") | .Channels' ) @@ -867,20 +955,20 @@ function media_checks() { echo_debug "The number of audio channels is \"${supplied_channels}\" from \"${media}\" ." # Gernerate the Spectrum and Waveform image - ffmpeg -hide_banner -loglevel error -y -i "${media}" -lavfi "showspectrumpic=s=960x540" "${working_dir}/processing/${media_basename%.*}_spectrum.png" - audio2image.bash "${media}" && mv -v "${media%.*}.png" "${working_dir}/processing/${media_basename%.*}_waveform.png" + ffmpeg -hide_banner -loglevel error -y -i "${media}" -lavfi "showspectrumpic=s=960x540" "${working_processing_dir}/${media_basename%.*}_spectrum.png" + audio2image.bash "${media}" && mv -v "${media%.*}.png" "${working_processing_dir}/${media_basename%.*}_waveform.png" # Getting metadata - mediainfo "${media}" > "${working_dir}/processing/${media_basename%.*}_mediainfo.txt" + mediainfo "${media}" > "${working_processing_dir}/${media_basename%.*}_mediainfo.txt" - exiftool "${media}" > "${working_dir}/processing/${media_basename%.*}_exiftool.txt" + exiftool "${media}" > "${working_processing_dir}/${media_basename%.*}_exiftool.txt" for check_file in spectrum.png waveform.png mediainfo.txt exiftool.txt do - if [ ! -s "${working_dir}/processing/${media_basename%.*}_${check_file}" ] + if [ ! -s "${working_processing_dir}/${media_basename%.*}_${check_file}" ] then - echo_error "The ${check_file} file was not generated for the \"${working_dir}/processing/${media_basename%.*}_${check_file}\"" >&2 + echo_error "The ${check_file} file was not generated for the \"${working_processing_dir}/${media_basename%.*}_${check_file}\"" >&2 fi done @@ -902,6 +990,13 @@ function media_checks() { function generate_initial_report() { echo_debug "Generating the initial report. generate_initial_report()" + + if [[ -n "${skip_post_show}" && "${skip_post_show}" == "true" ]] + then + echo_debug "The Episode hpr${ep_num} has already been posted. Skipping generate_initial_report()" + return + fi + # TODO list the images. @@ -964,25 +1059,25 @@ ${shownotes_json_sanatised}
-$( cat "${working_dir}/processing/${media_basename%.*}_mediainfo.txt" )
+$( cat "${working_processing_dir}/${media_basename%.*}_mediainfo.txt" )
-$( cat "${working_dir}/processing/${media_basename%.*}_exiftool.txt" )
+$( cat "${working_processing_dir}/${media_basename%.*}_exiftool.txt" )
-
+
-
+
@@ -1006,7 +1101,7 @@ $(cat "${shownotes_srt}" )