⬆️ ⬇️

Demon video conversion to FLV

I decided to share the newly written code of the demon for video hosting.

The demon principle is simple. The demon looks into the folder where the video files are loaded, and when new ones appear there, copies it to another folder, starts the conversion process in the background, creates a thumbnail, and copies the resulting video to the user's folder.



The script is written in bash, so in daemon mode, it must be started using the nohup command or screen. The disadvantage is the lack of load control. If file loading is active, then server overload is real. So far this question is not critical for us, but for those to whom such a solution is not suitable, the easiest way is to use the flock command to create a queue of processes.



The most important parameters of the daemon, you can specify via the command line. You can get a list of commands by running the script with the -h parameter.



Notice the -u option. With it, you can specify the location of user folders, where the converted video will be copied. The folder structure is set rigidly, and to change it, you will have to edit the script. But the default directory structure is: <USER_ID> / video

That is, videos will be added to daddy's video clips converted to FLV format.

')

And another such moment. Using the -s and -t options, you can specify the source directory and intermediate directory for conversion, respectively. It is important to know that the file in the directory specified by the -s parameter (default / var / videoinput) must be loaded with the name <USER_ID> _ <FILE_ID>. <Avi | mpg | 3gp | ...> , where <USER_ID> this is the user ID (that is, the name of his folder), and <FILE_ID> is the file ID, for example, the record ID in the database.

The preview will be copied to the same place as the video file, with the same name, but the png extension. The preview is taken from the 16th second. In principle, for good, you need to determine the length of the video (it may be shorter than 16 seconds), but I leave it to you as an independent work;)



From the code it is clear, but still I will describe the necessary software for the script to work:

mencoder - proper conversion

mplayer - for cutting previews

convert - ImageMagik utility for resize thumbnails

flvtool2 - to write meta information to FLV



#!/bin/bash



# folder_monitor.sh

# This is a daemon shell script for monitoring video input directory.

#



#

SRC_DIR=/var/videoinput

TRG_DIR=/var/videooutput

PARAMS='-ovc lavc -lavcopts vcodec=flv:keyint=50:vbitrate=300:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -vf scale=480:360 -of lavf -oac mp3lame -lameopts abr:br=64 -srate 22050'

OUTPUT_FORMAT='flv'

USER_FOLDER="/usr/local/jboss/server/default/resources/files/user_folders"

THUMBNAIL_WIDTH=175

THUMBNAIL_HEIGHT=110



#

while getopts ":s:t:hp:u:H:W:" optname

do

case $optname in

"u")

USER_FOLDER="$OPTARG"

;;

"p")

PARAMS="$OPTARG"

;;

"s")

SRC_DIR="$OPTARG"

;;

"W")

THUMBNAIL_WIDTH="$OPTARG"

;;

"H")

THUMBNAIL_HEIGHT="$OPTARG"

;;

"t")

TRG_DIR="$OPTARG"

;;

"h")

echo "-h - help"

echo "-W - width of thumbnail"

echo "-H - height of thumbnail"

echo "-p - command line params for mencoder"

echo "-u - path to user folders"

echo "-s - source dir"

echo "-t - target dir"

exit 0;

;;

*)

echo "Unknown parameter or option error with option - $OPTARG"

exit 1;

;;

esac

done



while :

do

echo "Looking dir ${SRC_DIR}...\n"

#

FILES=$(find $SRC_DIR -type f -exec basename '{}' \;)



#

for FILE in $FILES

do

# ,

USER_ID=$(echo $FILE | sed 's/[^0-9]/ /g' | awk '{print $1}')

VIDEO_ID=$(echo $FILE | sed 's/[^0-9]/ /g' | awk '{print $2}')



#

(echo "Converting $FILE..."

#

mv ${SRC_DIR}/${FILE} ${TRG_DIR}/${FILE}

#

mplayer -ss 16 -frames 1 -vo png -nosound ${TRG_DIR}/${FILE}

THUMBNAIL="${USER_FOLDER}/${USER_ID}/video/${VIDEO_ID}.png"

#

mv 00000001.png $THUMBNAIL

#

convert $THUMBNAIL -resize ${THUMBNAIL_WIDTH} -gravity center -crop ${THUMBNAIL_WIDTH}x${THUMBNAIL_HEIGHT}+0+0 -quality 75 $THUMBNAIL

#

mencoder ${TRG_DIR}/${FILE} -o "${TRG_DIR}/${FILE}.${OUTPUT_FORMAT}" ${PARAMS}

#

flvtool2 -UP "${TRG_DIR}/${FILE}.${OUTPUT_FORMAT}"

#

rm ${TRG_DIR}/${FILE}

#

mv "${TRG_DIR}/${FILE}.${OUTPUT_FORMAT}" "${USER_FOLDER}/${USER_ID}/video/${VIDEO_ID}.${OUTPUT_FORMAT}"

) &

done



sleep 10s

done





Happy New Year!



UPD. Article continuation

Source: https://habr.com/ru/post/48187/



All Articles