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.
FFmpeg can input most container formats natively, including MP4, . ts, MOV, AVI, Y4M, MKV, and many others. This is the output file.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With