3

Is it possible to record a voice onto the uploaded video using PHP?

Yacoby
  • 54,544
  • 15
  • 116
  • 120
jose
  • 464
  • 4
  • 7
  • 20

7 Answers7

5

you can use the MLT library and my class. You can download it from this link https://github.com/1fer/mlt

Features

  • Cut and join videos
  • Join videos with transitions
  • 10 ready-made transitions with options
  • Customizable Wipe transitions
  • Add background audio
  • Add watermark
  • Add text overlay with options
  • Add animated text
  • Run rendering on background
  • Get rendering progress percent

To install this melt library on server use this command: sudo apt install melt

take a look at documentation how to use it, for example, to join clips use this code:

require __DIR__ . '/vendor/autoload.php';

$videoProcessing = new Andchir\VideoProcessing([
    'melt_path' => '/usr/bin/melt',
    'session_start' => true
]);

// Join clips

$videoProcessing
    ->setProfile('hdv_720_25p')
    ->addOption(['joinClips' => [
        $rootPath . '/uploads/tmp/Social.mp4',
        $rootPath . '/uploads/tmp/Dog.mp4',
        $rootPath . '/uploads/tmp/Swans.mp4'
    ]])
    ->setOutputVideoOptions($rootPath . '/uploads/tmp/out1.mp4');

// Black color and fade transition

$videoProcessing
    ->setProfile('hdv_720_25p')
    ->addOption(['inputSource' => [
        'colour:black', ['out' => 24],
        $rootPath . '/uploads/tmp/Dog.mp4'
    ]])
    ->addOption(['mix' => 25])
    ->addOption(['mixer' => 'luma'])
    ->addOption(['inputSource' => [
        'colour:black', ['out' => 24]
    ]])
    ->addOption(['mix' => 25])
    ->addOption(['mixer' => 'luma'])
    ->setOutputVideoOptions($rootPath . '/uploads/tmp/out2.mp4'); 

// Join clips with transition

$videoProcessing
    ->setProfile('hdv_720_25p')
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Swans.mp4',
        $rootPath . '/uploads/tmp/Dog.mp4'
    ]])
    ->addOption(['mix' => 25])
    ->addOption(['mixer' => 'luma'])
    ->setOutputVideoOptions($rootPath . '/uploads/tmp/out3.mp4'); 

// Cut clips and join with transition

$videoProcessing
    ->setProfile('hdv_720_25p')
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Social.mp4', ['in' => 200, 'out' => 275]
    ]])
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Dog.mp4', ['in' => 50, 'out' => 125]
    ]])
    ->addReadyMadeTransition('fade', 25)
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Swans.mp4', ['in' => 50, 'out' => 125]
    ]])
    ->addReadyMadeTransition('shiftRightIn', 25, [
        'width' => 1280,
        'height' => 720
    ])
    ->setOutputVideoOptions($rootPath . '/uploads/tmp/out4.mp4');  

// Add background audio with delay

$videoProcessing
    ->setProfile('hdv_720_25p')
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Swans.mp4', ['in' => 50, 'out' => 125]
    ]])
    ->disableAudio()
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Dog.mp4', ['in' => 50, 'out' => 200]
    ]])
    ->addReadyMadeTransition('shiftLeftIn', 25)
    ->addBackgroundAudio($rootPath . '/uploads/tmp/Reformat.mp3', ['in' => 0, 'out' => 150, 'delay' => 50])
    ->setOutputVideoOptions($rootPath . '/uploads/tmp/out7.mp4');

// Add watermark

$videoProcessing
    ->setProfile('hdv_720_25p')
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Swans.mp4', ['in' => 50, 'out' => 125]
    ]])
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Dog.mp4', ['in' => 50, 'out' => 200]
    ]])
    ->addWatermark($rootPath . '/uploads/tmp/SampleLogo.png', false, [
        'distort' => 1
    ])
    ->addReadyMadeTransition('shiftLeftIn', 25)
    ->setOutputVideoOptions($rootPath . '/uploads/tmp/out8.mp4'); 

