User avatar
RPi_Mike
Posts: 91
Joined: Sat Dec 09, 2017 12:57 am
Location: United States

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Sun Dec 24, 2017 11:25 am

Gadgetguy wrote:
Sun Dec 24, 2017 7:37 am
Sorry I didn’t mean to hijack your thread ... Could you tell me whether your version of ffmpeg yielded the ffplay component of ffmpeg ... would you be able to test play a hd video with ffplay ... I gathered from your tutorial that your video output from mpv only comes in two flavours: full screen and the native size of the video ... I wonder if it would be too much to ask if you could try the hack ... to output ... to a window of specified size at a predetermined location...

As with your last extremely long post about the supposedly wonderful benefits of the SMPlayer GUI, FFplay is yet another off-topic matter. My tutorial doesn't even mention FFplay! Nonetheless, as a courtesy, I will answer your technical question.

I did not tamper with anything in the FFmpeg source code, so yes – my build creates everything in there, including the standard FFplay component. FFplay is more of a testing tool and was never intended as a standard media player. That's not my opinion – it's the description from the official FFmpeg website: FFplay "is mostly used as a testbed for the various FFmpeg APIs".

The standard version of FFplay that comes with FFmpeg's source code does NOT support hardware acceleration – at least not on the Raspberry. As a result, it is simply NOT able to play HD video without massive stuttering. I hesitate to even discuss this matter because not only is it off-topic, but it might confuse novice readers and create the false impression I'm somehow revealing a deficiency in my build. It is NOT a deficiency, nor is it even slightly relevant to my tutorial. Quite frankly, I'm not even sure why you're asking about FFplay. And please – I don't even want to know. That can only lead to even more confusing off-topic material on this thread. But I have no doubt you have your reasons. You've admitted to being a Linux beginner that loves to fiddle with your Raspberry and conduct all kinds of experiments. That's your business, and if you want to create a separate thread entitled "Gadgetguy's FFplay experiments", have at it!

Finally, you gathered wrong about the capabilities of my mpv build. Its behavior is EXTREMELY CUSTOMIZABLE and is NOT limited to "two flavors". You can absolutely control the size and location of any video. It's not a "hack" either – it's just one of the HUNDREDS of standard customizable features that come with mpv. Personally, when I want to enjoy a video, I'm not looking to "multitask" – especially not on a $35 computer with limited resources. When I want to enjoy a video, I want to enjoy a video! And again, I honestly don't want to clutter this thread with why you like to do that. It's just not relevant. But as you requested, I temporarily added ONE line to mpv's configuration file (mpv.conf) to shrink a full-blown 1080p video down to a tiny overlay. Here are two quick examples:

EXAMPLE 1 – TINY VIDEO IN UPPER-LEFT CORNER:

geometry=20%+0+0

The numerical values in the above line are saying "make the video 5 times smaller (20%) across the diagonal and 5 times smaller (20%) down the vertical. In the aggregate, that means it's 5 x 5 = 25 times smaller than full screen. In terms of surface area, that's only 4% of my screen! The "0+0" refers to the X and Y location you want for the UPPER-LEFT CORNER of the video. In this case, it means "I want the top-left corner of my video to appear 0 pixels from the left of my monitor, and 0 pixels from the top of my monitor".

EXAMPLE 2 – TINY VIDEO IN UPPER-RIGHT CORNER:

For this example, simple math is in order. My monitor is 1920 pixels across. 20% of 1920 is 384. And 1920 - 384 = 1536. That means the upper-left corner of the video should start at 1536 on the X axis and 0 on the Y axis. As a result, the following line will place a tiny video in the upper-right corner of my screen:

geometry=20%+1536+0

In both of these examples, tapping the "f" key instantly toggles the video back and forth between FULL SCREEN and TINY SCREEN. Mpv handles it beautifully in the blink of an eye.

I previously included other brief examples of the many things you can do to customize mpv (in Appendix 1 of my tutorial). But if you want to know any more about the full range of options available in the latest version of mpv, please visit their official website and read their extremely thorough manual:

https://mpv.io/manual/stable/

crispn
Posts: 5
Joined: Tue Jan 24, 2017 1:35 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Thu Dec 28, 2017 11:38 am

Hi,

I came upon this tutorial and have been stepping through, literally, partly as a way of familiarizing myself with linux but also hoping to be able to digitize some old camcorder DV tapes I have lying around.

So a Big Thanks, first of all to Rpi_Mike for the effort he's made.

Anyhow, I get to step 10, (libass subtitle renderer) and on the './configure --enable-shared' step I am getting...

configure: error: Package requirements (fribidi >= 0.19.0) were not met:

No package 'fribidi' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables FRIBIDI_CFLAGS
and FRIBIDI_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.


Has anyone else encountered this and know of a solution?

Debian version 9.3 and I didn't do the rpi_update.

User avatar
RPi_Mike
Posts: 91
Joined: Sat Dec 09, 2017 12:57 am
Location: United States

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Fri Dec 29, 2017 2:03 am

crispn wrote:
Thu Dec 28, 2017 11:38 am
Big Thanks, first of all to Rpi_Mike for the effort he's made.

Anyhow, I get to step 10, (libass subtitle renderer) and on the './configure --enable-shared' step I am getting...

configure: error: Package requirements (fribidi >= 0.19.0) were not met: No package 'fribidi' found

{I have} Debian version 9.3

My initial comment is not directed to you or your post – it's just a general observation for ALL readers: My tutorial was hard to create but it's RELATIVELY easy to follow. The key word is "relatively"! In other words, all you need to do is "mindlessly" copy and paste my command lines into the Terminal window. As long as you meet the simple conditions in my "requirements" section and follow my two basic "warnings", you should be OK. However, it's extremely easy to make mistakes on mindless tasks! There are more than 70 separate copy and paste steps in my procedure. If the user accidentally fails to correctly copy even one character, FUHGETTABOUTIT! If the user accidentally gets two lines out of sequence, FUHGETTABOUTIT! If the user accidentally skips a step, FUHGETTABOUTIT! And finally, if the user decides to "modify" my instructions even slightly, who knows what might happen!

I have found the best way to run through my tutorial is to have two (and only two) non-overlapping windows on your screen – the Terminal window on your left and the Text Editor on your right. That way, you can easily copy and paste between the two windows and keep track of the exact step you're on. (Please see the "Critical Tip" section of my tutorial for more detail.)

Now, let me address the specific error you report. I obviously didn't see what you did in the moments leading up to your problem, so all I can do is start with the most likely cause and work my way down.

The key element in your error message is this:

"configure: error: Package requirements (fribidi >= 0.19.0) were not met: No package 'fribidi' found"

In plain English, that means that either (1) a package named fribidi is not currently installed on your system or (2) a "sub-package" referred to as fribidi – which comes as part of a larger package – is not currently installed on your system.

In this case, since there's no such thing as a package with the exact name of "fribidi", the second scenario applies. All the "fribidi" stuff you need comes with the package named "libfribidi-dev". The "lib" prefix means it's a software library and the "dev" suffix means it's for "development" purposes – building from source code, for example.

It's also letting you know that even if you have "fribidi" installed on your system, that by itself is not good enough. Instead, it must also be at least version 0.19.0 – OR GREATER!

Due to my careful efforts, I'm pleased to report that my tutorial already included the correct package. Not only that, the version number is more than sufficient as well. Here's proof:

In Step 3 of my tutorial, you will see the following stanza:

sudo apt-get install git wget autoconf automake build-essential pkg-config checkinstall libsdl2-dev libtool libva-dev libvdpau-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev texinfo libfribidi-dev libfontconfig1-dev libfreetype6-dev libjpeg-dev gnutls-dev libluajit-5.1-dev python-docutils libbluray-dev libtheora-dev libvorbis-dev linux-headers-rpi2 libomxil-bellagio-dev

As you can see, "libfribidi-dev" is highlighted in bold. It's one of the 27 standard software packages that my instructions download and install from the official Raspbian Stretch repository – via the standard "sudo apt-get install" command.

To check on the exact version number that's currently in the repository, enter the following command in Terminal:

apt show libfribidi-dev

As long as you first ran "sudo apt-get update" to update your available package list – which my tutorial instructs you to do in Step 2 – you should get the following results (as of this writing). The results are verbose, so I'm only including the first two relevant lines:

Package: libfribidi-dev
Version: 0.19.7-1

This version of "fribidi" is more than sufficient to meet the needs of libass. However, this is *NOT* telling you that you have this package and version number on your system! It's only telling you that this item is AVAILABLE in the package repository.

To find out if you actually have the correct package installed on your system, you need to run this:

dpkg --status libfribidi-dev

If you followed my instructions in Step 3 correctly, it should yield the following results (truncated again to show only the relevant lines):

Package: libfribidi-dev
Status: install ok installed
Priority: optional
Source: fribidi
Version: 0.19.7-1

As you can see, since version 0.19.7 is "equal to or greater than" version 0.19.0, the package included in my tutorial is more than sufficient to meet the requirements of the libass subtitle renderer. If, however, you got a message that says "package 'libfribidi-dev' is not installed" – or it shows an older version number – it means you probably didn't install all the required dependencies listed in Step 3. There are other possibilities, however. I will address those in a moment.

I didn't make up any of these package requirements, by the way – these (mandatory) dependencies are solely dictated by FFmpeg, mpv, and the various encoders. Mpv, for example, will not compile without libass. And two of the dependencies are required by the Raspberry's unique GPU architecture.

If the entire stanza with all 27 packages in Step 3 was properly copied and pasted IN ONE SHOT – as indicated by my instructions – it's hard to imagine how one package buried in the middle of all that could end up missing. In fact, it's basically impossible. However, if for some reason a user chose to break the stanza up into separate chunks – perhaps to examine the resulting Terminal feedback for each individual package – it would greatly increase the chance that the user might make a transcription error. I'm not suggesting you did this, but it's certainly reasonable for me to mention this possibility.

Now, there are some other things that could generate this error message. One of them would be adding your own "prefix" to the "./configure" line in Step 10. Another would be downloading something other than the exact version of "libass-0.13.7.tar.gz" that's listed in the "wget" line of Step 10. All of this, of course, would fall under the "do not modify my instructions" rule.

Also, be aware that there's no specific intelligence behind the error message's reference to "environmental variables". That's generic text that CAN be useful, as a consideration, in some circumstances. But if you followed my instructions to the letter, there's absolutely no reason to mess around with environmental variables. Unless you're a very advanced user, attempting to redefine the paths is only going to dig an even deeper hole.

Hopefully, my observations have shed light on your problem. But if it's still not clear what the issue is, I would try the following troubleshooting steps: First, completely delete the "libass-0.13.7" folder, as well as the "libass-0.13.7.tar.gz" file that you downloaded via wget in Step 10 (libass). Both of these items should be in the "FFmpeg_Build" folder. After you've deleted both items, completely re-do Step 3 to make sure you have all the required dependencies. Don't worry – the package manager will automatically know what's already been installed and not installed – so just re-run the entire stanza with all 27 packages in it. Then, start Step 10 (libass) all over again from scratch! Also, if you previously chose to ignore the "fribidi" error and then decided to skip Step 10 entirely, you'll have to re-do every step thereafter from scratch – Step 11, Step 12, etc. That's because there is NO MERCY with a procedure like this! All steps must work correctly. And all steps must be done in the correct sequence. If even the slightest corner is cut, my tutorial will not work properly.

