Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geo2Grid additional option added to gtiff2mp4.sh #345

Open
jpnIII opened this issue Apr 24, 2021 · 5 comments
Open

Geo2Grid additional option added to gtiff2mp4.sh #345

jpnIII opened this issue Apr 24, 2021 · 5 comments
Milestone

Comments

@jpnIII
Copy link

jpnIII commented Apr 24, 2021

Currently, the gtiff2mp4.sh Geo2Grid bash script does not have an option to specify the mp4 loop speed. A default value of 24 frames per second is assigned within the script. This note is just to request that the next release of Geo2Grid include an updated version of gtiff2mp4.sh that allows the user to specify a desired loop speed on the gtiff2mp4.sh commandline.

I have updated the 1.0.2 version of gtiff2mp4.sh on our system (imaginator.ssec.wisc.edu) to do what I am suggesting. The updated script has been placed onto the SSEC anonymous ftp server (ftp.ssec.wisc.edu, 128.104.108.86) at:

   /pub/imaginator/users/jpn/for_dave_hoese/gtiff2mp4_jpn.sh

A user can enter either an "X" or an "x" for input argument #2 to cause the default loop speed of 24 to be used. I am also echoing to the screen the ffmpeg command that gets executed within gtiff2mp4.sh. That output was for our benefit in Room 211 -- Go ahead and remove it if you wish!

And I am also including the updated version of gtiff2mp4.sh immediately below. Thank you! Sincerely, Jim

#!/usr/bin/env bash

encoding: utf-8

Usage: gtiff2mp4.sh output.mp4 input1.tif input2.tif ...

Copyright (C) 2014 Space Science and Engineering Center (SSEC),

University of Wisconsin-Madison.

This program is free software: you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation, either version 3 of the License, or

(at your option) any later version.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

You should have received a copy of the GNU General Public License

along with this program. If not, see http://www.gnu.org/licenses/.

This file is part of the polar2grid software package. Polar2grid takes

satellite observation data, remaps it, and writes it to a file format for

input into another program.

Documentation: http://www.ssec.wisc.edu/software/polar2grid/

Written by David Hoese November 2018

University of Wisconsin-Madison

Space Science and Engineering Center

1225 West Dayton Street

Madison, WI 53706

[email protected]

dquote="

if [ -z "$POLAR2GRID_HOME" ]; then
export POLAR2GRID_HOME="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
fi

Setup necessary environments

source $POLAR2GRID_HOME/bin/env.sh
set -e