// Add text overlay

$videoProcessing
    ->setProfile('hdv_720_25p')
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Swans.mp4', ['out' => 120]
    ]])
    ->addTextOverlay('This is my best video', true, [
        'fgcolour' => '#004fed',
        'olcolour' => '#fff200',
        'outline' => 3,
        'pad' => '50x0',
        'size' => 80,
        'weight' => 700,
        'style' => 'italic',
        'halign' => 'center',
        'valign' => 'top',
        'family' => 'Ubuntu'
    ])
    ->setOutputVideoOptions($rootPath . '/uploads/tmp/out10.mp4');

// Animated text

$videoProcessing
    ->setProfile('hdv_720_25p')
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Swans.mp4', ['out' => 120]
    ]])
    ->addTextOverlay('This is my best video', true, [
        'pad' => '50x0',
        'size' => 80,
        'halign' => 'center',
        'valign' => 'top',
        'family' => 'Ubuntu',
        'slideFrom' => 'bottom',
        'duration' => 50,
        'inOpacity' => 0,
        'outOpacity' => 100
    ])
    ->setOutputVideoOptions($rootPath . '/uploads/tmp/out11.mp4');

// Rendering

$videoProcessing
    ->setProfile('hdv_720_25p')
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Swans.mp4', ['out' => 120]
    ]])
    ->disableAudio()
    ->addOption(['inputSource' => [
        $rootPath . '/uploads/tmp/Dog.mp4', ['out' => 120]
    ]])
    ->addReadyMadeTransition('shiftLeftIn', 25)
    ->addBackgroundAudio($rootPath . '/uploads/tmp/Reformat.mp3', ['out' => 215])
    ->setOutputVideoOptions($rootPath . '/uploads/tmp/out.mp4');

// Start rendering in background

$progressLogPath = $videoProcessing->render();  

// Rendering progress

$percent = $videoProcessing->getRenderingPercent();

You can also make some filter effects like in Instagram and so on. Read more here: https://www.mltframework.org/plugins/PluginsFilters/

Roman Panevnyk
  • 313
  • 3
  • 7
  • How to set custom font family in your class? – Azaz Khan Mar 04 '20 at 21:44
  • Hi @AzazKhan , i added answer in the end of the answer – Roman Panevnyk May 12 '20 at 13:19
  • looks nice. Is it possible to add text with image background or something like that? like the idea I have is that "video1" -> "explainer with text" -> "video2" is that possible? – Toskan Dec 18 '20 at 23:42
  • Hi @Toskan , of course you can do it. Just use addTextOverlay method with addWatermark – Roman Panevnyk Dec 20 '20 at 13:51
  • the thing is addTextOverlay is overlaying to video, right? thus one loses a couple seconds from video. But I think easiest solution is just to create a video for the image + text itself – Toskan Dec 22 '20 at 02:01
  • Hi, please take a look to ['in' => 50, 'out' => 125] params – Roman Panevnyk Dec 22 '20 at 07:13
  • For example, you want to play video from 0ms to 100ms, then show the text with an image for 100 ms, and start to play the video again from 200ms to 300ms – Roman Panevnyk Dec 22 '20 at 07:19
  • $videoProcessing ->setProfile('hdv_720_25p') ->addOption(['inputSource' => [ $rootPath . 'video.mp4', ['in'=> 0, 'out' => 100] ]]) ->addTextOverlay('text', true, [ 'in' => '100', 'out' => '200', 'size' => 80, ]) ->addWatermark($rootPath . '/bg.png', false, [ 'in' => '100', 'out' => '200', 'distort' => 1 ]) ->addOption(['inputSource' => [ $rootPath . '/video.mp4', ['in'=> 200, 'out' => 300] ]]) ->setOutputVideoOptions($rootPath . '/out.mp4'); – Roman Panevnyk Dec 22 '20 at 07:19
  • sry, for that non-readable code :), but I guess you will figure it out, and understand the main logic – Roman Panevnyk Dec 22 '20 at 07:23
  • This is an amazing project, makes it easy for non MLT expert to work in editing videos, for anyone looking for know the basic of MLT check this playlist https://www.youtube.com/watch?v=8wa5zo9Srno&list=PLcUid3OP_4OWC-GJ6KfHK7dIK_yRKKn0e , I found MLT documentation very hard to follow (maybe because I'm really bad at video editing) – AFT Aug 20 '21 at 17:53
