1

ffmpeg command to convert from avi file to mp4 file below does create mp4 file, but the quality of it is not all right at all. Fist, the sizes of the files are different, the mp4 is much smaller and about 1/10 of the avi file. Second, the playing time of the newly created mp4 is short lasting about 0.1 seconds where the avi file lasts about 60 seconds.

Can anyone please help me out for the ffmpeg command to create mp4 file from avi under the directory like below?

for i in /xxxxxxx/xxxxxxx/*.avi; do
    ffmpeg -i "$i" -frames:v 1  "/xxxxxxx/xxxxxxx/$(basename "$i" .avi).mp4"
done     

Log after the attempt:

[root@server-xxxxxxx-x ~]# for i in /xxxxxx/xxxxxxx/xxxxxxx/*.avi; do ffmpeg -i "$i" -pix_fmt yuv420p -movflags +faststart "/xxxxxx/xxxxxxx/xxxxxxx/$(basename "$i" .avi).mp4"; done
ffmpeg version 3.4 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-18)
  configuration:
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
 Guessed Channel Layout for Input Stream #0.1 : mono
 Input #0, avi, from '/xxxxxx/xxxxxxx/xxxxxxx/09141944_5b9b910308858.avi':
  Metadata:
    creation_time   : 2018-09-14 00:18:07
    encoder         : CanonMVI06
   Duration: 00:00:04.20, start: 0.000000, bitrate: 12999 kb/s
    Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj422p(pc,    bt470bg/unknown/unknown), 640x480, 12384 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc
     Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, mono, s16, 705 kb/s
 File '/xxxxxx/xxxxxxx/xxxxxxx/09141944_5b9b910308858.mp4' already exists.    Overwrite ? [y/N]

                                                                                                                          y
 Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> mpeg4 (native))
  Stream #0:1 -> #0:1 (pcm_s16le (native) -> aac (native))
  Press [q] to stop, [?] for help
  [swscaler @ 0x3d75fa0] deprecated pixel format used, make sure you did set   range correctly
 Output #0, mp4, to '/xxxxxx/xxxxxxx/xxxxxxx/09141944_5b9b910308858.mp4':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Video: mpeg4 (mp4v / 0x7634706D), yuv420p, 640x480, q=2-31, 200 kb/s, 30 fps, 65521 tbn, 30 tbc
    Metadata:
      encoder         : Lavc57.107.100 mpeg4
     Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
     Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 69 kb/s
     Metadata:
       encoder         : Lavc57.107.100 aac
  [mp4 @ 0x3d0e6e0] Starting second pass: moving the moov atom to the  beginning of the file17x
  frame=  126 fps=0.0 q=11.8 Lsize=     310kB time=00:00:04.20 bitrate= 603.3kbits/s speed=6.21x
  video:270kB audio:35kB subtitle:0kB other streams:0kB global headers:0kB  muxing overhead: 1.605194%
  [aac @ 0x3d36f40] Qavg: 2283.534
  ffmpeg version 3.4 Copyright (c) 2000-2017 the FFmpeg developers
   built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-18)
   configuration:
   libavutil      55. 78.100 / 55. 78.100
   libavcodec     57.107.100 / 57.107.100
   libavformat    57. 83.100 / 57. 83.100
   libavdevice    57. 10.100 / 57. 10.100
   libavfilter     6.107.100 /  6.107.100
   libswscale      4.  8.100 /  4.  8.100
   libswresample   2.  9.100 /  2.  9.100
   Guessed Channel Layout for Input Stream #0.1 : mono
   Input #0, avi, from '/xxxxxx/xxxxxxx/xxxxxxx/09141945_5b9b913e87834.avi':
    Metadata:
     creation_time   : 2018-09-14 00:18:07
     encoder         : CanonMVI06
    Duration: 00:00:04.20, start: 0.000000, bitrate: 12999 kb/s
    Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj422p(pc,      bt470bg/unknown/unknown), 640x480, 12384 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc
    Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, mono,    s16, 705 kb/s
user27240
  • 49
  • 10
  • reads like it has nothing to do with php, just ffmpeg? –  Sep 13 '18 at 22:42
  • Which, I'm not really sure about. Php, ffmpeg, or the combination of those? – user27240 Sep 13 '18 at 22:56
  • is the issue just with the results from calling ffmpeg? –  Sep 13 '18 at 22:58
  • IdontDownVote, Yes,the issue I'm facing now is the result from calling ffmpeg. The quality is not just fine and I think that the ffmpeg command is not sufficient enough to make quality file in this particular case. – user27240 Sep 13 '18 at 23:12
  • try adding `-preset slow -crf 22` if 22 is still not looking great lower it. really you just need to tweak the ffmpeg settings to get the desired result: https://www.ffmpeg.org/ffmpeg.html https://stackoverflow.com/questions/3561715/using-ffmpeg-to-encode-a-high-quality-video –  Sep 13 '18 at 23:20
  • Remove `-frames:v 1` unless you only want to output 1 frame. – llogan Sep 13 '18 at 23:31
  • LordNeckbeard, without -frames:v 1, the playing time of mp4 file has extented as long as avi file which is great. However, it seems that only audio has been synchronized, and I can not see any video from the newly created mp4 file. – user27240 Sep 14 '18 at 02:25
  • @user27240 Show the complete log from the ffmpeg command. If you're not able to do that then a guess/answer is to add `-pix_fmt yuv420p` (and `-movflags +faststart` is recommended if the video is intended for progressive playback–either way this option won't hurt). – llogan Sep 14 '18 at 16:49
  • LordNeckbeard, I've tried the recommended command at ffmpeg and still had no video playing at html, only audio is playing. I added the log on the top. – user27240 Sep 15 '18 at 00:44

1 Answers1

2

Problem: You have no supported encoder

Your configuration line is blank. This means that you do not have an H.264/H.265 video encoder enabled. So ffmpeg chooses the native encoder mpeg4 which outputs MPEG-4 Part 2 video. This is an old format which is not supported by HTML5 video.

Solutions

Compile

Compile using --enable-libx264 or --enable-libx265. You'll also need --enable-gpl to use these encoders. See this guide for instructions:

FFmpeg Wiki: Compile for RHEL/Fedora/CentOS

Or download

Alternatively, an easy method is to download an already compiled ffmpeg and put it somewhere in your PATH, such as /usr/local/bin. Or make sure your script provides the full path to the executable if you want it in an arbitrary location. Ensure that this new ffmpeg gets executed instead of your old, crippled one.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • I think openh264 supports it without having to be GPL...FWIW https://stackoverflow.com/a/39338135/32453 – rogerdpack Sep 15 '18 at 06:36
  • I've done with all of the Compilation & Installation part step by step with no error (I actually did twice.) (no updating part) and now I have ffmpeg in ffmpeg_sources folder, so it looks like [root@server-xxx-x ~]# ffmpeg_sources/ffmpeg instead of [root@server-xxx-x ~]# ffmpeg before. But, when I tried it from the same conversion command line I had the same message with before, saying Video: mpeg4. Do I have to change the command line after the change? Even after the compilation with no error it seems the compilation has not affected anything at all. I'm keep working on it. Any suggestion? – user27240 Sep 16 '18 at 08:38
  • 1
    LordNeckbeard, Thanks a lot. Finally, the avi file was converted to mp4 file by ffmpeg successfully with both audio and video. I also aplogize for the confusion I made about above comment. After I spent about good 2 days for the compilation which ended up with no result because, I guess, my os version is too old for those file, I tried the already compiled ffmpeg which took me about 15 min and it worked. I really appreciate your support. Thank you, IdontDownVote, and rogerdpack too. – user27240 Sep 16 '18 at 15:39
  • @user27240 As to why it didn't work after compiling–I'm guessing that even though you compiled `ffmpeg` your script was still executing the crippled, old `ffmpeg`. It appears you may have compiled as root, so the new `ffmpeg` would have been in `/root/bin`, but this may not have been accessible by the user executing the PHP script, so move `/root/bin/ffmpeg` to `/usr/local/bin`, or somewhere else in your `PATH`, or use the full path to the executable in the script. – llogan Sep 16 '18 at 17:37
  • LordNeckbeard, I'm confused about those three ffmpeg I've installed, but when I type whereisffmpeg and then it'll return ffmpeg: /usr/local/bin/ffmpeg so, I think the right one is where it supposed to be and I've also renamed other two ffmpegs like ffmpeg2 or 3. However, I'm facing new problem which's the chrome is the only browser I can convert from avi to mp4, and safari, IE, and others will not let me. Is there any way I can deal with this by changing ffmpeg command line? Or is there any way that I can check the way the ffmpeg was installed by log file of the conversion from avi to mp4? – user27240 Sep 17 '18 at 05:54
  • LordNeckbeard, Let me correct above comment. The chrome is the only browser that the video and the audio were converted successfully, and other browsers convert only audio like before, meaning I can not see any video when it is playing. So it seems that the already compiled ffmpeg file are not working for those non-chrome browsers. – user27240 Sep 17 '18 at 06:20
  • @user27240 You should ask that as a new question since this one is specifically addressing the lack of an appropriate encoder which was the first issue. Make sure to show your `ffmpeg` command and the complete log. – llogan Sep 17 '18 at 19:20