If, after all that, you're *STILL* getting weird error messages, there's only one remaining course of action: Download NOOBS (or Raspbian if you're more advanced) from raspberrypi.org/downloads. If you choose NOOBS, make sure it's regular NOOBS and not NOOBS "lite". And if it's Raspbian, make sure it's regular Raspbian – not Raspbian "lite". All of this is mentioned in my "requirements" section. You would then use the downloaded image file to create a brand-new, unaltered, fresh copy of Raspbian on another SD card. Or, if you're a more advanced user and have backed up all your data and have access to a second computer, you could NUKE your existing SD card and re-install a fresh copy of NOOBS or Raspbian. Creating a brand-new SD card is the only way to know for certain that you have a clean, undamaged, unaltered copy of Raspbian Stretch before you begin my tutorial – or anything else you wish to do on your system! And if you do go to that trouble, make sure you use the "SD Card Copier" tool to make a duplicate of your brand-new SD card on a second SD card. It only takes 10 minutes! That way, if you encounter any weird problems with anything in the future, you can be back in business in minutes instead of hours. (Please see "Warning #2" in the instructions section of my tutorial for more detail.)

One last thing: You said you have "Debian" version 9.3. I realize that Raspbian is almost completely based on Debian, but if you're running the standard version of the Raspberry Pi's official operating system on a real Raspberry Pi, it's simply not possible that you have "Debian" 9.3.

I'm guessing you either meant to say Raspbian, or maybe you're rather technical and ran this command:

cat /etc/debian_version

That will yield the answer "9.3" if your system is fully updated (as of this writing). But that's just a generic command that's common to Debian and most Debian variants (including Raspbian). However, to get SPECIFIC information on your Raspberry's operating system, you need to run this command instead:

lsb_release -a

If your system is fully updated, it will yield the following results (as of this writing):

No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.3 (stretch)
Release: 9.3
Codename: stretch

NOTE: In case you get a "command not found" message, first run this: sudo apt-get install lsb-release

If the above results say anything other than Raspbian 9.3 (Stretch), it means you either have the wrong operating system or an older version of the current operating system. To make sure you have the very latest version of Raspbian Stretch, be sure to run "sudo apt-get update" and then "sudo apt-get dist-upgrade" (after safely backing up your data, of course).

Finally, just to make sure you really have a Raspberry Pi 3, run this command:

cat /proc/device-tree/model

It should yield this answer:

Raspberry Pi 3 Model B Rev 1.2

crispn
Posts: 5
Joined: Tue Jan 24, 2017 1:35 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Fri Dec 29, 2017 11:13 am

RPi_Mike - Thanks for taking the time to analyse my problem and reply to my post. It is appreciated.

First of all, to clarify my 'Debian 9.3' comment, yes I did do a 'cat /etc/debian_version'.

If I do the 'lsb_release -a' I get this...

No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.3 (n/a)
Release: 9.3
Codename: n/a

Note that I have 'n/a' instead of stretch - not sure if that is relevant?

My Pi3 started life as Jessie Lite with a USB hard drive holding the OS in one partition and another partition being used for storing ip camera recordings. I upgraded this to Stretch rather than load the Stretch image directly as I wanted to avoid having to re-load the OS via the SD card onto the USB HDD. I had thought that the Lite version would satisfy my needs as I was primarily interested in encoding DV camcorder tapes rather than viewing anything on the Pi.

Although I was copying and pasting the steps from one screen to the other I think it's possible that I did in fact miss step 3 in my haste. I have tried your suggestion of deleting the libass folder and tar.gz file and repeating step 3 but this gives me a new problem (which in itself corroborates my theory that I missed step 3 or I would have seen this the first time).

When I do the entire step 3 stanza I get the following...

The following packages have unmet dependencies:
linux-headers-rpi2 : Depends: linux-headers-4.9.0-4-rpi2 but it is not going be installed
E: Unable to correct problems, you have held broken packages.


It looks as though there may be an underlying problem with my installation and you stress the importance of this in your reply, the provenance of my OS is not the same as yours, in particular the fact that I upgraded Jessie rather than starting with a Stretch image, and so I think I will take several steps back and start afresh with a virgin Stretch (non-lite) image and take it from there. This will take me some time.

Once again I do appreciate the time you have put into both the original tutorial and your reply - it certainly is fulfilling one of my aims - to become more familiar with Linux. It has also taught me a new word - stanza - that should be worth a few points in scrabble!

Wishing you and all forum readers a Happy 2018.

User avatar
RPi_Mike
Posts: 91
Joined: Sat Dec 09, 2017 12:57 am
Location: United States

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Sat Dec 30, 2017 8:59 pm

crispn wrote:
Fri Dec 29, 2017 11:13 am
RPi_Mike - Thanks for taking the time to analyse my problem and reply to my post. It is appreciated.

My Pi3 started life as Jessie Lite with a USB hard drive holding the OS in one partition and another partition being used for storing ip camera recordings. I upgraded this to Stretch rather than load the Stretch image directly as I wanted to avoid having to re-load the OS via the SD card onto the USB HDD. I had thought that the Lite version would satisfy my needs as I was primarily interested in encoding DV camcorder tapes rather than viewing anything on the Pi.

Although I was copying and pasting the steps from one screen to the other I think it's possible that I did in fact miss step 3 in my haste.

I'M GLAD I COULD HELP:
Above all, I appreciate your honesty in revealing your system's non-standard nature – on both the hardware level and the software level.

It's always frustrating when something doesn't work, no matter what the actual reason. And frustration can quickly turn to anger. So for some, it would have been very tempting to simply blame my tutorial without disclosing the full story. What impresses me even more is that your identity, like almost everyone else on here, is completely anonymous. It reminds me of the saying: "The true test of a man’s character is what he does when no one is watching." So thank you, crispn!

You clearly have a good sense of the situation at this point, so the rest of this post is solely for the benefit of other readers:


HARDWARE:
A standard Raspberry Pi has its operating system on a standard SD card in the standard internal SD card slot. Can you boot a Raspberry from other devices? Yes you can. Is it possible that a NSA-sponsored consortium of researchers from Stanford and MIT have secretly figured out how to boot a Raspberry from an external, partitioned, USB-connected holographic memory cube? Yes, I suppose that's possible. But none of these arrangements would qualify as a standard Raspberry Pi configuration. Would my tutorial work on such a system? WHO KNOWS! But let me quote the Raspberry Pi's official documentation on this matter:

Be warned that this feature is experimental and does not work with all USB mass storage devices.

SOURCE: https://www.raspberrypi.org/documentati ... des/msd.md


OPERATING SYSTEM, PART I:
Will my tutorial work on a "lite" version of Raspbian just as well as the standard, full version of Raspbian? NO IT WILL NOT. In fact, it will NOT WORK AT ALL! This is explicitely stated in my "REQUIREMENTS" section.


OPERATING SYSTEM, PART II:
Will my tutorial work on a version of Raspbian Stretch that was "upgraded" from Raspbian Jessie? Maybe it will, maybe it won't. But don't take my word for how much faith we can have in such a system. Instead, I will close with the Raspberry Pi Foundation's official release statement:

RASPBIAN STRETCH HAS ARRIVED FOR RASPBERRY PI

How to get Raspbian Stretch

As this is a major version upgrade, we recommend using a clean image; these are available from the Downloads page on our site as usual.

Upgrading an existing Jessie image is possible, but is not guaranteed to work in every circumstance.


SOURCE: https://www.raspberrypi.org/blog/raspbian-stretch/

crispn
Posts: 5
Joined: Tue Jan 24, 2017 1:35 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Wed Jan 03, 2018 8:32 pm

Hi Rpi_Mike,

Sorry to be a pain but I'm still encountering a problem.

I have started from scratch by downloading the Raspian Stretch (non-lite) image onto a fresh SD card. The only divergences between my installation and those in the tutorial are these ...
1) running a USB HDD with the sd card only acting at power up to point to the HDD root (i.e. not booting from the hdd)
2) installed motioneye (not motioneyeos) as I have 2 other Pi's with cameras feeding video streams
3) installed samba so I can share the motioneye captured videos to a windows desktop
4) running headless, using putty as SSH
I don't think these should make a difference, but happy to be advised otherwise.

This has been running for a few days and is perfectly stable.

cat /proc/device-tree/model

Code: Select all

Raspberry Pi 3 Model B Rev 1.2
lsb_release -a

Code: Select all

No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 9.3 (stretch)
Release:        9.3
Codename:       stretch
so far so good.

Now when I follow the steps 1 thru 11 ( I actually printed them off and ticked each sub-step as I went, just to be sure) I have no problems or warnings until I get to step 11 on the make-j4 command. This gives (after about 20 minutes)...


Code: Select all

libavcodec/libtheoraenc.c: In function ‘encode_init’:
libavcodec/libtheoraenc.c:211:5: warning: ‘avcodec_get_chroma_sub_sample’ is deprecated [-Wdeprecated-declarations]
     avcodec_get_chroma_sub_sample(avc_context->pix_fmt, &h->uv_hshift, &h->uv_vshift);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from libavcodec/libtheoraenc.c:39:0:
libavcodec/avcodec.h:5703:6: note: declared here
 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CC      libavcodec/libvorbisenc.o
CC      libavcodec/libvpx.o
CC      libavcodec/libvpxdec.o
CC      libavcodec/libvpxenc.o
CC      libavcodec/libx264.o
CC      libavcodec/ljpegenc.o
libavcodec/libx264.c: In function ‘X264_frame’:
libavcodec/libx264.c:282:9: error: ‘x264_bit_depth’ undeclared (first use in this function)
     if (x264_bit_depth > 8)
         ^~~~~~~~~~~~~~
libavcodec/libx264.c:282:9: note: each undeclared identifier is reported only once for each function it appears in
libavcodec/libx264.c: In function ‘X264_init_static’:
libavcodec/libx264.c:892:9: error: ‘x264_bit_depth’ undeclared (first use in this function)
     if (x264_bit_depth == 8)
         ^~~~~~~~~~~~~~
ffbuild/common.mak:60: recipe for target 'libavcodec/libx264.o' failed
make: *** [libavcodec/libx264.o] Error 1
make: *** Waiting for unfinished jobs....

Now I have done some googling on this and I think this post may be significant, as it refers to versions of libx264 and recent changes.

https://github.com/jb-alvarado/media-au ... issues/675

Sorry to be a pain as I am at the limit of my Linux knowledge, but am I right in thinking that the version of libx264 that I got in the step 11 stanza (the ./configure step has '--enable-libx264' as the 4th parameter) is incompatible with the version of ffmpeg obtained just 3 sub-steps earlier, also in step 11? If that's the case, surely all recent followers of this tutorial will be experiencing the same problem?

Happy to help with running diagnostics, and thanks in advance.

User avatar
RPi_Mike
Posts: 91
Joined: Sat Dec 09, 2017 12:57 am
Location: United States

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Thu Jan 04, 2018 2:40 am

crispn wrote:
Wed Jan 03, 2018 8:32 pm
Sorry to be a pain as I am at the limit of my Linux knowledge, but am I right in thinking that the version of libx264 that I got in the step 11 stanza (the ./configure step has '--enable-libx264' as the 4th parameter) is incompatible with the version of ffmpeg obtained just 3 sub-steps earlier, also in step 11? If that's the case, surely all recent followers of this tutorial will be experiencing the same problem?

There's nothing wrong with the "version" of libx264 in Step 11. The configure line is simply enabling the x264 encoder when FFmpeg compiles. So even though your error message presents itself in Step 11, the source of your problem is actually in Step 5.

Before I explain all this, however, let me step back and provide a 30,000-foot view:

A sly lawyer would love the last sentence of my "Fully Tested and Proven to Work" section. It clearly states the following: "At the time of this writing, I can confidently say that as long as you follow my instructions to the letter, you will achieve success!"

That means my words of confidence became null and void on December 13, 2017 – just one day after my tutorial was posted!

For example, the Raspberry Pi Foundation might issue a standard system update that could unintentionally "break" a critical dependency that my tutorial relies on. The update itself might be perfectly legitimate and represent an overall improvement for the Raspberry – but it could still trigger an unknown sequence of events that would make my dated statements utterly non-applicable. This could happen on any random day, week, or month.

If I were Microsoft or Apple, I would have the resources and money to assign a dedicated team to constantly test and re-test my tutorial as the larger software environment continuously evolved. But I'm doing all this for FREE, so my tutorial is definitely an "as is" offering.

Now, back to the issue you encountered: It appears that two weeks *AFTER* my tutorial was published, something beyond my control happened to the x264 source code. Since my build of FFmpeg depends on x264, that could easily trigger problems during the compile phase of FFmpeg.

