Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with overwriting file while using ffmpeg for converting

Tags:

ffmpeg

I'm using ffpmeg to convert all my videos to mp4:

 ffmpeg -i InpuFile -vcodec h264 -acodec aac -strict -2 OutputFile.mp4

The problem is, if I'm overwriting the input file, i.e the output and input files are the same:

 ffmpeg -i InpuFile -vcodec h264 -acodec aac -strict -2 InpuFile.mp4 -y

or

 ffmpeg -i InpuFile -y -vcodec h264 -acodec aac -strict -2 InpuFile.mp4

the new file is not good. He lasts one second and his size is extremely small.

Any ideas?

I want to use this as a script in my server so the overwriting is the most convinient way for me, I prefer that way instead of creating temporary files then replacting the temporary with original.

like image 631
izac89 Avatar asked Mar 05 '15 11:03

izac89


People also ask

Does FFmpeg work with mp4?

FFmpeg can input most container formats natively, including MP4, . ts, MOV, AVI, Y4M, MKV, and many others. This is the output file.


3 Answers

I had this same (frustrating) problem, you may have noticed that this happens because ffmpeg is writing over the file that it's reading, you are corrupting the source before the process finish... ffmpeg doesn't put the file in some buffer, so you can't do this way, you will have to use a temporary file.

just in case

like image 125
Rod Avatar answered Oct 04 '22 15:10

Rod


You cannot overwrite the input file while you are encoding. You must encode to an different output file.

Afterwards, you can replace the original file with the new encoded file.

like image 31
Andy Avatar answered Oct 04 '22 14:10

Andy


As others have mentioned, there is no way to do this without creating a temp file. You mentioned that you wanted to compress all your videos, and for it to be convenient. Here is a bash one-liner I used to compress all MP4 & MOV inside a directory:

find * -type f \( -iname \*.mp4 -o -iname \*.mov \) -execdir ffmpeg -i {} -vcodec libx265 -crf 24 temp_{} \; -execdir mv temp_{} {} \;

The -crf param controls the video bitrate. It's value ranges from 18-24, lower value is higher bitrate.

If you just wanted to compress .mp4 for example then you'd change the command to:

find * -type f -iname "*.mp4" -execdir ffmpeg -i {} -vcodec libx265 -crf 24 temp_{} \; -execdir mv temp_{} {} \;

Hope this helps OP, or anyone looking to do something similar.

like image 31
marcel Avatar answered Oct 04 '22 14:10

marcel