Piwigo / piwigo-videojs

Videojs port for piwigo. Play your videos on your web gallery!
http://piwigo.org/ext/extension_view.php?eid=610
GNU General Public License v3.0
64 stars 44 forks source link

Multiple quality versions of uploaded video automatic generation #99

Open quiniouben opened 7 years ago

quiniouben commented 7 years ago

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.

tvwerkhoven commented 3 years ago

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:

To implement, one crude solution I can think of is:

  1. Run cron job every minute watching the upload directory
  2. On new uploads or detection of 'big' videos (criteria tbd), convert using e.g. ffmpeg to /tmp
  3. After conversion, overwrite compressed video to original location

Will this work? Specifically:

  1. How are videos identified? Is keeping the path sufficient?
  2. What metadata is stored and might be incorrect?
  3. How to prevent conversion of files that are being uploaded?
tvwerkhoven commented 3 years ago

I have a crude implementation using inotifywait. Feedback or incorporation into the plugin would be appreciated :)

  1. Make inotifywait watcher script that converts when files are moved into upload dir. Note that files initially are uploaded to a upload/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 to upload/buffer/. We only watch the current year as not to overflow inotifywait, 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 == , 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

2. Start script on reboot using e.g. cron
```bash
sudo crontab -u www-data -e
@reboot /var/www/photos/videowatcher.sh
  1. Test
    sudo su - www-data -s /bin/bash -c '/var/www/photos/videowatcher.sh'
    sudo killall videowatcher.sh