how to compress mp3 file from php - php

I am using both commands in my controller, but sometimes they are not working properly.
Sometimes the compressed file size is larger than size of the original file.
exec(FFmpeg -i old.mp3 -ab 64 mp3 newfile.mp3)
And
exec(ffmpeg -i old.mp3 -acodec libmp3lame -ab 48k new.mp3)
Please, help me.

Above both command is different:
Option Description
-ab bitrate : set audio bitrate (in kbit/s)
-acodec codec : force audio codec ('copy' to copy stream)
That's why In the first command set audio stream in 64 bit, and in the second command is do the stream copy to copy.
I hope this helps.

Related

FFMPEG Converted MP3 file shows error "MPEG header 30 is missing"

I am using FFMPEG to convert all kind of media files to mp3. Files are being Converted Perfectly.
But files give error MPEG HEADER 30 is missing at 31576bytes. In All converted files I see this. And On Some MP3 Players, It do not play.
Here is my conversion Command what i use
$bitrate = '128k';
$command = "ffmpeg -i /home/musicupl/public_html/musicuploads/SavetoAccount/abc.mp4 -ar 44100 -ac 2 -b:a ".$bitrate." -acodec libmp3lame /home/musicupl/public_html/musicuploads/SavetoAccount/Output.mp3;
Is there any more tag to add MPEG Headers during the conversion. I have researched on this and found the -acodec libmp3lame but still same issue.
Is there any other way or attribute to add in command line.

Reduce file size of ffmpeg converted video

I have a .mp4 video with a file size of 540kb and a duration of 30 seconds before it was converted, but when I use ffmpeg to convert it to the file size became 21mb. What options do I need to add inorder to reduce the file size to less than 1mb?
ffmpeg command I am using right now:
video_filename = __DIR__. '/uploads/VID.mp4';
exec('ffmpeg -i '.$video_filename.' -c:v libx264 '.$video_filename.'');
Create tmp_dir in the desired location
exec('ffmpeg -i '.$video_filename.' -c:v libx264 -crf 25 tmp_dir'.$video_filename.'');
exec('rm -rf '. $video_filename);
exec('mv tmp_dir/'.$video_filename.' ..');
I hope it will work properly, I haven't test it!.

PHP/FFMPEG - why does my video conversion result in empty file?

I'm trying to convert a video that lives on my server from a .webm to a .mp4.
shell_exec('ffmpeg -i file.webm output.mp4');
However, this results in an empty mp4 (0 bytes - or, sometimes, and very weirdly, 28 bytes.) The input file exists, and is 45MB.
This answer recommended explicitly copying the input codec, but same result.
shell_exec('ffmpeg -i file.webm -vcodec copy -acodec-copy output.mp4');
What am I doing wrong?
[ ===== EDIT ===== ]
After trying a few more things, inspired by the comments, I'm still having no joy converting to MP4.
It seems I'm running FFMPEG v.2.8.15. This seems quite a lot lower than the current release of 4~, however I only installed this a week or so ago so I'm not sure why this is the case and I don't know how to update it (I'm on WHM Cpanel.)
Anyway, my problem is NOT to do with pathing, because the same command works fine if I change .mp4 to .webm - I get a successfully converted .webm file.
But when I run:
$foo = shell_exec('ffmpeg -i file.webm -vcodec copy -acodec copy output3.mp4 -report');
...I get this FFMPEG log output.
I've also tried:
shell_exec('ffmpeg -fflags +genpts -i file.webm -r 24 KKK.mp4 -report');
...from this answer, and
shell_exec('ffmpeg -i file.webm -c:v copy III.mp4');
...from this article.
Both result in the same problem, i.e. an 0-bytes .mp4 file.
Problem
You're trying to mux VP9 video and Opus audio into the MP4 container. Currently, muxing Opus into the MP4 container is considered experimental and requires -strict experimental (or -strict -2), but your ffmpeg is too old to support that. Download a new version.
Solutions
Do not mux typical WebM formats (VP9 or VP8 + Opus or Vorbis) into MP4. You can re-encode it to H.264 (or H.265) with AAC audio if you want a more common set of formats for MP4:
ffmpeg -i input -c:v libx264 -c:a aac -movflags +faststart output.mp4
Or upgrade your ffmpeg and add the -strict experimental (or -strict -2) output option to your command if you know you want VP9 + Opus in MP4:
ffmpeg -i input -c copy -strict experimental -movflags +faststart output.mp4
Or don't re-mux into MP4 in the first place. HTML5 video supports VP9 + Opus in WebM so just use the original file if it is supported by your target browser(s).

ffmpeg - convert mkv to mp4 with default subtitle

What is best way to convert mkv to mp4, with default subtitle (that mkv have), and keep same quality of video / audio ?
Currently I'm using this command
ffmpeg -y -i filename.mkv -vf subtitles='filename.mkv' -disposition:s default+forced -c:v libx264 -c:a libmp3lame -crf 27 -preset ultrafast filename.mp4
But the command still not perfect, the mkv have better quality than mp4 generated. Any advise?
Thanks
your preset is lowest possible quality, and crf could be lower, 18 is more or less lossless, this will make the transcoding slower. of course
So suggest -crf 18 and -preset slow
If you aren't bothered about which codecs you use you could just copy everything into the mp4 container, which would be megaquick

ffmpeg compresses upto 32 kbps only

I have created a PHP code which compresses mp3 while uploaded to 32kbps bit rate I have referred this thread
How to compress or convert to low quality Mp3 file from PHP
used this code
exec("ffmpeg -i inputfile.mp3 -ab 24000 outputfile.mp3")
but the problem is I can't compress more than 32kbps .my code instruct to compress up to 24kbps but after execution, I can see the output file is 32kbps. can anyone tell what should I do so that I can compress more than 32kbps .or is there any limitation of ffmpeg ??
The reason you cant achieve lower then 32kbps is because of the sample rate most likely is still 44100-Hz meaning larger stream size, you have a few options 44100-Hz, 22050-Hz, and 11025-Hz as valid frequency's.
Try (very low quality):
ffmpeg -i inputfile.mp3 -acodec libmp3lame -b:a 8k -ac 1 -ar 11025 outputfile.mp3
-b:a = audio bitrate
-ar = sample rate

Categories