ffmpeg Daemon

Make a PKGBUILD? Submit it here for user review and possible placement into the repository.

ffmpeg Daemon

Postby Socaltom » Sat Mar 17, 2012 2:44 pm

Wasn't sure where to put this, but I thought it was worth sharing. I wanted to take advantadge of the low power usage of my plug to convert some videos with ffmpeg, but the idea of leaving the terminal open on my PC for the entire time it took to convert seemed a waste of energy. I've been looking for script that would run ffmpeg in the background as a daemon but no luck. I finally came across a java daemon script. I modified it a bit and ... it works! I'm sure others can do better, and I may try to improve it over time, but for now this converts mp4 files to mpeg2 files. you can edit the ffmpeg command lines parameters in the file if you choose to. I named the script ffmpegd, and to run it the command is ffmpegd {start|stop|restart|status} and then the name of the video filename without the .mp4

I hope this is useful to others.
$this->bbcode_second_pass_code('', '#!/bin/bash
#
# chkconfig: 345 99 05
# description: Java deamon script
#
# A non-SUSE Linux start/stop script for Java daemons.
#
# Derived from -
# Home page: http://www.source-code.biz
# License: GNU/LGPL (http://www.gnu.org/licenses/lgpl.html)
# Copyright 2006 Christian d'Heureuse, Inventec Informatik AG, Switzerland.
#
# History:
# 2012-3-17 SocalTom: modified to make ffmpeg a daemon
#2010-09-21 Josh Davis: Changed 'sudo' to 'su', fix some typos, removed unused variables
# 2009-03-04 Josh Davis: Ubuntu/Redhat version.
# 2006-06-27 Christian d'Heureuse: Script created.
# 2006-07-02 chdh: Minor improvements.
# 2006-07-10 chdh: Changes for SUSE 10.0.


# Set this to your ffmpeg installation
FFMPEG_HOME=/usr/bin

serviceNameLo="ffmpegd" # service name with the first letter in lowercase
serviceName="Ffmpegd" # service name
serviceUser="root" # OS user name for the service
serviceGroup="users" # OS group name for the service
applDir="/usr/bin/" # home directory of the service application
serviceUserHome="/home/tom" # Location of video to be converted
serviceLogFile="/var/log/$serviceNameLo.log" # output goes to log file file for StdOut/StdErr
maxShutdownTime=15 # maximum number of seconds to wait for the daemon to terminate normally
pidFile="/var/run/$serviceNameLo.pid" # name of PID file (PID = process ID number)
javaCommand="ffmpeg" # name of the Java launcher without the path
javaExe="$FFMPEG_HOME/$javaCommand" # file name of the Java application launcher executable
javaArgs="-i /$serviceUserHome/$2.mp4 -vcodec mpeg2video -b:v 16384k -maxrate 30000k -bufsize 1950k -ab 448k -ar 44100 -acodec ac3 -loglevel error -copyts -f vob /$serviceUserHome/$2.mpg" # arguments for Java launcher
javaCommandLine="$javaExe $javaArgs" # command line to start the Java service application
javaCommandLineKeyword="ffmpeg" # a keyword that occurs on the commandline, used to detect an already running service process and to distinguish it from others

# Makes the file $1 writable by the group $serviceGroup.
function makeFileWritable {
local filename="$1"
touch $filename || return 1
chgrp $serviceGroup $filename || return 1
chmod g+w $filename || return 1
return 0; }

# Returns 0 if the process with PID $1 is running.
function checkProcessIsRunning {
local pid="$1"
if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
if [ ! -e /proc/$pid ]; then return 1; fi
return 0; }

# Returns 0 if the process with PID $2 is our Java service process.
function checkProcessIsOurService {
local pid="$1"
if [ "$(ps -p $pid --no-headers -o comm)" != "$javaCommand" ]; then return 1; fi
grep -q --binary -F "$javaCommandLineKeyword" /proc/$pid/cmdline
if [ $? -ne 0 ]; then return 1; fi
return 0; }

# Returns 0 when the service is running and sets the variable $pid to the PID.
function getServicePID {
if [ ! -f $pidFile ]; then return 1; fi
pid="$(<$pidFile)"
checkProcessIsRunning $pid || return 1
checkProcessIsOurService $pid || return 1
return 0; }

function startServiceProcess {
cd $applDir || return 1
rm -f $pidFile
makeFileWritable $pidFile || return 1
makeFileWritable $serviceLogFile || return 1
cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
su -m $serviceUser -s $SHELL -c "$cmd" || return 1
sleep 0.1
pid="$(<$pidFile)"
if checkProcessIsRunning $pid; then :; else
echo -ne "\n$serviceName start failed, see logfile."
return 1
fi
return 0; }

function stopServiceProcess {
kill $pid || return 1
for ((i=0; i<maxShutdownTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo -e "\n$serviceName did not terminate within $maxShutdownTime seconds, sending SIGKILL..."
kill -s KILL $pid || return 1
local killWaitTime=15
for ((i=0; i<killWaitTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo "Error: $serviceName could not be stopped within $maxShutdownTime+$killWaitTime seconds!"
return 1; }

function startService {
getServicePID
if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; return 0; fi
echo -n "Starting $serviceName "
startServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "started PID=$pid"
RETVAL=0
return 0; }

function stopService {
getServicePID
if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
echo -n "Stopping $serviceName "
stopServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "stopped PID=$pid"
RETVAL=0
return 0; }

function checkServiceStatus {
echo -n "Checking for $serviceName: "
if getServicePID; then
echo "running PID=$pid"
RETVAL=0
else
echo "stopped"
RETVAL=3
fi
return 0; }

function main {
RETVAL=0
case "$1" in
start) # starts the Java program as a Linux service
startService
;;
stop) # stops the Java program service
stopService
;;
restart) # stops and restarts the service
stopService && startService
;;
status) # displays the service status
checkServiceStatus
;;
*)
echo "Usage: $0 {start|stop|restart|status} Movie_File_Name without.mp4"
exit 1
;;
esac
exit $RETVAL
}

