# Buggy code
def prepare_filename(self, info_dict):
    """Generate the output filename."""
    try:
        template_dict = dict(info_dict)

        template_dict["epoch"] = int(time.time())
        autonumber_size = self.params.get("autonumber_size")
        if autonumber_size is None:
            autonumber_size = 5
        autonumber_templ = "%0" + str(autonumber_size) + "d"
        template_dict["autonumber"] = autonumber_templ % self._num_downloads
        if template_dict.get("playlist_index") is not None:
            template_dict["playlist_index"] = "%0*d" % (
                len(str(template_dict["n_entries"])),
                template_dict["playlist_index"],
            )
        if template_dict.get("resolution") is None:
            if template_dict.get("width") and template_dict.get("height"):
                template_dict["resolution"] = "%dx%d" % (
                    template_dict["width"],
                    template_dict["height"],
                )
            elif template_dict.get("height"):
                template_dict["resolution"] = "%sp" % template_dict["height"]
            elif template_dict.get("width"):
                template_dict["resolution"] = "?x%d" % template_dict["width"]

        sanitize = lambda k, v: sanitize_filename(
            compat_str(v),
            restricted=self.params.get("restrictfilenames"),
            is_id=(k == "id"),
        )
        template_dict = dict(
            (k, sanitize(k, v)) for k, v in template_dict.items() if v is not None
        )
        template_dict = collections.defaultdict(lambda: "NA", template_dict)

        outtmpl = self.params.get("outtmpl", DEFAULT_OUTTMPL)
        tmpl = compat_expanduser(outtmpl)
        filename = tmpl % template_dict
        return filename
    except ValueError as err:
        self.report_error(
            "Error in output template: "
            + str(err)
            + " (encoding: "
            + repr(preferredencoding())
            + ")"
        )
        return None


# Traceback (FFmpegPostProcessorError)
python -m youtube_dl -f 160+140 -v Y9o7ILCPSFU
[debug] System config: []
[debug] User config: []
[debug] Command-line args: ['-f', '160+140', '-v', 'Y9o7ILCPSFU']
[debug] Encodings: locale cp1251, fs mbcs, out cp866, pref cp1251
[debug] youtube-dl version 2015.01.23.4
[debug] Git HEAD: 80a49d3
[debug] Python version 2.7.6 - Windows-2003Server-5.2.3790-SP2
[debug] exe versions: ffmpeg N-68756-g627f565, ffprobe N-68694-g7c210c4, rtmpdump 2.3
[debug] Proxy map: {}
[youtube] Y9o7ILCPSFU: Downloading webpage
[youtube] Y9o7ILCPSFU: Extracting video information
[youtube] Y9o7ILCPSFU: Downloading DASH manifest
[debug] Invoking downloader on 'https://r19---sn-oguesne6.googlevideo.com/videoplayback?id=63da3b20b08f4855&amp;itag=160&amp;source=youtube&amp;requiressl=yes&amp;pl=22&amp;mv=m&amp;ms=au&amp;mm=31&amp;nh=IgpwcjAyLm5ydDE5KgkxMjcuMC4wLjE&amp;ratebypass=yes&amp;mime=video/mp4&amp;gir=yes&amp;clen=1646278&amp;lmt=1418493117455845&amp;dur=272.105&amp;mt=1422286131&amp;upn=ST6SA4yKetw&amp;signature=5982C88337984594B47DD0AE038E9E079D694B4F.09BBB44FE6C75594F04EF70F97E252516F31940D&amp;key=dg_yt0&amp;sver=3&amp;fexp=900718,907263,922243,924638,927622,930016,9406
082,9406326,941004,943917,947225,948124,952302,952605,952901,955301,957103,95710
5,957201,959701,962715&amp;ip=PRIVATE&amp;ipbits=0&amp;expire=1422307758&amp;sparams=ip,ip
bits,expire,id,itag,source,requiressl,pl,mv,ms,mm,nh,ratebypass,mime,gir,clen,lmt,dur'
[download] Destination: Trentemøller - Miss You (Mahmut Orhan Remix)-Y9o7ILCPSFU.f160.mp4
[download] 100% of 1.57MiB in 00:33
[debug] Invoking downloader on 'https://r19---sn-oguesne6.googlevideo.com/videoplayback?id=63da3b20b08f4855&amp;itag=140&amp;source=youtube&amp;requiressl=yes&amp;pl=22&amp;mv=m&amp;ms=au&amp;mm=31&amp;nh=IgpwcjAyLm5ydDE5KgkxMjcuMC4wLjE&amp;ratebypass=yes&amp;mime=audio/mp4&amp;gir=yes&amp;clen=4371063&amp;lmt=1418493113768940&amp;dur=272.230&amp;mt=1422286131&amp;upn=ST6SA4yKetw&amp;signature=23BBC9979391613C6CBE8C5FC1E54B3E01BEF5D7.8686958B23B344D9E7E24C1AF687379F4D291A47&amp;key=dg_yt0&amp;sver=3&amp;fexp=900718,907263,922243,924638,927622,930016,9406082,9406326,941004,943917,947225,948124,952302,952605,952901,955301,957103,957105,957201,959701,962715&amp;ip=PRIVATE&amp;ipbits=0&amp;expire=1422307758&amp;sparams=ip,ipbits,expire,id,itag,source,requiressl,pl,mv,ms,mm,nh,ratebypass,mime,gir,clen,lm
t,dur'
[download] Destination: Trentemøller - Miss You (Mahmut Orhan Remix)-Y9o7ILCPSFU.f140.m4a
[download] 100% of 4.17MiB in 01:46
[ffmpeg] Merging formats into "Trentemøller - Miss You (Mahmut Orhan Remix)-Y9o7ILCPSFU.mp4"
[debug] ffmpeg command line: ffmpeg -y -i 'Trentemller - Miss You (Mahmut OrhanRemix)-Y9o7ILCPSFU.f160.mp4' -i 'Trentemller - Miss You (Mahmut Orhan Remix)-Y9o7ILCPSFU.f140.m4a' -c copy -map 0:v:0 -map 1:a:0 'Trentemller - Miss You (Mahmut Orhan Remix)-Y9o7ILCPSFU.mp4'
ERROR: Trentemller - Miss You (Mahmut Orhan Remix)-Y9o7ILCPSFU.f160.mp4: No such file or directory
Traceback (most recent call last):
File "youtube_dl\YoutubeDL.py", line 1384, in post_process
keep_video_wish, info = pp.run(info)
File "youtube_dl\postprocessor\ffmpeg.py", line 544, in run
self.run_ffmpeg_multiple_files(info['__files_to_merge'], filename, args)
File "youtube_dl\postprocessor\ffmpeg.py", line 103, in run_ffmpeg_multiple_files
raise FFmpegPostProcessorError(msg)
FFmpegPostProcessorError

# Fixed code
def prepare_filename(self, info_dict):
    """Generate the output filename."""
    try:
        template_dict = dict(info_dict)

        template_dict["epoch"] = int(time.time())
        autonumber_size = self.params.get("autonumber_size")
        if autonumber_size is None:
            autonumber_size = 5
        autonumber_templ = "%0" + str(autonumber_size) + "d"
        template_dict["autonumber"] = autonumber_templ % self._num_downloads
        if template_dict.get("playlist_index") is not None:
            template_dict["playlist_index"] = "%0*d" % (
                len(str(template_dict["n_entries"])),
                template_dict["playlist_index"],
            )
        if template_dict.get("resolution") is None:
            if template_dict.get("width") and template_dict.get("height"):
                template_dict["resolution"] = "%dx%d" % (
                    template_dict["width"],
                    template_dict["height"],
                )
            elif template_dict.get("height"):
                template_dict["resolution"] = "%sp" % template_dict["height"]
            elif template_dict.get("width"):
                template_dict["resolution"] = "?x%d" % template_dict["width"]

        sanitize = lambda k, v: sanitize_filename(
            compat_str(v),
            restricted=self.params.get("restrictfilenames"),
            is_id=(k == "id"),
        )
        template_dict = dict(
            (k, sanitize(k, v)) for k, v in template_dict.items() if v is not None
        )
        template_dict = collections.defaultdict(lambda: "NA", template_dict)

        outtmpl = self.params.get("outtmpl", DEFAULT_OUTTMPL)
        tmpl = compat_expanduser(outtmpl)
        filename = tmpl % template_dict
        # Temporary fix for #4787
        # 'Treat' all problem characters by passing filename through preferredencoding
        # to workaround encoding issues with subprocess on python2 @ Windows
        if sys.version_info < (3, 0) and sys.platform == "win32":
            filename = encodeFilename(filename, True).decode(preferredencoding())
        return filename
    except ValueError as err:
        self.report_error(
            "Error in output template: "
            + str(err)
            + " (encoding: "
            + repr(preferredencoding())
            + ")"
        )
        return None