Laravel .mov validation - laravel

In a project I want to upload video. in my request I use 'path' => 'mimes:mp4,mov,avi,mpg,mpeg;quicktime|nullable',
When uploading a .mov video I always get the error "The video path must be a file of type: mp4, mov, avi, mpg, mpeg, quicktime.". The meme type of the video is video/quicktime.
Uploading .mp4 files works perfect, didn't test with other video types yet. Does anyone have a solution?

You can manually check for mime-type if the validation is not working for you:
$video = Input::file('path');
$mime = $video->getMimeType();
$accepted_mimes = array("video/x-flv", "video/mp4", "application/x-mpegURL",
"video/MP2T", "video/3gpp", "video/quicktime",
"video/x-msvideo", "video/x-ms-wmv");
if(in_array($mime, $accepted_mimes)) {
//valid video format begin upload
} else {
//invalid video mime type
// return back with errors
return redirect->back()->withErrors(['msg', 'Invalid video']);
}
For a list of all available mime-types see here

.mov Is just a container. So maybe the mime type / codec is still wrong. You should first verify this with a tool like this: https://mediaarea.net/. As a solution to you problem however (which is less secure) you could only verify the extension (pathname).
Here you see an example of a .mxf file but with an MPEG codec to help you understand that containers do not have only one mime type (and codec) that belongs to it most of the time.
Warning for just validating file extensions: this is very insecure and could lead to all kinds of trouble. Like people uploading php files or other type of files.

$Video= request('PostDetailsVideo');//this is name of posted file
$rules=[
'PostDetailsVideo' => 'required|mimetypes:video/x-ms-wmv,video/x-msvideo,video/quicktime,video/3gpp,video/MP2T,application/x-mpegURL,video/mp4,video/x-flv|max:32768'
];
$CheckIsVideo = Validator::make($request->all(),$rules);
if($CheckIsVideo->fails()){//this not video
return response()->json([
'Success'=> false,
], 200);
}
else
return response()->json([
'Success'=> true,
], 200);

Related

Laravel Media Library Expected the file to have mimetype image/jpeg, but found a file with mimetype image/png

I have a news aggregator site that grabs an image from a remote URL but the script is breaking with the following error when I try to crop the file after grabbing:
Expected the file at /home/vagrant/code/storage/media-library/temp/pemA8JMvkodnsavxgk5ZLXXHxeU3wo7n/tiny.jpg have mimetype image/jpeg, but found a file with mimetype image/png
I have no control over this image so I was wondering if there is a way to either correct the mimetype or to catch the error and remove the offending article?
Thanks in advance for your help
edit:
For clarity I am grabbing the image with:
$article->addMediaFromUrl($item['article']['image'])->toMediaCollection('article');
and then running:
$article->save();
The model has the following function:
public function registerMediaConversions(Media $media = null): void {
$this->addMediaConversion('listing')
->fit(Manipulations::FIT_CROP, 140, 140)
->performOnCollections('article');
}
To anyone struggling with this in the future, I solved this with:
try {
$article->save();
} catch (InvalidTinyJpg $e) {
echo "Skipped invalid file\n";
}

Unable to send gifs using Twilio

I am trying to send this gif - https://media.giphy.com/media/YVBC4HdSpB7z2/giphy.gif - using Twilio but I am getting this error - Channel did not accept given content. Please see Channel specific error message for more information.
This is my code -
try{
$client = new Client(env('TWILIO_SID'), env('TWILIO_TOKEN'));
$send = $client->messages->create(
"whatsapp:".$my_phone_number, // Text this number
array(
'from' => "whatsapp:".env('TWILIO_NUMBER'),
'body' => 'hey',
'mediaUrl' => 'https://media.giphy.com/media/YVBC4HdSpB7z2/giphy.gif',
'contentType' => ['image/gif']
)
);
}catch (\Exception $exception){
}
The content-type is image/gif, which is acceptable by Twilio. So what might be the problem here?
Looking at the documentation for Twilio WhatsApp supported MIME types:
Accepted Content Types for Media
It calls out the following:
The Twilio API for WhatsApp supports sending and receiving images,
audio, PDF files, and video. The following formats are currently
supported:
Images: JPG, JPEG, PNG Audio: MP3, OGG, AMR Documents: PDF Video: MP4