if [ $# -lt 3 ]; then
>&2 echo "Usage: gtiff2mp4.sh output.mp4 frames_per_second input1.tif input2.tif"
exit 1
fi

TMP_FRAME_DIR="gtiff2mp4_tmp"

OUTPUT_FILENAME="$1" # Get the name of the output mp4 loop via input argument #1.
shift # Shift all commandline arguments one argument to the left. The original first argument goes off the left hand side.

IN_FPS="$1" # Get the input frames per second desired for the output mp4 loop via input argument #1.

if [ "${IN_FPS}" = "X" -o "${IN_FPS}" = "x" ]; then
FPS=24
elif [ ${IN_FPS} -le 0 ]; then
>&2 echo "ERROR: Enter a positive integer for frames_per_second Usage: gtiff2mp4.sh output.mp4 frames_per_second input1.tif input2.tif"
exit 1
else
FPS=${IN_FPS}
fi
shift # Shift all commandline arguments one argument to the left. The original first argument goes off the left hand side.

INPUT_FILES=( "$@" ) # The files to be written to the mp4 loop are all remaining commandline arguments.

MAX_IMG_SIZE=${MAX_IMG_SIZE:-4096}

gdal_size() {
if [[ -z "$1" ]]; then
echo "Missing arguments. Syntax:"
echo " gdal_pixelsize_gdalwarp_tr <input_raster>"
return
fi;
EXTENT=$(gdalinfo "$1" |
grep "Size is" |
sed "s/Size is //g; s/,/ /g" |
tr "\n" " " |
tr -d "[(,])-");
echo -n "$EXTENT"
}

get_new_image_width() {
img_width=$1
img_height=$2
if [[ $img_width -ge $img_height ]]; then
if [[ $img_width -ge $MAX_IMG_SIZE ]]; then
echo $MAX_IMG_SIZE
else
echo $img_width
fi
else
if [[ $img_height -gt $MAX_IMG_SIZE ]]; then
# use ratio to get width
echo $(( $MAX_IMG_SIZE * $img_width / $img_height ))
else
echo $img_width
fi
fi
}

cleanup() {
>&2 echo "Removing temporary directory "$TMP_FRAME_DIR""
rm -rf "$TMP_FRAME_DIR"
}
trap cleanup 0 ERR

echo "Creating temporary directory for sorting frames..."
mkdir -p "$TMP_FRAME_DIR"

echo "Creating video file from: "
printf '%s\n' ${INPUT_FILES[@]}
echo "Total number of input files: ${#INPUT_FILES[@]}"

FILE_EXT=${INPUT_FILES[0]##.}

x=1
echo "Preparing images for video conversion..."
for i in "${INPUT_FILES[@]}"; do
counter=$(printf %03d $x)
img_width_height=gdal_size $i
img_width=echo $img_width_height | cut -f1 -d' '
img_height=echo $img_height | cut -f3 -d' '
new_width=get_new_image_width $img_width $img_height
if [[ $new_width -eq $img_width ]]; then
ln -s "../$i" "${TMP_FRAME_DIR}/${counter}.${FILE_EXT}"
else
echo "Scaling image to work with ffmpeg (New width=${new_width})"
gdal_translate -outsize $new_width 0 $i "${TMP_FRAME_DIR}/${counter}.${FILE_EXT}"
fi
x=$(($x+1))
done

echo "Generating animation..."

INPUT_PARAMS=${INPUT_PARAMS:--framerate ${FPS} -f image2}

OUTPUT_PARAMS=${OUTPUT_PARAMS:--c:v libx264 -crf 25 -vf "format=yuv420p,scale=trunc(iw/2)*2:trunc(ih/2)*2"}

cmd="ffmpeg -y $INPUT_PARAMS -i ${dquote}${TMP_FRAME_DIR}/%03d.${FILE_EXT}${dquote} $OUTPUT_PARAMS $OUTPUT_FILENAME"

echo
echo "($0) Execute: ${cmd}"
echo

ffmpeg -y $INPUT_PARAMS -i "${TMP_FRAME_DIR}/%03d.${FILE_EXT}" $OUTPUT_PARAMS $OUTPUT_FILENAME

echo "Done"

@djhoese
Copy link
Member

djhoese commented Apr 24, 2021

Thanks @jpnIII. Maybe it would be easiest if I converted this script to use Python and we could have a full, more complex, set of command line options. For this specifically, we could probably allow an environment variable default. This is actually what INPUT_PARAMS is doing right now. You could theoretically do:

$ INPUT_PARAMS="--framerate 48 -f image2}" gtiff2mp4.sh ...

And this would accomplish the same end goal.

@jpnIII
Copy link
Author

jpnIII commented Apr 24, 2021 via email

@djhoese djhoese added this to the Geo2Grid 1.1 milestone Mar 12, 2022
@djhoese
Copy link
Member

djhoese commented Nov 30, 2022

Unfortunately we won't have time for the G2G 1.1 release to include the changes for this script. We will put it on the TODO list for G2G 1.2. Hopefully the workaround mentioned above with the existing environment variables can be useful enough for users for the time being.

@djhoese djhoese modified the milestones: Geo2Grid 1.1, Geo2Grid 1.2 Nov 30, 2022
@kathys
Copy link
Collaborator

kathys commented Jan 10, 2024

Not addressing this until perhaps the next version.

@kathys kathys modified the milestones: Geo2Grid 1.2, Geo2Grid 1.3 Jan 10, 2024
@jpnIII
Copy link
Author

jpnIII commented Jan 11, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants