Operations on Audio/Video files using ffmpeg, avconv, and youtube-dl


This article aims to explain how to perform the following operations on audio files using a linux terminal:

  1. Download a youtube video
  2. Convert Audio/Video files between various formats
  3. Combine two mp3 files
  4. Play audio and video files on the terminal

Installation

sudo apt-get update
sudo apt-get install python3-pip
sudo pip3 install youtube-dl
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt install libav-tools
sudo apt-get install ffmpeg
sudo apt-get install libid3-3.8.3-dev
sudo apt-get install mp3wrap
  1. Download a youtube video:
    Youtube videos can directly be downloaded from the terminal by using the youtube-dl library. Its usage is simple.

    youtube-dl [options] [url]
    

    There are several parameters that may be passed under options such as:

    • -o [output file name]
    • -i (ignore errors)
    • –abort-on-error (stop download if any video is unavailable

    Example:



     youtube-dl https://www.youtube.com/watch?v=ka0tlCl7hwI -o geeksforgeeks 

    Below is an example of its usage. The file will be stored in the present working directory.

    It is possible that you receive a warning saying that the avconv version is not upto date. This can be corrected by either upgrading (sudo apt-get upgrade) or by forcing youtube-dl to use ffmpeg instead as shown below

     youtube-dl --prefer-ffmpeg [url] 

    The downloaded files can be played normally by any audio/video player.

  2. Convert A/V files between various formats:
    avconv will be made use of here. For example, consider a file ‘g4g.mkv’. Suppose we want to convert this to an mp3 file. The following command must be used:

    avconv -i "g4g.mkv" -c:a libmp3lame "g4g.mp3"
    

    Notice how all that had to be done was change the extension. All Video files can be converted to audio files, and Video files can be interconverted between video formats, for example, avi to mp4. However, quality might degrade.

  3. Combine two mp3 files:
    The following set of commands should be used to combine mp3 files.

    mp3wrap tmp_MP3WRAP.mp3 [file 1] [file 2]
    ffmpeg -i tmp_MP3WRAP.mp3 -acodec copy [output name] && rm tmp_MP3WRAP.mp3
    

  4. Play audio and videos:
    This operations makes use of ffplay, and is quite simple to use

    ffplay [file name]
    

Many more interesting and useful applications of ffmpeg can be found here

This article is contributed by Deepak Srivatsav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.