Since you're still learning the Linux ropes, you might find some value in this brief explanation:

In somewhat oversimplified terms, source code typically comes in two basic forms:

1: Constantly updated "git" versions (sometimes referred to as "daily snapshots").

AND

2: "Locked-in" stable release tarballs.

They each have their pros and cons. In theory, since "git" versions are typically updated on a daily or weekly basis, they offer the latest improvements. The downside is that because these inherently experimental versions are continually evolving, unanticipated things can happen!

There's also a cultural element to all this. Once the code inside a tarball is locked in, the developers generally move on and don't look back. They have a vested interest in keeping everyone focused on the latest "git" version instead of the old tarballs – simply because they have a good-faith desire to get continuous feedback and make improvements on the CURRENT code (not the old code). They also assume that if you're advanced enough to compile from source code, you must also be advanced enough to live life on the bleeding edge!

But when it comes to RELIABILITY and PREDICTABILITY, there's nothing like a "stable release" tarball. That's because you're dealing with FIXED CODE that will never change. This allows you to test and validate a static target, instead of a constantly changing target.

I was aware of all this when I created my tutorial, so I wanted to use predictable tarballs whenever possible. The bottom line is that my tutorial worked perfectly when I tested and published it. It therefore made no sense to take an unnecessary risk of "breaking it" with constantly changing git code. Unfortunately, due to the cultural element I just described, some of the developers have made it extremely difficult or impossible to find the "stable release" tarballs! In other words, they sometimes hide them on their websites in undocumented folders. They much prefer to keep pushing the current git code, so they often have no incentive to make the location of the tarballs obvious. For example, if you go to the official x264 website, all you'll see are the git versions and "daily tarballs". Although the latter has the word "tarball" in it, you'll also note that it has the word "daily" in it. These are basically the same thing – they both represent the current, "non-stable" release of the source code.

Fortunately, after some Googling, I found the "secret" location of the x264 stable release tarballs! Sure enough, they've been placed in an unadvertised folder.

I have therefore updated Step 5 of my tutorial (libx264) to grab a stable release tarball. Previously, Step 5 pulled the daily git version with the git clone command (with a URL ending in ".git"). But now it uses the wget command to pull a stable release tarball (with a URL ending in "stable.tar.bz2").

Since Step 5 has now fundamentally changed – and because it has a direct impact on the success of Step 11 – you must now do the following to start from a CLEAN SLATE:

1: sudo dpkg -r x264

2: Completely delete the "x264" folder inside the "FFmpeg_Build" folder

3: Completely delete the "ffmpeg-3.4" folder inside the "FFmpeg_Build" folder

4: Completely re-do my newly updated Step 5 (libx264)

5: Completely re-do Step 11 (FFmpeg)

That's it! After Step 11 is re-done, proceed to Step 12, etc.

I'm almost afraid to find out what happens since I don't have control or oversight of your system – or full knowledge of what you're actually doing. Good luck and let me know what happens!

PS: If and when you get through all this, there's something to look forward to. Once you've saved the freestanding Debian software packages in Step 13, all the source code will be permanently "baked in". In my case, since I followed my own tutorial weeks ago, I'm now IMMUNE to any change in the x264 source code – or any other source code in my procedure. As long as my operating system – Raspbian Stretch – does not undergo some unusual change, my pre-built software packages should still work. Is that a guarantee? No. Is anything guaranteed on any computer? No. But once I have those pre-built packages in my possession, the range of things that could affect me are greatly limited.

crispn
Posts: 5
Joined: Tue Jan 24, 2017 1:35 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Thu Jan 04, 2018 11:10 pm

Hi Rpi_Mike

Well I got there in the end. I had to do the rpi_update step as I was getting an error on step 12 related to 'bcm_host' and figured that it was to do with the videocore components (after a bit of googling). Anyhow, 'sudo rpi_update' and a rerun of step 12 has now got me to the end of the steps, including backing up the .deb files.

Once again, thank you so much for posting this - it really is appreciated that you have put together this tutorial. I certainly have learned more than I bargained for, especially regarding linux packages, tarballs, git and compiling source code, but that will be useful for me in the future, I'm sure.

I owe you a beer or two!

jerryrp
Posts: 18
Joined: Sat Dec 23, 2017 7:39 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Sun Jan 07, 2018 12:38 pm

Works nice, I just wish all tutorials was like this one! Many times you stumble upon some posts claiming: "this will be totally complete manual" followed by several lines of code with no explanation. Not the case here)))

Pretty happy right now. Mpv+yt-dl is really a killer-combination for all video hosts (mainly youtube for me). I am using yt-dl on Windows PC for a long time, and have some webm videos that i coudn't play on RPi until now. Thanks again.

richardac
Posts: 1
Joined: Wed Jan 10, 2018 1:27 am

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Wed Jan 10, 2018 1:36 am

Thanks! I've been struggling with other encoders and been scared of ffmpeg. But these instructions are easy to follow, with no 'magic' and worked great for me. I built on a raspberry pi including command 'sudo rpi-update'. I then copied .deb files as instructed to another pi, but skipped the 'sudo rpi-update' on that one. Everything works fine, i'm encoding frame grabs from a webcam into a timelapse video and serving up on pi based website. (Biggest problem was forgetting to include PATH of some lib files in crontab ..)

treeHouse
Posts: 29
Joined: Sat Aug 06, 2016 2:35 am

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Fri Jan 12, 2018 4:38 am

I have been wanting to follow this tutorial for weeks, and finally chipped away at it over the last few days. This is an "above and beyond" tutorial. Seriously fine work! Thanks Rpi_mike!

Works wonderfully as stated, and keeps the pi resources free. Like having the movie window in a corner for some tutorial or whatnot! Love this.

Thanks again, and now I have a new toy to explore....

iskabin
Posts: 1
Joined: Thu Jan 18, 2018 12:30 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Thu Jan 18, 2018 12:39 pm

Hello!

First, thanks for sharing all of this!

I wonder, is it possible to achieve this performance without the encoding part? And if is, what are the steps I should follow?

I ask this because I have no intention to use Pi as an encoding machine. All I need is to be able to play videos with great performance on Chromium, using it's default player, or another player that is available...

I'm already able to play 1080p videos seamlessly (both through streaming and from the MicroSD card), although with my actual configurations I'm only able to achieve this performance on some MP4 videos - and to tell the truth, I don't really know why. I must be able to play any full hd video with no framedrops or anything like this (specially .webm).

Thanks in advance.

Lucas.

User avatar
RPi_Mike
Posts: 91
Joined: Sat Dec 09, 2017 12:57 am
Location: United States

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Fri Jan 19, 2018 6:37 pm

iskabin wrote:
Thu Jan 18, 2018 12:39 pm
I wonder, is it possible to achieve this performance without the encoding part? And if it is, what are the steps I should follow? I ask this because I have no intention to use Pi as an encoding machine. All I need is to be able to play videos with great performance.....

Yes! If you do not want the latest and most advanced encoding capabilities, you can easily modify my tutorial and create a stripped-down "play-centric" version. Such a version would be perfectly suitable for those who are mainly interested in PLAYING video and audio (with mpv) – not encoding it (with FFmpeg).

It's worth noting that since mpv requires the FFmpeg "engine", this stripped-down version would still include the many native encoders that come with FFmpeg's "stock" source code. For example, you would still be able to encode H.264 video and AAC audio – but instead of using the award-winning x264 and Fraunhofer implementations of these encoders, FFmpeg would use its built-in generic encoders. The command lines to do the encoding would slightly change as well, since the generic and advanced versions of these encoders do not use the same syntax – but those details can easily be found on the FFmpeg website. For some compression schemes, such as the older MP3 audio standard, FFmpeg does not include a native encoder. My full tutorial, however, addresses this in Step 9 by building the highly regarded LAME encoder for MP3s. I realize you're not interested in encoding, but I wanted to include this paragraph for the benefit of other readers.

Since you specifically mention WEBM videos, keep in mind that at the hardware level, the Raspberry's GPU only supports H.264 (MP4) – not VP9 (WEBM). However, thanks to some very efficient FFmpeg and mpv code, the Raspberry's CPU is still able to crank out 1080p WEBM videos at 30 FPS with an extremely low (or zero) frame drop count. For WEBM, the exact performance will depend on 3 factors: native frame rate, interframe motion, and the computational complexity of the scenes. Nonetheless, even the most demanding 1080p WEBM videos at 30 FPS will typically achieve a frame throughput that exceeds 99.7%. My testing also shows that 720p WEBM videos at 30 FPS will always have a 100% frame throughput. Finally, thanks to direct GPU support, 1080p H.264 videos are guaranteed to have a 100% frame throughput – even at 60 FPS! See the "Testing Results" section of my tutorial for detailed benchmarks.

Now, back to your main question. Here's a quick summary of the differences between my full tutorial and a stripped-down "play-centric" version of my tutorial:


MY FULL TUTORIAL BUILDS AND INSTALLS THESE 8 ITEMS (BEGINNING WITH 5 ADVANCED ENCODERS):

  • x264 video encoder
  • AAC audio encoder (Fraunhofer)
  • VP9 video encoder
  • Opus audio encoder
  • MP3 audio encoder (LAME)
  • libass subtitle renderer
  • FFmpeg
  • mpv

A STRIPPED-DOWN "PLAY-CENTRIC" VERSION OF MY TUTORIAL WOULD ONLY BUILD AND INSTALL THESE 3 ITEMS:

  • libass subtitle renderer (required for mpv to work)
  • FFmpeg (required for mpv to work)
  • mpv


Thus, to create a stripped-down version of my tutorial without the advanced encoders, there are just 4 "concepts" you need to apply:


CONCEPT A: DO EVERY SINGLE STEP IN MY TUTORIAL – EXCEPT THESE:

Skip steps 5, 6, 7, 8, 9

In other words, you would not build and install the 5 advanced encoders.



CONCEPT B: CAREFULLY REMOVE THESE 5 ENCODER ITEMS FROM THE "./configure" LINE IN STEP 11 (FFmpeg):

--enable-libx264 --enable-libfdk-aac --enable-libvpx --enable-libopus --enable-libmp3lame



CONCEPT C: RUN THIS EXTRA COMMAND LINE RIGHT BEFORE YOU RUN "sudo checkinstall make install" IN STEP 11 (FFmpeg):

sudo mkdir -p /usr/local/share/doc



CONCEPT D: RUN "sudo rpi-update" IN STEP 2 (full system update):

To make certain my stripped-down method works, I just tested it on a brand-new installation of Raspbian Stretch. Everything works perfectly! It plays 1080p videos just as well as my full tutorial does – both MP4/H.264 and WEBM/VP9. My testing revealed, however, that "sudo rpi-update" is mandatory, not optional. Without it, mpv will simply not compile. Before you proceed, please make note of my tutorial's common-sense warning about basic "data safety", and the importance of having a backup copy of your operating system on a separate SD card.

I hope that answers your question. Let me know how everything works out for you!
Last edited by RPi_Mike on Tue Jan 23, 2018 12:48 am, edited 1 time in total.

insolito
Posts: 17
Joined: Fri Feb 14, 2014 11:44 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Mon Jan 22, 2018 1:10 pm

Hello, thanks for sharing!

With this, can i run a video in a custom resolution bordeless at any point of my screen by terminal?

I mean, my resolution is 1080p and i want to run a video at 200x300 screen position at 600x400 size bordeless by terminal like a widget, can i do that?

Thanks!

jerryrp
Posts: 18
Joined: Sat Dec 23, 2017 7:39 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Wed Jan 24, 2018 3:34 pm

insolito wrote:
Mon Jan 22, 2018 1:10 pm
Hello, thanks for sharing!

With this, can i run a video in a custom resolution bordeless at any point of my screen by terminal?

I mean, my resolution is 1080p and i want to run a video at 200x300 screen position at 600x400 size bordeless by terminal like a widget, can i do that?

Thanks!
Of course you can -- RPi_Mike already explained how to do it with this post.

insolito
Posts: 17
Joined: Fri Feb 14, 2014 11:44 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Thu Jan 25, 2018 9:58 am

