Issue with overwriting file while using ffmpeg for converting

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


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.


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.

Tags:

Ffmpeg