main $1

')
used to be owned by me
Pink Pogo V2, Black Pogo V3, Zyxel NAS 325 v1,
used to be Adminstrator for
Goflex net, Black V3, Black V2
Now I have a couple of raspberry pi ( 3+ and 4)
Socaltom
 
Posts: 571
Joined: Thu Apr 07, 2011 2:21 pm
Location: The left side

Re: ffmpeg Daemon

Postby slycat » Sat Mar 17, 2012 4:08 pm

Very cool! I've never had to convert a boat-load of movie files so I have just used screen (life-saver when you don't want to keep a terminal open) and started a conversion there. Later I then ran the same conversion without screen while piping any errors to a log and appending "&" to the end to make it run in the background. My template is somewhat like this (depending on application you may add more)
$this->bbcode_second_pass_code('', 'ffmpeg -i "$IN" -vcodec mpeg2 /media/Harddrive/Converts/"$OUT" > /var/log/convert.log 2>&1 &')

I couldn't write a java script to tell me the time of day so I've strictly made bash scripts on my PPPro. If you have a series of things to convert, I suggest making each process run in the background and double up on processes (since PPPro is dual core :) ). Below is the part of a script I wrote to convert a folder of lossless audio into apple lossless for my itunes library to read.
$this->bbcode_second_pass_code('', 'find . -name \*.flac -print0 | while read -d $'\0' Flacs
do
IF=`echo "$Flacs" | sed 's/.*\//.\//g'`
while [ `ps -A | grep -c ffmpeg` -ge 2 ]
do
sleep 5
done
OF=`echo "$IF" | sed s/\.flac$/.m4a/g`
echo "$IF -> $OF"
echo "n" | ffmpeg -i "$Flacs" -acodec alac /media/Harddrive/nas/alacs/"$OF" > /dev/null 2>&1 &
sleep 5
done')
The while-loop is what does the multicore process. I am not sure how to impliment something like this in java, but if you have a batch to convert and want to work double-time try having it start two processes in the background and wait until one is done before starting another conversion.
Pogoplug Pro w/ Wireless User -> decomm.
Cubox-i4pro User
4TB eSATA HDD (8g/3700+ Sw/Storage)
Kodi / Transmission / Minidlna / Samba / Batch-audio-conversions / Lighttpd
------------------------------
Rollback Machine - Thanks to impatt
slycat
 
Posts: 169
Joined: Wed Feb 09, 2011 3:07 am
Location: Miami, FL

Re: ffmpeg Daemon

Postby Socaltom » Sat Mar 17, 2012 5:04 pm

Screen is new to me.Everytime I post I learn something new.
Tom
used to be owned by me
Pink Pogo V2, Black Pogo V3, Zyxel NAS 325 v1,
used to be Adminstrator for
Goflex net, Black V3, Black V2
Now I have a couple of raspberry pi ( 3+ and 4)
Socaltom
 
Posts: 571
Joined: Thu Apr 07, 2011 2:21 pm
Location: The left side

Re: ffmpeg Daemon

Postby dinjo » Thu Jun 21, 2012 7:39 am

How far can it handle the load of encoding since its a CPU intensive task i wanted to encode some movies to AC3 audio can someone pass me towards right directions also what are all are dependencies ?
dinjo
 
Posts: 258
Joined: Mon Nov 28, 2011 5:59 am

Re: ffmpeg Daemon

Postby dinjo » Thu Jun 21, 2012 4:29 pm

Failing for me

:: Retrieving packages from extra...
error: failed retrieving file 'libva-1.0.15-1-arm.pkg.tar.xz' from mirror.archlinuxarm.org : The requested URL returned error: 404
warning: failed to retrieve some files from extra
error: failed to commit transaction (download library error)
dinjo
 
Posts: 258
Joined: Mon Nov 28, 2011 5:59 am

Re: ffmpeg Daemon

Postby WarheadsSE » Thu Jun 21, 2012 4:36 pm

pacman -Syy
Core Developer
Remember: Arch Linux ARM is entirely community donation supported!
WarheadsSE
Developer
 
Posts: 6807
Joined: Mon Oct 18, 2010 2:12 pm

Re: ffmpeg Daemon

Postby dinjo » Sat Jun 23, 2012 7:24 am

Thanks it worked , little offtopic here but I'm on very early version of archlinux the tarball is from month of December , so instead of running a pacman -Syu should i just get the new tar ball ? Asking this since when you upgrade there would be certain things which would be ignored (kmod etc) Whats the best approach do you recommend ?

$this->bbcode_second_pass_code('', '
[root@DINJO ~]$ uname -a
Linux DINJO 2.6.31.6_SMP_820 #99 SMP Sun May 29 03:04:43 EDT 2011 armv6l ARMv6-compatible processor rev 5 (v6l) Oxsemi NAS GNU/Linux')
dinjo
 
Posts: 258
Joined: Mon Nov 28, 2011 5:59 am

Re: ffmpeg Daemon

Postby WarheadsSE » Sat Jun 23, 2012 12:36 pm

That has all changed, you need to read the note about upgrading.
Core Developer
Remember: Arch Linux ARM is entirely community donation supported!
WarheadsSE
Developer
 
Posts: 6807
Joined: Mon Oct 18, 2010 2:12 pm

Re: ffmpeg Daemon

Postby dinjo » Tue Dec 11, 2012 11:03 am

I'm trying to encode some audio from the video file and as expected it takes a long time , looked at the cpu usage does not even hits 80% , load average around 1.5 is there a way the conversion process can be speed up ? Not sure whether swap would work or not
dinjo
 
Posts: 258
Joined: Mon Nov 28, 2011 5:59 am

Re: ffmpeg Daemon

Postby slycat » Tue Dec 11, 2012 6:20 pm

What is the source audio codec (AC3, DTS, other) and what is the desired output audio codec (AC3, mp3, ogg, other)? If you have no swap at all I am surprised youve been able to do other conversions before, should probably scrap a couple of gigs for swap anyway.
I've noticed that when I do batch conversions of audio files I never hit a high load either, 20-40% per ffmpeg process (2 for PPPro). I've also heard that no matter what using AC3/ACC/MP3 through ffmpeg tends to take a good amount of time, so for feature length films it would probably take a while regardless.
Pogoplug Pro w/ Wireless User -> decomm.
Cubox-i4pro User
4TB eSATA HDD (8g/3700+ Sw/Storage)
Kodi / Transmission / Minidlna / Samba / Batch-audio-conversions / Lighttpd
------------------------------
Rollback Machine - Thanks to impatt
slycat
 
Posts: 169
Joined: Wed Feb 09, 2011 3:07 am
Location: Miami, FL

Next

Return to User-Submitted Packages

Who is online

Users browsing this forum: No registered users and 9 guests