piwigo-videojs
piwigo-videojs copied to clipboard
Multiple quality versions of uploaded video automatic generation
Hello, thank you for your work. I use the web interface to upload videos (actually, I use a modified version of ReGalAndroid, allowing for video upload). This is an enhancement request: it would be perfect that lower quality versions of videos I upload are automatically generated (like piwigo does for still pictures). Some features of your plugin already require ffmpeg, with which it is quite easy to generate whatever format. I can understand that proposing fixed formats may not be acceptable or useful for all. Providing a hook possibility would be an useful option too. Thank you in advance for any feedback on this idea / request.
I have a similar request, I'd like to compress videos after upload.
Multiple qualities would be bonus, but allowing compression for storage reduction / faster loading would be great. How can this be implemented? My request:
- Server-side compression triggered as soon as possible after upload
- Non-blocking compression, either by serving the full resolution before conversion or waiting for serving the video until compression is complete
To implement, one crude solution I can think of is:
- Run cron job every minute watching the upload directory
- On new uploads or detection of 'big' videos (criteria tbd), convert using e.g. ffmpeg to
/tmp
- After conversion, overwrite compressed video to original location
Will this work? Specifically:
- How are videos identified? Is keeping the path sufficient?
- What metadata is stored and might be incorrect?
- How to prevent conversion of files that are being uploaded?
I have a crude implementation using inotifywait
. Feedback or incorporation into the plugin would be appreciated :)
- Make
inotifywait
watcher script that converts when files are moved into upload dir. Note that files initially are uploaded to aupload/buffer/
dir, and from there moved into the repository named by year. By watching only the repository, we will not trigger on moves being uploaded toupload/buffer/
. We only watch the current year as not to overflowinotifywait
, hence this requires a reboot on January first :)
#!/bin/bash
WATCHDIR=/var/www/photos/upload/$(date +%Y)/
inotifywait --quiet -e MOVED_TO --monitor --recursive "${WATCHDIR}" | while read line
do
# line == <dir> <event> <filen>, e.g. /var/www/photos/upload/2021/02/04/ MOVED_TO 20210204135704-a4e62c48.jpg
echo "triggered " $line
params=($line)
thisfilepath="${params[0]}${params[2]}"
mime=$(file --brief --mime-type "${thisfilepath}")
# For any video, try to compress using ffmpeg. This might misfire in case of
# low-quality videos which are upscaled, but that's unlikely/impossible in my use case
if [[ "${mime}" =~ ^video/ ]]; then
echo "video triggered " $mime
# For this video, we need to:
# 1. Compress it: $file to ${thisfilepath}-x264_aac.mp4
# 2. Replace it without moving: cp ${thisfilepath}-x264_aac.mp4 $file && rm ${thisfilepath}-x264_aac.mp4
# which shouldn't trigger ourself
nice ffmpeg -hide_banner -nostdin -nostats -loglevel error -i "${thisfilepath}" -profile:v high -level 4.0 -pix_fmt yuv420p -c:v libx264 -preset slower -movflags use_metadata_tags -crf 28 -vf "scale='iw*min(1,sqrt(1280*720/ih/iw)):-2" -c:a aac -vbr 3 -threads 0 -y "${thisfilepath}-x264_aac.mp4"
cp "${thisfilepath}-x264_aac.mp4" "${thisfilepath}" && rm "${thisfilepath}-x264_aac.mp4"
echo "video conversion done"
fi
done
- Start script on reboot using e.g. cron
sudo crontab -u www-data -e
@reboot /var/www/photos/videowatcher.sh
- Test
sudo su - www-data -s /bin/bash -c '/var/www/photos/videowatcher.sh'
sudo killall videowatcher.sh