Calling python script through a php script accessed by the browser

4

I have a php script that will get uploaded videos and I would like to convert these videos using ffmpeg.

I created a python script that receives php parameters and calls ffmpeg to do the conversion.

index.php

<?php

$data = array('filePath' => 'video.mov');

$result = shell_exec('/usr/bin/python /home/fernando/Workspace/lab1/public_html/converter.py ' . escapeshellarg(json_encode($data)));

converter.py

#!/usr/bin/env python

import os, sys, json, subprocess, string, random

class Converter():

WORK_DIR = '/home/fernando/Workspace/lab1/public_html'
DESTINATION_DIR = '/home/fernando/Workspace/lab1/public_html/videos/'
NEW_AUDIO = 'audio.mp3'

def __init__(self, data):
    try:
        os.chdir(self.WORK_DIR)
        self.arg = data
    except:
        print "ERROR"
        sys.exit(1)     


def generateId():
    return ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase ) for _ in range(12))      


def convertVideo(self, type):

    convertedFileName = self.DESTINATION_DIR + self.generateId() + '.' + type

    typesDic = {
                'mp4': ['/usr/bin/ffmpeg', '-loglevel', 'quiet', '-i', self.arg['filePath'], '-i', self.NEW_AUDIO, '-map', '0:0', '-map', '1', '-shortest', '-codec', 'copy', convertedFileName, '-y'], 
                'ogv': ['/usr/bin/ffmpeg', '-loglevel', 'quiet', '-i', self.arg['filePath'], '-i', self.NEW_AUDIO, '-map', '0:0', '-map', '1', '-shortest', '-vcodec', 'libtheora', '-acodec', 'libvorbis',  convertedFileName, '-y'] 
               }

    sp = subprocess.Popen(typesDic[type], shell=True)

    out, err = sp.communicate()

    if err:
        return {'status': 'error'}

    return {'status': 'success', 'filename': convertedFileName}




data = json.loads(sys.argv[1])

c = Converter(data)
print c.convertVideo('mp4')
print c.convertVideo('ogv')

These codes are working the way I need them, but only if I call them   via command line . Home Ex: $ php index.php
or: $ ./converter.py '{"fileName": "video.avi"}'

If I access via browser, which was my main intention, it does not work.

What is wrong? Home Is it possible to do this via browser?
Would you have a better approach?

Edited:

Exit the apache log:

Use -h to get full help or even better, run 'man ffmpeg' ffmpeg version 1.2.6-7: 1.2.6-1 ~ trusty1 Copyright (c) 2000-2014 the FFmpeg developers built on Apr 26 2014 18:52:58 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1) configuration: --arch = amd64 --disable-stripping --enable-avresample --enable-pthreads --enable-runtime-cpudetect --extra-version = '7: 1.2.6-1 ~ trusty1' -libdir = / usr / lib / x86_64-linux-gnu --prefix = / usr --enable -bzlib --enable -libdc1394 --enable-libfreetype --enable -frei0r --enable-gnutls --enable-libgsm --enable-libmp3lame --enable-librtmp --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable -libspeex --enable-libtheora --enable-vaapi --enable -vdpau --enable-libvorbis --enable -libvpx --enable -libile --enable -gpl --enable -postproc --enable -libcdio --enable -x11grab --enable-libx264 --shlibdir = / usr / lib / x86_64-linux-gnu --enable-shared --disable-static libavutil 52. 18.100 / 52. 18.100 libavcodec 54. 92,100 / 54. 92,100 libavformat 54. 63.104 / 54. 63.104 libavdevice 53. 5.103 / 53. 5.103 libavfilter 3. 42.103 / 3. 42.103 libswscale 2. 2.100 / 2. 2.100 libswresample 0. 17.102 / 0. 17.102 libpostproc 52. 2.100 / 52. 2.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile] ... {[outfile options] outfile}

    
asked by anonymous 16.11.2014 / 02:29

1 answer

2

Finally solved. They were really the permissions. Although the destination directories of the files and scripts had the correct permissions, ffmpeg was not allowed to read the video and audio input files. After changing the permissions of the input files to ffmpeg, it worked quietly.

Thanks to everyone for the tips.

    
18.11.2014 / 12:00