Reputation: 3940
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.
Upvotes: 23
Views: 27021
Reputation: 435
Maybe this can help someone.
I once dealt with this adding album art to mp3 album directories.
for f in ./*.mp3; do ffmpeg -i "$f" -i *jpg -map_metadata 0 -map 0 -map 1 -codec copy tmp_"${f#./}" && mv tmp_"${f#./}" "$f"; done
I also used nearly the same thing here.
Upvotes: 0
Reputation: 575
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.
Upvotes: 2
Reputation: 9
Neither is too annoying tmp file use. In one line:
input="InpuFile"; ffmpeg -i "$input" -y -vcodec h264 -acodec aac -strict -2 "/tmp/$input";rm "$input"; mv "/tmp/$input" .;
Upvotes: -1
Reputation: 995
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.
Upvotes: 2
Reputation: 804
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.
Upvotes: 24