
Daemon Convert Video to FLV
I decided to share the newly written demon code for video hosting.
The principle of the demon is simple. The daemon looks in the folder where the video files are downloaded, and when new ones appear there, copies to another folder, starts the conversion process in the background, creates a preview, and copies the received video to the user folder.
The script is written in bash, so in daemon mode you need to run it with the nohup command or screen. The disadvantage is the lack of load control. If file loading is active, then server overload is real. This issue is not critical for us so far, but for those who do not like this solution, the easiest way is to use the flock command to create a process queue.
The most important parameters of the daemon, you can specify through the command line. You can get a list of commands by running a script with the -h option.
Note 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 in order to change it, you will have to edit the script. But the default directory structure is this:/ video
That is, videos converted to FLV format will be added to the video folder.
And another such moment. Using the -s and -t options, you can specify the source directory and an intermediate directory for conversion, respectively. It is important to know that the file in the directory specified in the -s parameter (by default / var / videoinput) must be loaded with the name_. where this is the user ID (i.e. the name of his folder), and - The file ID, for example, the ID of the record in the database.
The preview will be copied to the same place as the video file, with the same name, but with the extension png. The preview is taken from the 16th second. In principle, for good, you need to determine the length of the clip (it can be shorter than 16 seconds), but I leave it to you as an independent work;)
From the code it’s clear, but still I will describe the necessary software for the script to work:
mencoder - actually
mplayer conversion - to cut
convert previews - ImageMagik utility for resizing thumbnails
flvtool2 - for recording meta-information in FLV Happy New Year! UPD Continuation of the article
The principle of the demon is simple. The daemon looks in the folder where the video files are downloaded, and when new ones appear there, copies to another folder, starts the conversion process in the background, creates a preview, and copies the received video to the user folder.
The script is written in bash, so in daemon mode you need to run it with the nohup command or screen. The disadvantage is the lack of load control. If file loading is active, then server overload is real. This issue is not critical for us so far, but for those who do not like this solution, the easiest way is to use the flock command to create a process queue.
The most important parameters of the daemon, you can specify through the command line. You can get a list of commands by running a script with the -h option.
Note 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 in order to change it, you will have to edit the script. But the default directory structure is this:
That is, videos converted to FLV format will be added to the video folder.
And another such moment. Using the -s and -t options, you can specify the source directory and an intermediate directory for conversion, respectively. It is important to know that the file in the directory specified in the -s parameter (by default / var / videoinput) must be loaded with the name
The preview will be copied to the same place as the video file, with the same name, but with the extension png. The preview is taken from the 16th second. In principle, for good, you need to determine the length of the clip (it can be shorter than 16 seconds), but I leave it to you as an independent work;)
From the code it’s clear, but still I will describe the necessary software for the script to work:
mencoder - actually
mplayer conversion - to cut
convert previews - ImageMagik utility for resizing thumbnails
flvtool2 - for recording meta-information in FLV Happy New Year! UPD Continuation of the article
#!/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