Call python script through a php script accessed by browser

Question:

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

I created a python script that receives parameters from php 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 to, but only if I call them via the command line .
Ex: $ php index.php
or: $ ./converter.py '{"fileName": "video.avi"}'

If I access via browser, which was my main intention, it doesn't work.

What could be wrong?
Is it possible to do this via browser?
Would there be a better approach?

Edited:

Output from 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-zlib –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.2100 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}

Answer:

Finally resolved. It was 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 smoothly.

Thanks everyone for the tips.

Scroll to Top