public function exportToZip(MediaResource $resource, $data) { $files = []; // get original file url $url = $resource->getMedias()[0]->getUrl(); $originalFileFullPath = $this->container->getParameter('claroline.param.files_directory') . DIRECTORY_SEPARATOR . $url; // ensure the name is clean $cleanName = preg_replace('/[^A-Za-z0-9]/', '', $resource->getName()); // create temp_dir $tempDir = $this->uploadDir . DIRECTORY_SEPARATOR . $cleanName . '_temp'; $fs = new Filesystem(); if (!$fs->exists($tempDir)) { $fs->mkdir($tempDir); } // copy original file // get original file extension $ext = pathinfo($originalFileFullPath, PATHINFO_EXTENSION); $fullFileName = $cleanName . '_full_file.' . $ext; $copiedFilePath = $tempDir . DIRECTORY_SEPARATOR . $fullFileName; // create srt file $vttFile = $tempDir . DIRECTORY_SEPARATOR . $cleanName . '_SRT.vtt'; $fs->touch($vttFile); $vtt = ''; // make a copy of the file if (copy($originalFileFullPath, $copiedFilePath)) { // create chuncked audio files in temp dir $index = 1; array_push($files, $copiedFilePath); $vtt .= 'WEBVTT' . PHP_EOL; foreach ($data as $region) { $start = $region['start']; $end = $region['end']; $duration = $end - $start; // create .vtt line $vtt .= PHP_EOL; $vtt .= $index . PHP_EOL; $vtt .= $this->secondsToSrtTime($start) . ' --> ' . $this->secondsToSrtTime($end) . PHP_EOL; if ($region['note'] !== '') { $vtt .= strip_tags($region['note']); } $vtt .= PHP_EOL; $partFilePath = $tempDir . DIRECTORY_SEPARATOR . $cleanName . '_part_' . $index . '.' . $ext; $cmd = 'ffmpeg -i ' . $copiedFilePath . ' -ss ' . $start . ' -t ' . $duration . ' ' . $partFilePath; exec($cmd, $output, $returnVar); ++$index; // cmd success if (count($output) === 0 && $returnVar === 0) { array_push($files, $partFilePath); } else { // @TODO do something in case of cmd error } } file_put_contents($vttFile, $vtt); array_push($files, $vttFile); } $zipName = $cleanName . '.zip'; $archive = new \ZipArchive(); $pathToArchive = $tempDir . DIRECTORY_SEPARATOR . $zipName; $archive->open($pathToArchive, \ZipArchive::CREATE); foreach ($files as $f) { $archive->addFromString(basename($f), file_get_contents($f)); } $archive->close(); $fs->remove($files); return ['zip' => $pathToArchive, 'name' => $zipName, 'tempFolder' => $tempDir]; }