4

No you can't, at least not by just using PHP, because PHP in itself doesn't provide the necessary libraries for recording sound and editing videos.

To actually record video and sound you'll need libraries like ffmpeg (there's a handy extension for PHP) for video editing and SoX for sound installed on your server. You can then access these programmes by using the the exec() function in PHP, for example. The implementation wouldn't be that simple, though.

mensch
  • 4,411
  • 3
  • 28
  • 49
0

I'd say no, because I don't know of any Hardware-Layer (Client-Side) for PHP...it might be possible with AJAX, but I don't think so.

But PHP might be able to overlay a video with a provided sound file, that should work if you find a library for that.

Bobby
  • 11,419
  • 5
  • 44
  • 69
0

Strange idea.
PHP is for server side scripting, but record vioce you can only on client side (need sound card/codec for it)
PHP have nothing for it.
It can be more relative if you'd ask about JavaScript as it client sided. But still, most ready to use and featurefull thing for such task if Adobe Flash technology. If you not notice it -- if you right click on some flash banner, you'd see a property page where above other features you can see vioce recording.

user204724
  • 160
  • 9
0

Technically you could write a PHP plugin that binds with C libraries for audio/video editing. However, that's probably not the best path. A better option would be to invoke a ffmpeg/mencoder/... process from PHP.

StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
0

PHP is for server side scripting, but record vioce you can only on client side (need sound card/codec for it)

PHP have nothing for it.

Francesco
  • 4,052
  • 2
  • 21
  • 29
0

You can use PHP to edit video using a 3rd party service like the Shotstack video editing API. They have a PHP library that lets you perform all sorts of video editing and manipulation.

The library is available as a Composer package so you can install it using:

composer require shotstack/shotstack-sdk-php

The code below is an example of trimming a video clip start and end point:

<?php

require 'vendor/autoload.php';

use Shotstack\Client\Api\EditApi;
use Shotstack\Client\Configuration;
use Shotstack\Client\Model\Edit;
use Shotstack\Client\Model\Output;
use Shotstack\Client\Model\Timeline;
use Shotstack\Client\Model\Track;
use Shotstack\Client\Model\Clip;
use Shotstack\Client\Model\VideoAsset;

$config = Configuration::getDefaultConfiguration()
    ->setHost('https://api.shotstack.io/stage')
    ->setApiKey('x-api-key', 'REGISTER FOR A KEY');

$client = new EditApi(null, $config);

$videoAsset = new VideoAsset();
$videoAsset
    ->setSrc('https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/footage/skater.hd.mp4')
    ->setTrim(3);

$videoClip = new Clip();
$videoClip
    ->setAsset($videoAsset)
    ->setLength(8)
    ->setStart(0);

$track = new Track();
$track->setClips([$videoClip]);

$timeline = new Timeline();
$timeline->setTracks([$track]);

$output = new Output();
$output
    ->setFormat('mp4')
    ->setResolution('hd');

$edit = new Edit();
$edit
    ->setTimeline($timeline)
    ->setOutput($output);

$render = $client->postRender($edit)->getResponse();

sleep(30);

$video = $client->getRender($render->getId())->getResponse();

if ($video->getStatus() === 'done') {
    echo $video->getUrl();
}

The library is open source but the platform it self is a paid service, with free account options.

Jeff S.
  • 1,201
  • 1
  • 14
  • 17