Of course you can -- RPi_Mike already explained how to do it with this post.
Got it, Thanks!

Now i am able to run videos from my GTK# mono program using it:
scr.png
scr.png (6.15 KiB) Viewed 3996 times
But i cant use the interactive constrols like LEFT ARROW/RIGHT ARROW to Seek backward/forward, F KEY to Toggle fullscreen or 9/0 KEYS to Decrease/increase volume because i have no terminal to get these pressed keys

There is a way to simulate these keys to have control of the video playback? Something like a bash command (I can run it within mono) or even a way to use mpv inside a mono program as a component? (didnt find a solution for GTK# like a widget)

Thanks!

User avatar
RPi_Mike
Posts: 91
Joined: Sat Dec 09, 2017 12:57 am
Location: United States

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Thu Jan 25, 2018 5:03 pm

insolito wrote:
Thu Jan 25, 2018 9:58 am
Now i am able to run videos from my GTK# mono program using it....... But i cant use the interactive controls....... because i have no terminal to get these pressed keys....... Is there a way to simulate these keys to have control of the video playback? Something like a bash command (I can run it within mono) or even a way to use mpv inside a mono program as a component?

A big part of the beauty and power of mpv is that it has no GUI baggage to slow things down and clutter the screen. It's not a limitation – IT'S A FEATURE! It's a deliberate philosophy that's especially well-suited to the Raspberry – a tiny computer with limited resources. Thanks in part to that streamlined approach, my tutorial is able to squeeze out 60 frames per second at 1080p on a $35 computer!

Mpv, by design, is a terminal-based program. That's an mpv decision – not mine. It's a decision I happen to agree with completely. If you install the generic stock version of mpv from the official Raspbian repository, you will find that it behaves the exact same way – so your question has nothing specifically to do with my tutorial or the build it creates. No matter what the build or version, mpv absolutely requires a normal instance of the standard Terminal program running in the background at all times. It must also be the active window or you will lose keyboard control. [In case any novices are reading this, don't let any of this scare you. You do NOT use Terminal when playing videos. Instead, you just double-click any media file you want and mpv automatically starts playing it! Yes, the Terminal window is activated – but it runs quietly in the background. You don't see it or interact with it. It's "out of sight, out of mind."]

Yet you say you "have no terminal". That would definitely be a deal killer!

Here's the bottom line: I can tell you're at least somewhat technically advanced and are trying to build or fiddle with some kind of GUI-based application that either interacts with mpv or has mpv inside it. Unfortunately, to be completely honest, that has NOTHING to do with my tutorial or the topic of this thread. It's totally off-topic and will only confuse the vast majority of readers. The requirements section of my tutorial explicitly states that it's intended for a completely STANDARD install of Raspbian Stretch. It certainly makes no mention of GTK# wrappers, Mono, widgets, or the C# programming language.

Don't get me wrong – I'm not saying there's anything wrong with whatever it is you're trying to do. I think it's great that people love to experiment and try new things on their own. But your exotic experiments are definitely a topic for a separate thread!

Finally, if you'd like to know more about my thoughts on GUIs and mpv, here's an extensive posting I made on that topic last month:

viewtopic.php?f=38&t=199775#p1248745

insolito
Posts: 17
Joined: Fri Feb 14, 2014 11:44 pm

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Fri Jan 26, 2018 1:25 am

RPi_Mike wrote:
Thu Jan 25, 2018 5:03 pm
insolito wrote:
Thu Jan 25, 2018 9:58 am
Now i am able to run videos from my GTK# mono program using it....... But i cant use the interactive controls....... because i have no terminal to get these pressed keys....... Is there a way to simulate these keys to have control of the video playback? Something like a bash command (I can run it within mono) or even a way to use mpv inside a mono program as a component?

A big part of the beauty and power of mpv is that it has no GUI baggage to slow things down and clutter the screen. It's not a limitation – IT'S A FEATURE! It's a deliberate philosophy that's especially well-suited to the Raspberry – a tiny computer with limited resources. Thanks in part to that streamlined approach, my tutorial is able to squeeze out 60 frames per second at 1080p on a $35 computer!

Mpv, by design, is a terminal-based program. That's an mpv decision – not mine. It's a decision I happen to agree with completely. If you install the generic stock version of mpv from the official Raspbian repository, you will find that it behaves the exact same way – so your question has nothing specifically to do with my tutorial or the build it creates. No matter what the build or version, mpv absolutely requires a normal instance of the standard Terminal program running in the background at all times. It must also be the active window or you will lose keyboard control. [In case any novices are reading this, don't let any of this scare you. You do NOT use Terminal when playing videos. Instead, you just double-click any media file you want and mpv automatically starts playing it! Yes, the Terminal window is activated – but it runs quietly in the background. You don't see it or interact with it. It's "out of sight, out of mind."]

Yet you say you "have no terminal". That would definitely be a deal killer!

Here's the bottom line: I can tell you're at least somewhat technically advanced and are trying to build or fiddle with some kind of GUI-based application that either interacts with mpv or has mpv inside it. Unfortunately, to be completely honest, that has NOTHING to do with my tutorial or the topic of this thread. It's totally off-topic and will only confuse the vast majority of readers. The requirements section of my tutorial explicitly states that it's intended for a completely STANDARD install of Raspbian Stretch. It certainly makes no mention of GTK# wrappers, Mono, widgets, or the C# programming language.

Don't get me wrong – I'm not saying there's anything wrong with whatever it is you're trying to do. I think it's great that people love to experiment and try new things on their own. But your exotic experiments are definitely a topic for a separate thread!

Finally, if you'd like to know more about my thoughts on GUIs and mpv, here's an extensive posting I made on that topic last month:

viewtopic.php?f=38&t=199775#p1248745
Ok, sorry!

BTW, got a solution to my problem using JSON IPC (Sockets) :)

mikedice417
Posts: 5
Joined: Mon Nov 16, 2015 1:03 am

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Sun Jan 28, 2018 11:41 pm

Hi,
Thank you for this post! I have learned a lot by reading it and going through the steps.

I have encountered several errors at the end though.

Here they are:
During the waf build part of building mpv, I did run into the build error shown below.

I fixed this by adding this command
export C_INCLUDE_PATH=/opt/vc/include

I did this because I found the file bcm_host.h in /opt/vc/include/bcm_host.h
===== Error =====
../video/out/opengl/context_rpi.c:21:22: fatal error: bcm_host.h: No such file or directory
#include <bcm_host.h>
^
compilation terminated.

Waf: Leaving directory `/home/pi/FFmpeg_Build/mpv-0.27.0/build'
Build failed
-> task in 'objects' failed with exit status 1 (run with -v to display more information)


I was able to complete the step after making the change I mentioned above. But then I ran into a worse error and I can't figure out how to resolve it. I tried to run mpv and got this error
[email protected]:~/Videos $ mpv my-tv-show.mkv
Playing: my-tv-show.mkv
(+) Video --vid=1 (*) (h264 1276x720 23.976fps)
(+) Audio --aid=1 (*) (ac3 6ch 48000Hz)
AO: [alsa] 48000Hz stereo 2ch float
Using hardware decoding (rpi).
VO: [rpi] 1276x720 mmal
libEGL warning: DRI2: failed to authenticate
[vo/rpi] Could not create EGL surface!
[vo/rpi] EGL/GLES initialization for OSD renderer failed.
Could not initialize video chain.
Video: no video


Do you have any ideas how to resolve this? Any ideas maybe about debugging steps? I looked around on the internet but don't see any answers that seem related to mpv or that I understand :)

Thanks again for the great tutorial!

User avatar
RPi_Mike
Posts: 91
Joined: Sat Dec 09, 2017 12:57 am
Location: United States

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Mon Jan 29, 2018 8:41 am

mikedice417 wrote:
Sun Jan 28, 2018 11:41 pm
Thank you for this post! I have learned a lot by reading it and going through the steps.

I have encountered several errors at the end though.

Believe it or not, every single error you encountered was due to ONE simple thing: You didn't run rpi-update in Step 2!

As I explained in great detail in my tutorial, since I always use standard "data safety" practices – keeping a backup copy of both my operating system and my data – I had no reason to "fear" rpi-update and not use it. After all, it's quite fundamental in that it contains the very latest VideoCore libraries for the GPU. As a result, I never had to personally deal with the issue you described. But I have simulated it in recent testing.

Just by coincidence, I've been working on a surprise announcement over the last several days. It will be posted within the next 24 hours – so if you can, hold off until then!

mikedice417
Posts: 5
Joined: Mon Nov 16, 2015 1:03 am

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Tue Jan 30, 2018 2:11 am

RPi_Mike wrote:
Mon Jan 29, 2018 8:41 am
mikedice417 wrote:
Sun Jan 28, 2018 11:41 pm
Thank you for this post! I have learned a lot by reading it and going through the steps.

I have encountered several errors at the end though.

Believe it or not, every single error you encountered was due to ONE simple thing: You didn't run rpi-update in Step 2!

As I explained in great detail in my tutorial, since I always use standard "data safety" practices – keeping a backup copy of both my operating system and my data – I had no reason to "fear" rpi-update and not use it. After all, it's quite fundamental in that it contains the very latest VideoCore libraries for the GPU. As a result, I never had to personally deal with the issue you described. But I have simulated it in recent testing.

Just by coincidence, I've been working on a surprise announcement over the last several days. It will be posted within the next 24 hours – so if you can, hold off until then!
Thank you for your reply again! You are correct that running rpi-update resolved the issue and things are working now. I actually read your note about running rpi-update but I had read another doc that I mis-understood to mean that sudo apt-get dist-upgrade did the same thing as rpi-update. Now I know I was wrong :).

Again great tutorial and thank you.

treeHouse
Posts: 29
Joined: Sat Aug 06, 2016 2:35 am

Re: TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Wed Jan 31, 2018 5:47 am

Just by coincidence, I've been working on a surprise announcement over the last several days. It will be posted within the next 24 hours – so if you can, hold off until then!

Aww man, the suspense ... I think we are pushing more than 24 hours!

User avatar
RPi_Mike
Posts: 91
Joined: Sat Dec 09, 2017 12:57 am
Location: United States

APPENDICES: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Wed Jan 31, 2018 12:24 pm

---


ALL 10 TUTORIAL APPENDICES IN FULL:

I have made numerous additions and enhancements to my FFmpeg and mpv tutorial. So much has been added, in fact, that I have now exceeded the 60,000 character limit!

As a result, I had to free up space by removing several appendices from my tutorial's main page:

TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

I am therefore including all 10 appendices in this (separate) post:



---------
APPENDIX 1: MOST ESSENTIAL MPV KEYBOARD CONTROLS AND OPTIONS:


f key: Toggle the video between full screen and actual size

space bar: Toggle the video (or audio) between pause and play

left and right arrow keys: Rewind and fast-forward by 6 seconds

up and down arrow keys: Rewind and fast-forward by 1 minute

9 and 0 keys: Lower and raise the volume

o key: Make the on-screen display briefly appear to show time remaining, etc.

[ and ] keys: Speed up or slow down the video

q key: Quit out of the video (or audio)


REMINDER: Be sure to close any open Terminal windows before you use mpv, or you will lose keyboard control.


CUSTOMIZE MPV's BEHAVIOR: If you wish to customize things further, you can always modify mpv's configuration file (mpv.conf). The mpv.conf file is a standard text file (but with a ".conf" extension) that can be quickly modified with the standard Text Editor. It's located at /home/pi/.config/mpv (you may have to temporarily click View | Show Hidden in order to see these hidden files and folders in File Manager). In my instructions, I already included 2 of the most useful customizations inside the mpv.conf file:

--fullscreen <---This forces all videos, both large and small, to automatically expand to fill your screen. To prevent any distortion, this option always maintains the native aspect ratio of the video. By design, it will never "stretch" or "squeeze" the video to fit the shape of your monitor or TV. But as long as the video's aspect ratio matches the aspect ratio of your screen, the video will completely and perfectly FILL the screen. For example, if you have a 480p, 720p or 1080p video (all of which would normally have a 16:9 aspect ratio) – and you also have a monitor or TV with a 16:9 aspect ratio – then your entire screen will be filled with active pixels.

rpi-background=yes <---Sometimes, you may watch an old video – or one that's simply not as "wide" as a 16:9 aspect ratio. For example, you might have an old video from a VHS tape with a low resolution of 320 x 240 (a 4:3 aspect ratio). Or you might have a DVD video with a resolution of 720 x 480 (a 3:2 aspect ratio). Either way, these videos are not "wide" enough to perfectly fit the shape of a modern 16:9 display without being stretched or distorted. In those cases, the "rpi-background=yes" option will automatically add black bars to the left and right sides of your screen so that there are no visual distractions in the background!

--loop <---I did NOT add this option in my instructions because many people would not want this particular behavior. But if you're using your Raspberry to power a display at a trade show or other event, for example, this is a great way to make the video automatically play over and over again.

geometry=20%+0+0 <---This mpv.conf line will force a 1080p video to play at only 4% of the total surface area of a 1080p monitor or TV – and it will place it in the upper-left corner of your screen. NOTE: In case you're wondering how 20% becomes 4%, it all comes down to understanding the difference between linear dimensions and surface dimensions. Assuming the native resolution of the video is the same as your screen's resolution, 20% smaller across the X axis AND 20% smaller across the Y axis means the video would only cover 4% of your screen's total surface area (20% x 20% = 4%).

geometry=20%+1536+0 <---And what if I want a tiny video to appear in the upper-right corner of my screen? For this mpv.conf example, simple math is in order: My 1080p monitor is 1920 pixels across. 20% of 1920 is 384. And 1920 - 384 = 1536. That means the upper-left corner of the video should start at 1536 on the X axis and 0 on the Y axis. In both of these examples, tapping the "f" key will instantly toggle the video back and forth between FULL SCREEN and TINY SCREEN. Mpv handles it beautifully in the blink of an eye.

TAKE MANUAL CONTROL OF MPV: The contents of the mpv.conf file affects mpv's "fundamental" behavior. In other words, it will always affect the way mpv behaves when it plays video and audio. But for some options, you can temporarily invoke a behavior on a one-time basis without modifying your mpv.conf file. Generally, this only works for options that begin with 2 dashes (--). For example, let's say you do NOT have the "--loop" option in your mpv.conf file, but you still want to launch a video with that behavior on a one-time basis. To do that, you can manually launch the video inside Terminal (instead of double-clicking it in File Manager as you would normally do). Here are some common syntax examples for manual launching of multimedia files:

EXAMPLE A: This is the MANUAL way to play a video with mpv. Normally, as I just mentioned, you would simply double-click the file and it would automatically start playing – but I'm including these manual examples for the benefit of interested users. In this example, we are NOT invoking any special options (other than what's already in your mpv.conf file). It also assumes that Terminal is currently open inside the same folder as your video:

mpv video.mp4


EXAMPLE B: This manually instructs the video to loop continuously (in addition to whatever else your mpv.conf file may have it do). It also assumes that Terminal is currently open inside the same folder as your video:

mpv --loop video.mp4


EXAMPLE C: This does the same thing as Example B, but it does so for a video that's in a completely different folder than the one Terminal is currently located in (which is normally your "home" folder by default – /home/pi). In other words, in this example, you're using the FULL DIRECT PATH to the video. The full path to a file can be easily obtained by right-clicking any file in File Manager and selecting "Copy Path(s)". This syntax is necessary whenever Terminal and the video are in different locations:

mpv --loop /home/pi/vacations/hawaii/video.mp4


EXAMPLE D: This also does the same thing as Example B, but it places the full path inside standard quote marks. Why is this important? You must do this any time there are SPACES in the file path. That's a Linux thing, not a mpv thing. For example:

mpv --loop "/home/pi/truly awesome vacations/hawaii/video.mp4"


MPV HAS HUNDREDS OF OTHER OPTIONS AND FEATURES: For complete details, please review the extremely thorough manual at mpv's official website.






---------
APPENDIX 2: DOWNLOAD AND RETAIN HIGH-QUALITY VIDEOS FROM HUNDREDS OF WEBSITES – WITH YOUTUBE-DL:

INSTALLING YOUTUBE-DL: Although youtube-dl is available in the Raspbian software repository, it’s nearly a year old. To acquire the very latest version from youtube-dl's official website, enter the following two command lines, one at a time, in the Terminal window:

sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl

sudo chmod a+rx /usr/local/bin/youtube-dl



DOWNLOADING MP4 VIDEOS (H.264 / AAC): Let’s say you have your Raspberry hooked up to a high-definition 1920 x 1080 monitor or TV and you wish to download and enjoy a NASA video, at maximum resolution and quality, of two neutron stars colliding – with the screen completely filled-up by the video. Here’s how you do that with youtube-dl:

STEP 1: Identify the URL of the page that contains the video you want. In this example, it’s a YouTube video at the following URL:

https://www.youtube.com/watch?v=x_Akn8fUBeQ


STEP 2: Open the Terminal window and enter the following code (make sure you use a capital “-F”):

youtube-dl -F https://www.youtube.com/watch?v=x_Akn8fUBeQ


STEP 3: You will then see an impressive list of all the different audio and video formats that the YouTube server has available for the NASA video. If you look carefully, you will see there’s a video track with a resolution of 1920 x 1080 that uses the “avc1” codec at 30 FPS (Frames Per Second). “AVC1” is simply another name for the H.264 codec. The Raspberry’s GPU has been specifically optimized to play H.264 video with full-blown hardware acceleration. On the far left column, you will see a “format code” for that particular video format. In this case, it’s code “137”. You will also see a high-quality audio track that’s described as an “m4a” with a sampling rate of 44100Hz. That’s YouTube's terminology for an AAC audio track. It has format code “140”. It's the best choice for this example, since an AAC audio track is specifically designed to work with an H.264 video track when creating an MP4 video. To download the high-resolution video (with sound), you will therefore issue the following command (make sure you use a lower-case “-f”):

1920 x 1080 MP4 – 30 FPS:
youtube-dl -f 137+140 https://www.youtube.com/watch?v=x_Akn8fUBeQ

Once the download is complete, be sure to close the Terminal window or you will lose keyboard control of mpv. Then, simply double-click the MP4 video you just downloaded and mpv will automatically start playing it full screen! If you downloaded a resolution smaller than your screen, you can tap the "f" key to toggle back and forth between actual size and full screen mode.



DOWNLOADING WEBM VIDEOS (VP9 / Opus): If you want a much smaller file size at a surprisingly high quality – albeit with lower resolutions of 720p or 480p – the “VP9” format for video and the “Opus” format for audio are the ones you want. They are the most advanced compression technologies currently available. The two tracks will be automatically combined by youtube-dl (using the FFmpeg engine) and merged into a standard WebM video. Be aware that the Raspberry’s GPU is only designed to provide full hardware acceleration for MP4 videos using H.264 – so a 1080p VP9-based WebM video will push your Raspberry to the absolute limit and will likely result in at least some stutter. That's why I recommend 720p and 480p resolutions for VP9-based videos. They also, of course, should not exceed 30 FPS. Finally, if you wish to share the video with someone else who doesn’t have an advanced player like mpv, it’s best to stick with a standard H.264-based MP4 video for compatibility reasons. For the NASA video, here are the two best command lines if you want smaller file sizes (for example, if you're using a cellular connection and need to conserve your data usage). When it's competently encoded by the uploader, I've been consistently amazed by how good a humble 480p video can look when viewed full screen on my large 24 inch 1080p monitor.

1280 x 720 WEBM – 30 FPS:
youtube-dl -f 247+249 https://www.youtube.com/watch?v=x_Akn8fUBeQ

854 x 480 WEBM – 30 FPS:
youtube-dl -f 244+249 https://www.youtube.com/watch?v=x_Akn8fUBeQ



HIGH FRAME-RATE CAPABILITY: In my testing, I was surprised to discover that the Raspberry’s GPU can handle 1920 x 1080 H.264 video at DOUBLE the normal frame rate – 60 FPS without dropping even one frame! Some newer videos on YouTube are now available in 60 FPS format, and the NASA video is one of them. If you wish to download the 60 FPS version of the above 1080p video, you would simply change the video format code from 137 to 299. In other words, you would use this command line instead:

1920 x 1080 MP4 – 60 FPS:
youtube-dl -f 299+140 https://www.youtube.com/watch?v=x_Akn8fUBeQ



CAUTION: Don't assume the format codes will always be the same. YouTube is quite consistent – but even with them, the available formats can vary from one video to another. For example, I've noticed that YouTube's servers will often place different formats in a queue, prioritizing H.264 over VP9. Even for the mighty Google, encoding video is very computationally intensive and time consuming – so it makes sense for them to quickly get the H.264 version up on their server, and then process the VP9 version an hour or half hour later. Other video sites, however, use completely different format codes and are typically limited to H.264 videos – so for them, you'll want to first run youtube-dl's "-F" option (capital F), as explained above, to see exactly what formats are available. Finally, be aware that most other sites tend to integrate the video and audio tracks into a single pre-packaged MP4 video – so there won't be separate video and audio formats to choose from. YouTube does this as well, by the way. It's usually listed in the last couple lines of their available formats. In YouTube's case, you'll know if everything's already been combined if it says both "avc1" and "mp4a" in the description. In those cases, you would not use the "+" symbol. Instead, you would simply use a single format code.

YOUTUBE-DL HAS MANY OTHER FEATURES AND CAPABILITIES: To learn more, visit youtube-dl's official website.






---------
APPENDIX 3: PLAY ONLINE VIDEOS AUTOMATICALLY WITH MPV (WITHOUT ANY MANUAL DOWNLOADING):

Personally, I like to review all the available video and audio formats before I download a video. If it's a concert or music video, I may want to enjoy it multiple times. By downloading it, as described in Appendix 2, I have immediate access to it at the highest quality – even if I'm offline. Downloading is also crucial if you're using a cellular data connection and need to conserve your data. Even with an "unlimited" plan, your carrier may still throttle back your speed once you reach a monthly limit. For those instances, I've found that 480p videos that use the advanced VP9 video codec and Opus audio codec at 50 or 70 kbps are the perfect balance between file size and quality. In an HD aspect ratio, they typically have resolutions of 854 x 480. For a relatively small video, they look surprisingly good on my large 1080p monitor in full screen mode. Of course, if you want the absolute best in quality on a 1080p screen and data use is not a concern, there's nothing like a 1080p video.

In many cases, however, data use may not be a concern. You also may not have any desire to manually download (and retain) the video. And finally, you may not really care if you're viewing the most optimal format and quality – because you just want to watch a video without any thought. In those cases, I have configured my procedure to support tight integration between mpv and youtube-dl. As long as you have properly followed this tutorial and have installed a recent copy of youtube-dl (see Appendix 2), all you have to do is enter a URL after the mpv command! That will automatically invoke youtube-dl, which will start downloading the first several seconds of the video (invisibly as a background process). It does this to create a sufficient cache to help prevent buffering interruptions while playing. Once it's done that, mpv will automatically start playing the video! Generally, when operating in this mode, youtube-dl's algorithm will attempt to choose the "best quality" version of the video. It works well on leading video sites like YouTube, but there are no guarantees with less sophisticated websites. If you're unsatisfied with the results on other sites, nothing of course beats a manual review of the available formats (as described in Appendix 2).

As an example, if you want to automatically play the NASA video mentioned in Appendix 2, this simple command line is all it takes:

mpv https://www.youtube.com/watch?v=x_Akn8fUBeQ






---------
APPENDIX 4: STREAM LIVE TELEVISION FROM AROUND THE WORLD:

Not only can you "download and play" and "auto play" videos from sites like YouTube, you can also use the mpv / youtube-dl combination to stream live television broadcasts from around the world. This is often referred to as IPTV – Internet Protocol TV.

Quality television requires considerable resources to produce – so most high-end content is not freely available. But there are a few sources of free, high-quality streaming content. These are typically government-funded broadcasters that wish to freely share their nation's culture and perspective with the larger world. Perhaps the best examples of this are the national broadcasters of Australia, Germany, Japan, and France.

All four of these broadcasters make their live TV streams openly and freely available on their public websites – so they definitely want people to watch them!

You can also watch these feeds from inside the Raspberry's web browser – but that typically results in stuttering video. Mpv provides a much smoother, customizable, full-screen experience.

All four networks offer live, 24-hour TV news that is generally regarded as relatively fair and journalistically professional. To the best of my knowledge, these are the only "reputable" sources of FREE TV news from major, internationally-recognized outlets that broadcast in English. The BBC, though also very reputable, limits its TV streams to UK citizens and paying international subscribers only. There are other free "news" broadcasts available – but many of them act as propaganda outlets for various dictatorships around the world.

In my testing, it usually took about 10 seconds from the moment I entered the following command lines until the live TV broadcasts appeared (youtube-dl has to first analyze the stream before it hands it off to mpv, which then buffers and plays it). Technically, due to buffering and other factors, you will see these broadcasts on a slight "tape delay". In other words, as you're watching the news, you're actually seeing it between 10 and 50 seconds in the past. I calculated the typical delays by noting the exact moment the "top of the hour" broadcasts appeared and compared those times to an atomically-synchronized clock. Though interesting, this tiny delay obviously has no meaningful impact on the value of these feeds.

If you're a "cord cutter" and have cancelled your cable or satellite TV, there are also legitimate IPTV subscription services that will provide dozens or hundreds of streams for a monthly fee.

Here, now, are the four best freely available sources of live, international TV news. For each broadcaster, the first command line will simply cause mpv to play the stream. The second command line will play the stream AND save the video to your system. Be aware that this recording capability is an EXPERIMENTAL feature of mpv with absolutely no guarantees – although it worked quite well in my testing of these four news sites. My suggested command lines will save the video as a ".ts" file to /home/pi (you can change the path to any location you wish). As with MP4s and virtually all other video formats, you can associate ".ts" files with mpv so that double-clicking the recorded files will automatically play them:


AUSTRALIA – ABC News – Australian Broadcasting Corporation [640 x 360; 6.8 MB per minute; H.264 stream]

mpv https://abcnews24mha-lh.akamaihd.net/i/[email protected]/master.m3u8

mpv https://abcnews24mha-lh.akamaihd.net/i/[email protected]/master.m3u8 --record-file="/home/pi/Australia_News.ts"



GERMANY – DW News – Deutsche Welle [720 x 400; 13.5 MB per minute; H.264 stream]

mpv https://dwstream4-lh.akamaihd.net/i/[email protected]/master.m3u8

mpv https://dwstream4-lh.akamaihd.net/i/[email protected]/master.m3u8 --record-file="/home/pi/Germany_News.ts"



JAPAN – NHK World – Japan Broadcasting Corporation [1280 x 720; 9.4 MB per minute; H.264 stream]

mpv https://nhkwtvglobal-i.akamaihd.net/hls/live/263941/nhkwtvglobal/index_1180.m3u8

mpv https://nhkwtvglobal-i.akamaihd.net/hls/live/263941/nhkwtvglobal/index_1180.m3u8 --record-file="/home/pi/Japan_News.ts"



FRANCE – France 24 – France Vingt-Quatre [1024 x 576; 8.2 MB per minute; H.264 stream]

mpv http:\/\/static.france24.com\/live\/F24_EN_LO_HLS\/live_web.m3u8

mpv http:\/\/static.france24.com\/live\/F24_EN_LO_HLS\/live_web.m3u8 --record-file="/home/pi/France_News.ts"



CRITICAL NOTE: The URLs in the above command lines could change or expire at any time – so you may have to search for updated m3u8 streams – or use the technique I developed which appears below. If you get an error message that mentions "[ytdl_hook]" and it says "unable to download webpage", or you see a reference to "HTTP error 403", the URL is either temporarily down or has expired and become a dead link. The source for the above news streams is located at the LegalStream GitHub site. Finally, in addition to my build of mpv, you must of course have youtube-dl for the streaming function to work (preferably the latest version; see Appendix 2 for details). Hat tip to user "johndavies" for his suggestion to include streaming news!



FIND THE LATEST STREAMING URLs ON YOUR OWN Since these m3u8 streaming links can change or expire at any time, I developed the following technique to obtain the latest streaming URL for a major national broadcaster. In this case, I'm using the live TV news feed from the Australian Broadcasting Corporation as an example (the same outlet listed above). My method has the added advantage of getting the URL "straight from the source" rather than a third-party website that may have old links. It also lets you choose the resolution (and corresponding data usage). Keep in mind that although I'm using ABC News as an example, my basic approach will work on other websites as well. Whether or not it works on any particular site depends on what's available in the HTML code! Technical Note: URLs that end with m3u8 use the HLS streaming protocol, the most popular modern standard (the other leading streaming standard is the DASH protocol, which is heavily used by YouTube).


1: Visit the ABC News channel at this URL:

http://www.abc.net.au/news/newschannel

NOTE: This must be done with Chromium – the built-in web browser that comes with the Raspbian operating system. If you can do it with an alternate browser, that's fine too – just know that I have only tested it with Chromium.


2: On your keyboard, press CTRL + S. (You can also click the tiny button in the upper-right corner with the 3 vertical dots; then click "More tools"; then click "Save page as...")


3: In the bottom-right corner of the "Save File" window that appears, you'll see a drop-down button that lets you save the "Complete" web page or just the "HTML Only" version. For our purposes, you only need to save the "HTML Only" version. This has the added advantage of saving only ONE simple file to your system. Once you've set it to "HTML Only", make note of the folder it's saving it to. By default, the file will probably be called "ABC News (Australian Broadcasting Corporation).html". But to make things simple, change the file name to ABC.html and click the Save button!


4: Using File Manager, right-click the ABC.html file and then click Text Editor.


5: On your keyboard, press CTRL + F and do a text search for "m3u8" (no quotes). Note: Video streams can use extensions other than m3u8 – so if you're trying this technique on a different website, you can also search for URLs ending in .mpd, .f4m, .mp4, etc.


6: As of this writing, you'll find only one "hit" in the HTML code for "m3u8". It should appear at the very end of a URL that begins with http or https


7: That's it – that's the latest m3u8 streaming URL for ABC's 24-hour news channel! So carefully copy that URL!


8: You can now place the "mpv" command right before that URL and start watching live TV news from Australia!


CRITICAL NOTE: The URL I'm using below works perfectly at the time of this writing – but you may need to occasionally repeat the above procedure to get the latest URL if the old one expires. It's also possible that this entire procedure will suddenly stop working due to a major change in the design of the ABC website. The m3u8 link could suddenly disappear from their HTML code, for example. Nonetheless, at the time of this writing, you would simply use one of the following command lines in Terminal:

VIEW ONLY:
mpv https://abc-iview-mediapackagestreams-1.akamaized.net/out/v1/50345bf35f664739912f0b255c172ae9/index.m3u8

VIEW AND RECORD:
mpv https://abc-iview-mediapackagestreams-1.akamaized.net/out/v1/50345bf35f664739912f0b255c172ae9/index.m3u8 --record-file="/home/pi/Australia_News.ts"



WANT TO GET MORE SOPHISTICATED? I also figured out how to obtain ALL the video formats that are currently available on the streaming server used by ABC! By "formats", I mean the many different resolutions that are available. In my testing, when you do Step 8 above, the server will automatically feed the highest-resolution format to your Raspberry (1024x576). But if you need to limit your data usage for whatever reason, it can be extremely useful to know how to access the other formats.

To do that, just follow these 2 simple steps:


1: Copy the m3u8 URL you obtained previously in Step 7 and use it with youtube-dl's "-F" command (capital F). Like this, for example:

youtube-dl -F https://abc-iview-mediapackagestreams-1.akamaized.net/out/v1/50345bf35f664739912f0b255c172ae9/index.m3u8


After a few seconds, you will get a response like this (truncated by me to only show what's relevant; estimated data usage also added by me):

Format Code........ Resolution........ Data Use Per Minute
365...................... 320x180............ 3.1 MB
691...................... 512x288............ 5.4 MB
932...................... 640x360............ 7.1 MB
1276.................... 800x450............ 9.3 MB
1826.................. 1024x576.......... 13.1 MB


2: Do you see the format codes and their corresponding resolutions? Simply pick one and use it with mpv's "--ytdl-format=" option like this:

VIEW ONLY – 512 x 288 RESOLUTION:
mpv --ytdl-format=691 https://abc-iview-mediapackagestreams-1.akamaized.net/out/v1/50345bf35f664739912f0b255c172ae9/index.m3u8

VIEW AND RECORD – 512 x 288 RESOLUTION:
mpv --ytdl-format=691 https://abc-iview-mediapackagestreams-1.akamaized.net/out/v1/50345bf35f664739912f0b255c172ae9/index.m3u8 --record-file="/home/pi/Australia_News.ts"

CRITICAL NOTE: In the above command lines, I used format code "691" to retrieve the rather tiny 512x288 resolution format. If you're concerned about data usage, this actually might be the "best bang for the buck" as it looked surprisingly good at full screen on my large 1080p monitor. Feel free to replace the "691" format code with 365, 932, 1276 or 1826. Keep in mind, of course, that those are the available format codes as of this writing. In the future, you can re-do the above procedure to verify that those codes still apply – or to obtain new ones!






---------
APPENDIX 5: SAMPLE FFMPEG COMMAND LINES – TO ENCODE OR PROCESS VIDEO AND AUDIO:

NOTE: In the following examples, simply right-click any folder that contains the media files you wish to encode or process and click “Open in Terminal”. Then, run the following command lines (changing the file names and settings where appropriate).

NOTE: Be aware that most of these examples will only work for those who did my entire tutorial without skipping any steps. In other words, your encoding capabilities will be much more limited if you only did the stripped-down "play-centric" version of my tutorial, which only builds and installs FFmpeg, mpv, and libass (but not the 5 advanced encoders).

TIP: Unless it's necessary, try not to have your media folder open in File Manager while FFmpeg is actively running (by "media folder", I mean the folder in which you're encoding and processing files with FFmpeg). Remember – you can select "Open in Terminal" without actually opening a folder. Depending on what kind of work you're doing, having a folder open (whether its contents are visible on your screen or not) can slow things down considerably – because File Manager will keep automatically refreshing its contents, thus unnecessarily wasting CPU cycles.



EXAMPLE 1: Convert an old AVI video (with sound) – or any other input video format you choose – into a modern MP4 (with H.264 video and AAC audio):

ffmpeg -i input.AVI -vf format=yuv420p -c:v libx264 -crf 23 -c:a libfdk_aac -b:a 128k output.MP4

NOTE: This example uses the higher-quality (but slower) x264 software-based encoder. That means there is no GPU hardware acceleration – however it does use a CPU optimization known as “NEON” that helps to speed up the encoding. The “-crf 23” setting is the default video quality and generally produces excellent results. The lower the number, the higher the encoding quality (at the expense of a larger file size). A setting of 18 is said to be “visually lossless” – but the file size will be almost double that of a 23 setting. Be aware that the number scale is non-linear – but as a rough rule of thumb, a change of 5 to 6 will double (or cut in half) the file size. In many cases, when a smaller file size is important, a setting of 26 still maintains a high-quality appearance. Finally, the “-c:a libfdk_aac -b:a 128k” part indicates that the audio will be converted with the Fraunhofer AAC codec, at a bit rate of 128 kbps.



EXAMPLE 2: Resize a large “1080p” MP4 video (1920 x 1080) with sound into a smaller “480p” MP4 video (854 x 480) with sound – without (unnecessarily) re-encoding the original audio:

ffmpeg -i input.MP4 -vf "format=yuv420p, scale=854:480:flags=lanczos" -c:v libx264 -crf 23 -c:a copy output.MP4

NOTE: In the above FFmpeg command line, "-c:a copy" means that the audio track in the input file will be directly COPIED over to the output file without any transcoding (re-encoding). If you're certain the original audio is already in the desired format, that is the best course of action – because any additional encoding of an already-encoded file will result in some degradation in quality. If, however, the original audio is in the MP3 format (for example) and for some reason you want the output video to use ACC audio instead, then you would simply replace the "-c:a copy" with "-c:a libfdk_aac -b:a 128k".



EXAMPLE 3: Extract every frame from a 30 FPS MP4 video and output it as a padded, 7-digit numerical JPEG image sequence:

ffmpeg -i video.mp4 -vf fps=30 -q:v 2 %07d.jpg



EXAMPLE 4: Convert an alphanumeric image sequence of JPEG pictures into a 30 FPS MP4 video:

ffmpeg -framerate 30 -pattern_type glob -i "*.jpg" -vf format=yuv420p -c:v libx264 -crf 23 output.MP4



EXAMPLE 5: Add a transparent overlay – such as text or a logo – on top of a video:

ffmpeg -i input.MP4 -i overlay.png -filter_complex "[0:v][1:v] overlay=0:0" -c:v libx264 -crf 23 -c:a copy output.MP4

NOTE: The “overlay.png” file must be a transparent PNG file (with alpha channel) that includes your text or logo. This can be created with any standard image-manipulation program like GIMP or Photoshop. The dimensions of the overlay image should obviously be the same as the video's resolution. Also, in this particular example, the inputted MP4 video is assumed to have a proper sound track that can simply be copied over without re-encoding. If the sound requires encoding, simply change “-c:a copy” to “-c:a libfdk_aac -b:a 128k”.



EXAMPLE 6: Use the hardware-accelerated H.264 encoder to convert an AVI into an MP4:

ffmpeg -i input.AVI -c:v h264_omx -b:v 4500k -c:a copy output.MP4

NOTE: The hardware encoder, though about 3 times faster, has fewer options and generally, bit for bit, produces a decent but lower-quality result. The software-based encoder, which relies on the versatile and general-purpose CPU, provides the programmers maximum flexibility to use whatever coding techniques they like – but a hardware encoder, by its very nature, is constrained by the features the GPU supports. Like most things in life, there are pros and cons to each approach. In this example, the “-b:v 4500k” part indicates a bit rate of 4,500 kbps – which is generally good for a large 1080p video. Unlike the software-based encoder, there is no “crf” quality setting, since the hardware-based encoder uses bit rate as a proxy for quality. If your video is smaller than 1080p, you will obviously want to lower the bit rate accordingly – to 2,000, 1,500, etc. Finally, this example assumes the existing audio is already in an MP4-compatible format, such as an AAC or MP3, and can thus be copied over. Other audio formats are NOT compatible with the MP4 container – so if you need to transcode the audio to a compatible format, simply change “-c:a copy” to “-c:a libfdk_aac -b:a 128k”.



EXAMPLE 7: Create a WebM video with the highly-efficient VP9 video encoder and Opus audio encoder:

ffmpeg -y -i input.AVI -vf format=yuv420p -c:v libvpx-vp9 -b:v 480K -pass 1 -c:a libopus -b:a 64K -f webm /dev/null && \
ffmpeg -i input.AVI -vf format=yuv420p -c:v libvpx-vp9 -b:v 480K -pass 2 -c:a libopus -b:a 64K output.WEBM

NOTE: The entire two-line stanza that you see above should be pasted into Terminal as a single block of text. It uses a technique known as 2-pass encoding. On the first pass, it carefully analyzes the video's characteristics from start to finish. It then uses those insights to perform the actual encoding on the second pass. In this example, it's converting a 640 x 480 AVI at 30 FPS to a WebM – but you can choose almost any other video format as the input. As you can see, the input file name needs to be listed twice, but the output file name is only listed once (at the very end of the stanza). The "480K" is the video encoding rate – 480 Kbps (kiloBITS per second, not kiloBYTES). And the "64K" is the audio encoding rate – 64 Kbps. Those are the only values you need to adjust. 480K is a good, mid-range setting for a low-resolution 640 x 480 video. I analyzed the VP9 bit rates that YouTube uses by picking two extreme examples on the opposite ends of the "complexity spectrum". First, I looked at how they handle 640 x 480 videos: For a news show where the presenter is calmly sitting at a desk with a mostly static background, their systems chose an overall bit rate of only 340K. For a major outdoor concert with tons of visual complexity, lighting effects and motion, the overall bit rate shot up to 600K. I ignored the rates listed by the YouTube server – which are accessible via youtube-dl's "-F" option – because those appear to be theoretical maximum bit rates as opposed to the ACTUAL overall bit rate. Instead, my calculations are based on reality – I simply took the total size of the final video track (in bits) and divided by the duration of the video (in seconds). This is especially appropriate for the stanza in my example, because the rates you enter serve as your request for the OVERALL AVERAGE BIT RATE (not maximum, minimum, or anything else). I've tested it, and that's exactly what the VP9 encoder does – the final overall bit rate for the entire file comes out to whatever value you place in my stanza! Using the same method, my findings are that a good mid-range encoding rate for a 1920 x 1080 video is about 2000K. Be aware that the mathematical "scaling behavior" behind these algorithms is not entirely linear. For example, going from 640 x 480 to 1920 x 1080 increases the pixel count by a factor of 6.75 – but the encoding rate that's required to maintain the same visual quality only goes up by a factor of about 4 or 5. But again, whether it's high resolution or low resolution, the optimal bit rate depends on the overall scene complexity and motion levels inside the video. To give you some idea of the optimal range for 1080p, YouTube chose a rate of only 1300K for the news anchor sitting at a desk – and a rate of nearly 2500K for the visually intense concert. It's a pretty simple concept – every video is different and some are more demanding than others!

CRITICAL NOTE: When it comes to encoding a video, the H.264 / AAC combination – wrapped inside an MP4 container file – is almost always the way to go. For one thing, it's the only truly universal modern format. After all, what's the point of encoding a video if 20% or 50% of people can't view it on their device? For a whole bunch of complicated legal and business reasons, an MP4 video is the only modern format that's supported by all the major browser players – Google, Apple, and Microsoft. Thanks to a donation that pays for the licensing rights, even the non-profit Mozilla Foundation supports MP4 videos in the Firefox browser. The VP9 / Opus combination – wrapped inside a WebM container file – is more advanced and can achieve higher compression levels, but it has one giant problem: Apple doesn't support it! Globally, Apple's market share in the mobile space might be "only" 20%. But that's very misleading. Among well-off demographic groups in major countries, Apple's market share can easily exceed 80%! Although WebM is a truly open standard, its main backer is Google – which might explain why Apple still refuses to support it. It is what it is. However, if you're looking to impress your friends with the ultimate in encoding efficiency, nothing beats a WebM. Be aware, however, that there is one other big issue to consider when it comes to VP9 – especially on the Raspberry. That greater efficiency is computationally costly – VERY COSTLY! I used the above stanza on a test video with a low resolution of 640 x 480 (with medium-high motion and moderate complexity). Believe it or not, it took 5.6 MINUTES to encode every 1 SECOND of video! That's an encoding-to-playtime ratio of 336 to 1. So even if you let it run for 8 HOURS while you're sleeping, it would only encode about 85 SECONDS of 640 x 480 video! By comparison, the x264 encoder has a ratio of about 2 to 1 or 3 to 1 on a 640 x 480 video. In other words, it only takes about 2 to 3 seconds for x264 to encode each second of video – a speed that's more than 100 times faster than the VP9 encoder! Keep in mind that my testing only used a 640 x 480 video. If I were encoding a 1080p video with VP9, the ratio could easily increase to about 2,000 to 1! So why is the VP9 encoder so slow on the Raspberry? There are at least 4 reasons: (1) Apples and Oranges. The only time VP9 really shines compared to H.264 is if you're willing to throw extra computing muscle behind it. Now it's true that VP9 also supports 1-pass, constant bit-rate encoding – but my testing has shown that 1-pass encoding produces a file that's about the same size as an H.264. To top it off, my testing has also shown that VP9 1-pass encoding is only 35% faster than VP9 2-pass encoding. In other words, VP9 1-pass encoding still yields a very slow encoding ratio of 218 to 1. VP9 1-pass encoding, therefore, doesn't bring anything special to the table – especially since WebM is less universally compatible to begin with. So if you're going to the trouble to make a WebM, you might as well go "all in" and make one that shines. With the right settings, you can create a WebM video that's HALF the file size of an MP4 – with no obvious difference in visual quality! But the only way to achieve that is with 2-pass encoding. Unfortunately, 2-pass encoding is always going to gobble up more computing time than a single pass. But as I mentioned, that by itself only accounts for a 35% hit in computing time. (2) Pound for pound, the VP9 algorithm is mathematically more complex than the H.264 algorithm. (3) On the Raspberry, the VP9 encoder does not support any CPU optimizations (such as the NEON optimization that the x264 encoder is specifically designed to take advantage of). (4) On the Raspberry, the VP9 encoder does not support any parallel processing and therefore it only uses a single core (the x264 encoder is able to use all 4 cores). Leaving the other 3 cores idle automatically makes the VP9 encoder 300% slower than it could otherwise be. All of these factors are due to the mathematics and source code of VP9 itself, and are therefore unrelated to my build.



EXAMPLE 8: Create an MP3 audio file with the highly-regarded LAME encoder:

ffmpeg -i input.WAV -c:a libmp3lame -q:a 5 output.MP3

NOTE: In this example, we're converting a .wav audio file to a .mp3 audio file (the input file can be almost any audio format of your choosing). The "-q:a 5" part is the encoding quality. The LAME encoder supports a variety of settings, but my example features a quality-centric setting rather than one that attempts to achieve an EXACT – but entirely arbitrary – bit rate. This approach is known as variable bit-rate encoding (VBR). In the case of stereo sound, "-q:a 5" will yield an overall bit rate that's ABOUT (but not exactly) 128 Kbps. This is the most commonly used bit rate, and it roughly approximates "CD-quality" sound. The highest-quality setting, "-q:a 0", will produce an overall bit rate of ABOUT 256 Kbps. Finally, the lowest-quality setting, "-q:a 9", yields an overall bit rate of about 64 to 80 Kbps. For all of these quality settings, the actual bit rate will depend on the overall complexity of the sounds in the input file. Also, if it's a mono recording instead of stereo, the bit rates will be cut roughly in half. Sample rate – such as 22.05 KHz instead of the more common 44.1 KHz – will also lower the overall bit rate. Don't necessarily be dissuaded by the "lowest quality" setting. For a non-musical voice recording, such as an interview, this setting is still quite excellent and produces a file that's roughly 3 to 4 times smaller than the highest-quality setting. If you want to take a "deep dive" on how the "-q:a" setting affects perceived quality, take a sample audio file and encode it 3 separate times – with a "-q:a" of 0, 5, and 9. In many cases, unless you're an expert audiophile with a golden ear, you may not even notice a difference! Finally, be aware there are reports that some MP3 players from the 1990s and early 2000s may not be able to play files that use VBR encoding. I have not personally encountered that issue and won't bother addressing it here – just be aware that LAME also supports constant bit-rate encoding (CBR) in case you experience that issue with an antiquated player.



LEARN MORE ABOUT FFMPEG: The 8 examples I just explained are only a small sample of the astonishing capabilities of FFmpeg. For more information, visit FFmpeg's official website.






---------
APPENDIX 6: IF FOR SOME REASON YOU WISH TO COMPLETELY UNINSTALL ALL 8 PROGRAMS YOU BUILT AND INSTALLED IN THIS TUTORIAL, SIMPLY RUN THE FOLLOWING COMMAND LINE IN TERMINAL. IF YOU ONLY BUILT AND INSTALLED MPV, FFMPEG AND LIBASS, YOU WOULD OBVIOUSLY SHORTEN THE FOLLOWING COMMAND LINE TO INCLUDE ONLY THOSE 3 PROGRAMS:

sudo dpkg -r mpv ffmpeg libass lame opus libvpx fdk-aac x264-snapshot-20180125-2245


THEN, DELETE THE "preferences" FILE YOU USED TO "PIN" THE PROGRAMS (IF YOU HAVE MANUALLY PINNED OTHER PROGRAMS UNRELATED TO THIS TUTORIAL, YOU WOULD OBVIOUSLY WANT TO EDIT THIS FILE, NOT DELETE IT):

sudo rm /etc/apt/preferences






---------
APPENDIX 7: IF FOR SOME REASON YOU NEED TO RE-INSTALL ALL THE PROGRAMS IN THIS TUTORIAL ON YOUR *EXISTING* SYSTEM, YOU CAN DO IT IN LESS THAN ONE MINUTE! BY "EXISTING" SYSTEM, I MEAN THE SAME RASPBERRY WITH THE SAME OPERATING SYSTEM THAT YOU WERE USING WHEN YOU DID MY TUTORIAL. BY "EXISTING" SYSTEM, I ALSO MEAN THAT YOUR CORE SYSTEM AND "FIRMWARE" FILES HAVE NOT BEEN ALTERED. IF YOU HAVE SINCE RE-INSTALLED THE OPERATING SYSTEM OR IT'S A NEW SYSTEM – OR IT JUST ISN'T YOUR "EXISTING" SYSTEM FOR WHATEVER REASON – YOU NEED TO USE APPENDIX 8 INSTEAD. BUT IF IT TRULY IS YOUR EXISTING SYSTEM, SIMPLY RIGHT-CLICK WHATEVER FOLDER CONTAINS ALL THE ".DEB" FILES, SELECT "Open in Terminal", AND RUN THE FOLLOWING COMMAND (SEE STEP 13 OF MY TUTORIAL FOR MORE INFORMATION ON THE DEBIAN PACKAGE FILES). IF YOU BUILT ALL THE PROGRAMS IN MY TUTORIAL, THERE SHOULD BE EXACTLY 8 .DEB FILES IN THE FOLDER. IF YOU ONLY BUILT FFMPEG, MPV, and LIBASS, THERE SHOULD OBVIOUSLY BE ONLY 3 .DEB FILES IN THE FOLDER:

sudo dpkg -i *.deb






---------
APPENDIX 8: IF YOU WISH TO INSTALL ALL THE PROGRAMS IN THIS TUTORIAL ON ANOTHER RASPBERRY PI 3 – OR ON THE SAME RASPBERRY PI 3 AFTER A FRESH INSTALL OF THE OPERATING SYSTEM – IT ONLY TAKES A FEW MINUTES! JUST FOLLOW THESE 9 SIMPLE STEPS:

STEP 1: Make sure the other Raspberry has a completely normal, standard copy of the Raspbian Stretch operating system. Please see my tutorial to find out exactly what I mean by "normal" – both the requirements section and Step 2 have critical information on this topic. If you're certain that everything is perfectly "normal" with the other Raspberry, simply open Terminal and do a standard update to make sure everything is current:

sudo apt-get update

sudo apt-get dist-upgrade



STEP 2: Set your GPU memory allocation to 128 MB (mpv requires 128 MB to work). Proceed as follows:

Click the Raspberry Menu | Preferences | Raspberry Pi Configuration | Performance Tab

Change "GPU Memory" to 128

Click OK | Click YES to reboot!

CRITICAL NOTE: If the GPU memory is already at 128 MB, reboot anyway – to make sure the system upgrade in Step 1 has fully kicked in!



STEP 3: Install the dependencies required by the pre-compiled Debian packages that you created with my tutorial. Because the ".deb" files have already been compiled, most of the dependencies have already been "baked in" during the compiling process – which explains why there are only 3 dependencies in this step (instead of the 19 dependencies that appear in Step 3 of my tutorial):

sudo apt-get install libsdl2-2.0-0 libva-wayland1 libluajit-5.1-2



STEP 4: Install the OpenGL ES (GLES) and EGL files from the official Raspberry GitHub site. These GPU-related files are essential for mpv to function. Since we're installing pre-compiled ".deb" files in this procedure, the compiling has already "baked in" 2 of the 4 GPU-related files – which explains why we only need to install 2 files in this step:

sudo wget -P /opt/vc/lib https://github.com/raspberrypi/firmware/raw/master/hardfp/opt/vc/lib/libEGL.so

sudo wget -P /opt/vc/lib https://github.com/raspberrypi/firmware/raw/master/hardfp/opt/vc/lib/libGLESv2.so



STEP 5: Install all the programs that you built with my tutorial: Locate the folder that contains all the Debian package files – these ".deb" files might be on a separate thumb drive, or wherever you put them for safe keeping (see Step 13 of my tutorial for more on this topic). Then, right-click that folder, select "Open in Terminal", and run the following command. If you built all the programs, there should be exactly 8 ".deb" files in the folder. If you only built FFmpeg, mpv, and libass, there should obviously be only 3 ".deb" files in the folder:

sudo dpkg -i *.deb



STEP 6: Run this command in Terminal to make sure all the “shared libraries” are properly recognized by FFmpeg and mpv. This step is critical, so don't overlook it:

sudo ldconfig



STEP 7: Create mpv's configuration file (mpv.conf):

sudo mkdir -p /home/pi/.config/mpv

sudo nano /home/pi/.config/mpv/mpv.conf

WHEN THE NANO EDITOR APPEARS INSIDE TERMINAL, COPY AND PASTE THE FOLLOWING 2 LINES INTO THE EMPTY FILE:

--fullscreen
rpi-background=yes

SAVE THE FILE: PRESS "CTRL + X" TO EXIT OUT OF NANO – THEN TAP "Y" FOR YES – THEN TAP THE ENTER KEY TO ACCEPT THE CHANGE.

NOW RUN THIS COMMAND TO CHANGE THE OWNERSHIP OF THE mpv.conf PARENT FOLDER AND FILE FROM ROOT TO PI:

sudo chown -R pi:pi /home/pi/.config/mpv



STEP 8: Create a proper "Open With" behavior so that you can quickly open any video or audio file by simply double-clicking it within File Manager:

1: Right-click any media file whose file type you wish to associate with mpv – such as a video ending in ".mp4"

2: Click “Open With...”

3: Click the “Custom Command Line” tab

4: In the “Command line to execute" box, type “mpv %f” (all lower-case, no quotes)

5: Check “Execute in terminal emulator”

6: Check “Keep terminal window open after command execution”

7: In the “Application name” box, type “MPV” (no quotes)

8: Check “Set selected application as default action for this file type”

9: Click the OK button (and then tap the "q" key to quit when the media file starts playing)

10: Repeat this process for any other file type you wish to associate with mpv – such as .webm, .mov, .mkv, .avi, .wmv, .mpg, .mpeg, .3gp, .ts, .wav, .mp3, .m4a, .opus, etc.



STEP 9: Pin the advanced versions of the programs you just installed so that your system does not inappropriately "upgrade" them with older generic versions when you run "sudo apt-get upgrade" or "sudo apt-get dist-upgrade". Hat tip to "rpdom" for his pinning suggestion!

sudo nano /etc/apt/preferences

WHEN THE NANO EDITOR APPEARS INSIDE TERMINAL, COPY AND PASTE THE FOLLOWING 8 STANZAS INTO THE FILE (OR JUST THE FIRST 3 STANZAS IF YOU DIDN'T BUILD THE 5 ADVANCED ENCODERS):


Package: ffmpeg
Pin: version 3.4.1-1
Pin-Priority: 1001

Package: mpv
Pin: version 0.27.0-1
Pin-Priority: 1001

Package: libass
Pin: version 0.13.7-1
Pin-Priority: 1001

Package: x264-snapshot-20180125-2245
Pin: version 20180125-1
Pin-Priority: 1001

Package: fdk-aac
Pin: version 0.1.5-1
Pin-Priority: 1001

Package: libvpx
Pin: version 1.6.1-1
Pin-Priority: 1001

Package: opus
Pin: version 1.2.1-1
Pin-Priority: 1001

Package: lame
Pin: version 3.100-1
Pin-Priority: 1001


SAVE THE FILE: PRESS "CTRL + X" TO EXIT OUT OF NANO – THEN TAP "Y" FOR YES – THEN TAP THE ENTER KEY TO ACCEPT THE CHANGE.

REBOOT – Just to make sure your system is fully "fresh" and responsive!






---------
APPENDIX 9: TROUBLESHOOTING:

If you encounter any issues in the future, a good thing to try is to simply re-install all the ".deb" packages you backed up in Step 13. The entire process takes less than one minute! See Appendix 7 for details.

If mpv generates any "port" or "MMAL" errors, make sure your GPU memory allocation is 128 MB (as indicated in my mpv instructions). If mpv generates a "failed to add service" error, make sure you're not using the experimental desktop GL drivers known as Full KMS and Fake KMS (as indicated in my requirements section). And if you get a "[ytdl_hook] youtube-dl failed" message, it means you probably didn't install youtube-dl (see Appendix 2 for instructions).

Finally, as mentioned previously, be sure that all Terminal windows are closed before you use mpv (in order to maintain keyboard control).






---------
APPENDIX 10: TECHNICAL NOTES AND OBSERVATIONS:

There are only 19 "explicitly" required dependencies in Step 3 of my tutorial. In other words, only 19 software packages are explicitly listed BY NAME in the "sudo apt-get install" stanza. However, some of the 19 dependencies have dependencies of their own that will automatically "come along for the ride". Some of these other dependencies are necessary for my tutorial to compile and build, and some of them are not. Either way, those other dependencies are determined by the maintainers of the official Raspbian repository – not me. Through careful testing, I have identified the other dependencies whose installation is automatically "triggered" by the 19 explicit dependencies. As just one example, I observed that the explicit installation of "automake" automatically triggers the installation of "autoconf". Autoconf is a critical dependency for my tutorial to work, yet I have deliberately NOT included it due to the fact that its installation is automatically triggered by the installation of automake! Two other essential dependencies that I have NOT explicitly listed are libxcb1-dev and libxcb-xfixes0-dev – both of which are automatically installed as a result of installing libsdl2-dev (one of the 19 explicit dependencies). Theoretically, there is some risk in not explicitly requesting more dependencies – because at some point in the future, the repository behavior could change – resulting in critical dependencies not being installed. Nonetheless, my minimalist philosophy considers it "bad form" to explicitly list more dependencies than necessary. The stanza I created, therefore, has been carefully optimized and verified to provide everything the tutorial needs – no more, no less. Of course, since I obviously don't have a crystal ball, it should go without saying that I can only certify the truth of my statements "at the time of their writing"!






---------
THIS PAGE ONLY CONTAINS THE APPENDICES – PLEASE VISIT MY MAIN TUTORIAL PAGE:

TUTORIAL: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

.
Last edited by RPi_Mike on Thu May 24, 2018 3:20 am, edited 15 times in total.

86jumpy
Posts: 13
Joined: Mon Jan 19, 2015 9:58 am

Re: MAJOR UPDATE: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Fri Feb 02, 2018 9:41 pm

Wow wonderful guide. Compliments. I have performed all the steps and at the moment everything works perfectly. I have three questions to ask:

1) Once you get the 8 * .deb files, if you want to reinstall mpv what is the exact procedure?

2) If in the future I had to run sudo apt-get dist-upgrade, could mpv no longer work as before?

3) Are there any minimum hopes that the procedure will work on Raspberry1 mod B or Raspberry 2 mod B?

Thanks in advance.

User avatar
rpdom
Posts: 12810
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: MAJOR UPDATE: Play or Encode High-Quality Video and Audio – with FFmpeg and mpv

Fri Feb 02, 2018 9:50 pm

RPi_Mike wrote:
Wed Jan 31, 2018 12:24 pm
If you run a system update – such as "sudo apt-get upgrade" or "sudo apt-get dist-upgrade" – there's at least some chance that Raspbian's package manager may see that you've installed FFmpeg, mpv, x264 (or any of the 8 programs) and wrongly think "oh, I need to update these programs!" If it does that, it may overwrite your enhanced versions with the repository's stock versions, thus eliminating the advanced features this tutorial created.
That's where apt pinning can help. Pin to the specific versions from your .deb files and they won't get updated by the standard ones.

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 11 guests