Upload APK file via Gradle/Groovy Multipart Request task

I'm trying to implement a gradle task to upload an APK file to my web service using http-builder-ng. I'm struggling with the encoding part.
An APK file effectively is a ZIP format file, so I tried using the content type application/zip but it's not recognized by the encoders provided:
task publish(...) {
// ...
post {
request.contentType = 'multipart/form-data'
request.encoder 'multipart/form-data', OkHttpEncoders.&multipart
request.body = multipart {
part 'file', 'myApp.apk', 'application/zip', new File(System.getProperty('user.dir'), 'myApp.apk')
}
response.success { fs, content ->
prinln "success"
}
}
}
The error message is following:
Could not find encoder for content-type (application/zip)
Can anybody help me which encoder to use and how?

Laravel write file stream

I am using guzzle to downlaod file from url and save it into my storage.
So I have a code look like this
$response = $this->client->request('GET', $model->url, [
'stream' => true
]);
$body = $response->getBody();
while (!$body->eof()) {
Storage::append($this->filePath, $body->read(1024));;
}
but when I open the folder where my file is located, I see that the file size is changing and sometimes it is zero. So in the end I am getting a invalid file.
How can I solve this problem?

webRTC convert webm to mp4 with ffmpeg.js

I am trying to convert webM files to mp4 with ffmpeg.js.
I am recording a video from canvas(overlayer with some information) and recording the audio data from the video.
stream = new MediaStream();
var videoElem = document.getElementById('video');
var videoStream = videoElem.captureStream();
stream.addTrack(videoStream.getAudioTracks()[0]);
stream.addTrack(canvas.captureStream().getVideoTracks()[0]);
var options = {mimeType: 'video/webm'};
recordedBlobs = [];
mediaRecorder = new MediaRecorder(stream, options);
mediaRecorder.onstop = handleStop;
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.start(100); // collect 100ms of data
function handleDataAvailable(event) {
if (event.data && event.data.size > 0) {
recordedBlobs.push(event.data);
}
}
mediaRecorder.stop();
This code works as expected and returns a webm video
var blob = new Blob(recordedBlobs, {type: 'video/webm'});
Now I want a mp4 file and checked the ffmpeg.js from muaz-khan.
The examples just show how to convert to mp4 when you have 2 single streams (audio and video). But I have one stream with an additional audio track. Can I convert such a stream to mp4? How can that be done?
As per the provided code sample, your recorder stream is having only one audio & one video tracks.
If your input file is having both Audio & Video, then you need to specify output codec for both tracks here as following.
worker.postMessage({
type: 'command',
arguments: [
'-i', 'audiovideo.webm',
'-c:v', 'mpeg4',
'-c:a', 'aac', // or vorbis
'-b:v', '6400k', // video bitrate
'-b:a', '4800k', // audio bitrate
'-strict', 'experimental', 'audiovideo.mp4'
],
files: [
{
data: new Uint8Array(fileReaderData),
name: 'audiovideo.webm'
}
]
});
Trans-coding the video inside browser is not recommend, as it will consume more CPU Time & Memory. And ffmpeg_asm.js is heavy. May be ok for POC :)
What is your use case? webm(vp8/vp9) is widely using these days.
Chrome will support following mime types:
"video/webm"
"video/webm;codecs=vp8"
"video/webm;codecs=vp9"
"video/webm;codecs=h264"
"video/x-matroska;codecs=avc1"
So you can get mp4 recording directly from chrome MediaRecorder with following hack
var options = {mimeType: 'video/webm;codecs=h264'};
mediaRecorder = new MediaRecorder(stream, options);
.....
//Before merging blobs change output mime
var blob = new Blob(recordedBlobs, {type: 'video/mp4'});
// And name your file as video.mp4

Resources